Files
fotospiel-app/app/Services/Analytics/EventAnalyticsService.php
Codex Agent fa33e7cbcf Fix Event & EventType resource issues and apply formatting
- Fix EventType deletion error handling (constraint violations)
- Fix Event update error (package_id column missing)
- Fix Event Type dropdown options (JSON display issue)
- Fix EventPackagesRelationManager query error
- Add missing translations for deletion errors
- Apply Pint formatting
2026-01-21 10:34:06 +01:00

73 lines
2.2 KiB
PHP

<?php
namespace App\Services\Analytics;
use App\Models\Event;
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();
}
}