This commit is contained in:
Stefanescu Mihai
2026-03-18 12:18:07 +02:00
parent a4383337f4
commit 6ad100d331
12 changed files with 513 additions and 77 deletions
@@ -0,0 +1,64 @@
<?php
namespace App\Http\Controllers;
use App\Models\Website;
use App\Services\AnalyticsQueryService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\View\View;
class DashboardController extends Controller
{
public function show(Request $request, Website $website): View
{
$this->authorize('view', $website);
[$from, $to] = $this->parseDateRange($request);
$service = AnalyticsQueryService::for($website, $from, $to);
$metrics = $service->summary();
$chartData = $service->visitorsOverTime();
$activeVisitors = AnalyticsQueryService::activeVisitors($website);
return view('dashboard.show', compact(
'website', 'metrics', 'chartData', 'activeVisitors', 'from', 'to',
));
}
public function realtime(Website $website): JsonResponse
{
$this->authorize('view', $website);
return response()->json([
'active' => AnalyticsQueryService::activeVisitors($website),
]);
}
private function parseDateRange(Request $request): array
{
$range = $request->get('range', '7d');
$to = Carbon::today();
$from = match ($range) {
'today' => Carbon::today(),
'7d' => Carbon::today()->subDays(6),
'30d' => Carbon::today()->subDays(29),
'90d' => Carbon::today()->subDays(89),
'custom' => Carbon::parse($request->get('from', Carbon::today()->subDays(6)->toDateString())),
default => Carbon::today()->subDays(6),
};
if ($range === 'custom' && $request->has('to')) {
$to = Carbon::parse($request->get('to'));
}
if ($from->gt($to)) {
[$from, $to] = [$to, $from];
}
return [$from, $to];
}
}
+3 -3
View File
@@ -31,11 +31,11 @@ class WebsiteController extends Controller
->with('success', 'Website added successfully.');
}
public function show(Website $website): View
public function show(Website $website): RedirectResponse
{
$this->authorize('view', $website);
return view('websites.show', compact('website'));
return redirect()->route('dashboard.show', $website);
}
public function edit(Website $website): View
@@ -54,7 +54,7 @@ class WebsiteController extends Controller
$website->update($data);
return redirect()
->route('websites.show', $website)
->route('dashboard.show', $website)
->with('success', 'Website updated successfully.');
}