154 lines
4.6 KiB
PHP
Executable File
154 lines
4.6 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Insight;
|
|
use App\Models\Subject;
|
|
use App\Models\TokenUsage;
|
|
use App\Models\UserSubject;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\View\View;
|
|
|
|
/**
|
|
* Controller for the main user dashboard.
|
|
* Displays insights, statistics, token usage, and charts.
|
|
*/
|
|
class DashboardController extends Controller
|
|
{
|
|
/**
|
|
* Display the user dashboard with insights and statistics.
|
|
*/
|
|
public function index(Request $request): View
|
|
{
|
|
/** @var \App\Models\User $user */
|
|
$user = Auth::user();
|
|
|
|
// Basic statistics
|
|
$subjectsCount = UserSubject::where('user_id', $user->id)->count();
|
|
$activeSubjectsCount = UserSubject::where('user_id', $user->id)
|
|
->where('is_active', true)
|
|
->count();
|
|
$insightsCount = Insight::where('user_id', $user->id)->count();
|
|
|
|
// Token usage statistics
|
|
$totalInputTokens = TokenUsage::where('user_id', $user->id)->sum('input_tokens');
|
|
$totalOutputTokens = TokenUsage::where('user_id', $user->id)->sum('output_tokens');
|
|
$totalTokens = $totalInputTokens + $totalOutputTokens;
|
|
|
|
// Recent insights (last 10)
|
|
/** @var \Illuminate\Database\Eloquent\Collection<int, Insight> $recentInsights */
|
|
$recentInsights = Insight::where('user_id', $user->id)
|
|
->with('subject')
|
|
->orderByDesc('created_at')
|
|
->take(10)
|
|
->get();
|
|
|
|
// Active subjects with schedules
|
|
/** @var \Illuminate\Database\Eloquent\Collection<int, UserSubject> $activeSubjects */
|
|
$activeSubjects = UserSubject::where('user_id', $user->id)
|
|
->where('is_active', true)
|
|
->with('subject')
|
|
->get();
|
|
|
|
// Token usage chart data (last 30 days)
|
|
$tokenChartData = $this->getTokenChartData($user->id);
|
|
|
|
// Insights per subject chart data
|
|
$subjectChartData = $this->getSubjectChartData($user->id);
|
|
|
|
// Weekly activity data
|
|
$weeklyActivity = $this->getWeeklyActivity($user->id);
|
|
|
|
return view('dashboard.index', compact(
|
|
'subjectsCount',
|
|
'activeSubjectsCount',
|
|
'insightsCount',
|
|
'totalInputTokens',
|
|
'totalOutputTokens',
|
|
'totalTokens',
|
|
'recentInsights',
|
|
'activeSubjects',
|
|
'tokenChartData',
|
|
'subjectChartData',
|
|
'weeklyActivity'
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Get token usage data for charts (last 30 days).
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function getTokenChartData(int $userId): array
|
|
{
|
|
$days = 30;
|
|
$data = [];
|
|
|
|
for ($i = $days - 1; $i >= 0; $i--) {
|
|
$date = now()->subDays($i)->format('Y-m-d');
|
|
$dailyUsage = TokenUsage::where('user_id', $userId)
|
|
->whereDate('created_at', $date)
|
|
->selectRaw('COALESCE(SUM(input_tokens), 0) as input_tokens')
|
|
->selectRaw('COALESCE(SUM(output_tokens), 0) as output_tokens')
|
|
->first();
|
|
|
|
$data['labels'][] = now()->subDays($i)->format('d/m');
|
|
$data['input'][] = (int) ($dailyUsage->input_tokens ?? 0);
|
|
$data['output'][] = (int) ($dailyUsage->output_tokens ?? 0);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Get insights per subject for chart data.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function getSubjectChartData(int $userId): array
|
|
{
|
|
$insightsBySubject = Insight::where('user_id', $userId)
|
|
->selectRaw('subject_id, COUNT(*) as total')
|
|
->groupBy('subject_id')
|
|
->with('subject')
|
|
->get();
|
|
|
|
$data = [
|
|
'labels' => [],
|
|
'values' => [],
|
|
];
|
|
|
|
foreach ($insightsBySubject as $item) {
|
|
$data['labels'][] = $item->subject?->name ?? __('Unknown');
|
|
$data['values'][] = (int) $item->total;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Get weekly activity data (insights per day of week).
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function getWeeklyActivity(int $userId): array
|
|
{
|
|
$daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
|
$data = [
|
|
'labels' => $daysOfWeek,
|
|
'values' => [],
|
|
];
|
|
|
|
foreach (range(0, 6) as $day) {
|
|
$count = Insight::where('user_id', $userId)
|
|
->whereRaw('DAYOFWEEK(created_at) = ?', [$day + 1])
|
|
->count();
|
|
$data['values'][] = $count;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
} |