feat: implement advanced analytics for mobile admin dashboard
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled

This commit includes:
- Backend EventAnalyticsService and Controller
- API endpoint for event analytics
- Frontend EventAnalyticsPage with custom bar charts and top contributor lists
- Analytics shortcut on the dashboard
- Feature-lock upsell UI for non-premium users
This commit is contained in:
Codex Agent
2026-01-06 16:17:23 +01:00
parent 322cafa3c2
commit ee3e9737c4
10 changed files with 425 additions and 2 deletions

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Http\Controllers\Api\Tenant;
use App\Http\Controllers\Controller;
use App\Models\Event;
use App\Services\Analytics\EventAnalyticsService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
class EventAnalyticsController extends Controller
{
public function __construct(
private readonly EventAnalyticsService $analyticsService
) {}
public function show(Request $request, Event $event): JsonResponse
{
// Check if package has advanced_analytics feature
$packageFeatures = $event->eventPackage?->package?->features ?? [];
// Handle array or JSON string features
if (is_string($packageFeatures)) {
$packageFeatures = json_decode($packageFeatures, true) ?? [];
}
$hasAccess = in_array('advanced_analytics', $packageFeatures, true);
if (!$hasAccess) {
return response()->json([
'message' => 'This feature is only available in the Premium package.',
'code' => 'feature_locked'
], 403);
}
$timeline = $this->analyticsService->getTimeline($event);
$contributors = $this->analyticsService->getTopContributors($event);
$tasks = $this->analyticsService->getTaskStats($event);
return response()->json([
'timeline' => $timeline,
'contributors' => $contributors,
'tasks' => $tasks,
]);
}
}

View File

@@ -0,0 +1,73 @@
<?php
namespace App\Services\Analytics;
use App\Models\Event;
use App\Models\Photo;
use Illuminate\Support\Facades\DB;
class EventAnalyticsService
{
/**
* Get the activity timeline (uploads per hour).
*/
public function getTimeline(Event $event): array
{
// Group by hour of created_at
// Adjust for timezone if necessary, but for now we'll use UTC or server time
// Ideally we should use the event's timezone if stored, or client's.
// We'll return data in ISO format buckets.
$stats = $event->photos()
->selectRaw('DATE_FORMAT(created_at, "%Y-%m-%d %H:00:00") as hour, count(*) as count')
->groupBy('hour')
->orderBy('hour')
->get();
return $stats->map(fn ($item) => [
'timestamp' => $item->hour,
'count' => (int) $item->count,
])->toArray();
}
/**
* Get top contributors (users with most uploads).
*/
public function getTopContributors(Event $event, int $limit = 5): array
{
$stats = $event->photos()
->select('guest_name', DB::raw('count(*) as count'), DB::raw('sum(likes_count) as likes'))
->whereNotNull('guest_name')
->groupBy('guest_name')
->orderByDesc('count')
->limit($limit)
->get();
return $stats->map(fn ($item) => [
'name' => $item->guest_name,
'count' => (int) $item->count,
'likes' => (int) $item->likes,
])->toArray();
}
/**
* Get task completion stats.
*/
public function getTaskStats(Event $event, int $limit = 5): array
{
$stats = $event->photos()
->whereNotNull('task_id')
->select('task_id', DB::raw('count(*) as count'))
->groupBy('task_id')
->with('task:id,name,name_translations') // Eager load task name
->orderByDesc('count')
->limit($limit)
->get();
return $stats->map(fn ($item) => [
'task_id' => $item->task_id,
'task_name' => $item->task ? $item->task->getNameForLocale() : 'Unknown Task', // Assuming getNameForLocale exists or similar
'count' => (int) $item->count,
])->toArray();
}
}