Files
fotospiel-app/app/Services/Analytics/EventAnalyticsService.php
Codex Agent ee3e9737c4
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
tests / ui (push) Has been cancelled
feat: implement advanced analytics for mobile admin dashboard
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
2026-01-06 16:17:23 +01:00

74 lines
2.3 KiB
PHP

<?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();
}
}