47 lines
1.4 KiB
PHP
Executable File
47 lines
1.4 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\AiProvider;
|
|
use App\Services\PromptGenerationService;
|
|
use App\Services\PromptProjectService;
|
|
use Illuminate\View\View;
|
|
|
|
class DashboardController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly PromptProjectService $promptProjectService,
|
|
private readonly PromptGenerationService $promptGenerationService
|
|
) {}
|
|
|
|
public function index(): View
|
|
{
|
|
$userId = auth()->id();
|
|
$user = auth()->user();
|
|
|
|
$stats = [
|
|
'projects' => $this->promptProjectService->index()['total'] ?? 0,
|
|
'generations' => $this->promptGenerationService->index()->total() ?? 0,
|
|
'providers' => AiProvider::count(),
|
|
'tokens' => $user && $user->role === 'superadmin'
|
|
? \App\Models\PromptGeneration::sum('tokens_used') ?? 0
|
|
: \App\Models\PromptGeneration::where('user_id', $userId)->sum('tokens_used') ?? 0,
|
|
];
|
|
|
|
$recentGenerationsQuery = \App\Models\PromptGeneration::with(['project', 'model'])
|
|
->latest()
|
|
->limit(5);
|
|
|
|
if (!$user || $user->role !== 'superadmin') {
|
|
$recentGenerationsQuery->where('user_id', $userId);
|
|
}
|
|
|
|
$recentGenerations = $recentGenerationsQuery->get();
|
|
|
|
return view('dashboard', compact('stats', 'recentGenerations'));
|
|
}
|
|
}
|