Build & Deploy / build (push) Successful in 19s
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
67 lines
1.9 KiB
PHP
67 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use Filament\Widgets\ChartWidget;
|
|
use Functional\Tracking\Enums\WorkoutSessionStatus;
|
|
use Functional\Tracking\Models\WorkoutSession;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
/**
|
|
* Séances complétées par jour sur les 30 derniers jours (barres).
|
|
* Permet de repérer les pics/creux d'activité de la communauté.
|
|
*/
|
|
class WorkoutSessionsPerDayChart extends ChartWidget
|
|
{
|
|
protected static ?int $sort = 2;
|
|
|
|
protected int|string|array $columnSpan = 'full';
|
|
|
|
public function getHeading(): string
|
|
{
|
|
return 'Séances complétées (30 derniers jours)';
|
|
}
|
|
|
|
protected function getData(): array
|
|
{
|
|
$start = Carbon::now()->subDays(29)->startOfDay();
|
|
|
|
// Nombre de séances complétées par jour, sur la fenêtre glissante de 30 jours.
|
|
$countsByDay = WorkoutSession::query()
|
|
->where('status', WorkoutSessionStatus::Completed)
|
|
->where('started_at', '>=', $start)
|
|
->selectRaw('DATE(started_at) as day, COUNT(*) as total')
|
|
->groupBy('day')
|
|
->pluck('total', 'day');
|
|
|
|
$labels = [];
|
|
$data = [];
|
|
|
|
// On matérialise chaque jour (même sans séance) pour un axe continu.
|
|
for ($offset = 0; $offset < 30; $offset++) {
|
|
$date = $start->copy()->addDays($offset);
|
|
$key = $date->format('Y-m-d');
|
|
|
|
$labels[] = $date->format('d/m');
|
|
$data[] = (int) ($countsByDay[$key] ?? 0);
|
|
}
|
|
|
|
return [
|
|
'datasets' => [
|
|
[
|
|
'label' => 'Séances complétées',
|
|
'data' => $data,
|
|
'backgroundColor' => '#f59e0b',
|
|
'borderRadius' => 4,
|
|
],
|
|
],
|
|
'labels' => $labels,
|
|
];
|
|
}
|
|
|
|
protected function getType(): string
|
|
{
|
|
return 'bar';
|
|
}
|
|
}
|