Add a statistics page to the panel
The most read, reads per language with a daily chart, and published content nobody opened, each compared with the period before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
52
app/Filament/Charts/ViewsByLanguageChart.php
Normal file
52
app/Filament/Charts/ViewsByLanguageChart.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Filament\Charts;
|
||||
|
||||
use App\Support\ViewStats;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
|
||||
/**
|
||||
* Reads per day, one line per language. Lives outside `Filament/Widgets` so
|
||||
* the dashboard does not discover it: it belongs to the statistics page,
|
||||
* which says which period to draw.
|
||||
*/
|
||||
class ViewsByLanguageChart extends ChartWidget
|
||||
{
|
||||
public int $days = 30;
|
||||
|
||||
protected int|string|array $columnSpan = 'full';
|
||||
|
||||
protected ?string $maxHeight = '280px';
|
||||
|
||||
public function getHeading(): string
|
||||
{
|
||||
return __('panel.statistics.daily');
|
||||
}
|
||||
|
||||
protected function getType(): string
|
||||
{
|
||||
return 'line';
|
||||
}
|
||||
|
||||
protected function getData(): array
|
||||
{
|
||||
$daily = (new ViewStats($this->days))->daily();
|
||||
$colors = ['#f59e0b', '#3b82f6', '#10b981', '#ef4444', '#8b5cf6'];
|
||||
|
||||
return [
|
||||
'labels' => $daily['labels'],
|
||||
'datasets' => collect($daily['series'])
|
||||
->values()
|
||||
->map(fn (array $data, int $i) => [
|
||||
'label' => config('locales.supported.'.array_keys($daily['series'])[$i].'.native'),
|
||||
'data' => $data,
|
||||
'borderColor' => $colors[$i % count($colors)],
|
||||
'backgroundColor' => $colors[$i % count($colors)],
|
||||
'tension' => 0.3,
|
||||
])
|
||||
->all(),
|
||||
];
|
||||
}
|
||||
}
|
||||
84
app/Filament/Pages/Statistics.php
Normal file
84
app/Filament/Pages/Statistics.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Filament\Pages;
|
||||
|
||||
use App\Support\ViewStats;
|
||||
use BackedEnum;
|
||||
use Filament\Pages\Page as FilamentPage;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Livewire\Attributes\Url;
|
||||
use UnitEnum;
|
||||
|
||||
/**
|
||||
* What people read: the most read, how each language is doing, and what is
|
||||
* on the site that nobody opened. Counts only — what the frontend reports is
|
||||
* a daily total per record and language, with nothing about the person.
|
||||
*/
|
||||
class Statistics extends FilamentPage
|
||||
{
|
||||
public const PERIODS = [7, 30, 90];
|
||||
|
||||
protected string $view = 'filament.pages.statistics';
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedChartBar;
|
||||
|
||||
protected static ?int $navigationSort = 6;
|
||||
|
||||
#[Url]
|
||||
public int $days = 30;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
if (! in_array($this->days, self::PERIODS, true)) {
|
||||
$this->days = 30;
|
||||
}
|
||||
}
|
||||
|
||||
public function updatedDays(): void
|
||||
{
|
||||
$this->mount();
|
||||
}
|
||||
|
||||
public static function canAccess(): bool
|
||||
{
|
||||
return Auth::user()?->can('panel.access') ?? false;
|
||||
}
|
||||
|
||||
public static function getNavigationGroup(): string|UnitEnum|null
|
||||
{
|
||||
return __('panel.groups.content');
|
||||
}
|
||||
|
||||
public static function getNavigationLabel(): string
|
||||
{
|
||||
return __('panel.statistics.title');
|
||||
}
|
||||
|
||||
public function getTitle(): string|Htmlable
|
||||
{
|
||||
return __('panel.statistics.title');
|
||||
}
|
||||
|
||||
public function stats(): ViewStats
|
||||
{
|
||||
return new ViewStats($this->days);
|
||||
}
|
||||
|
||||
/**
|
||||
* "+52 %", "−10 %", or "new" when there was nothing to compare with.
|
||||
*/
|
||||
public function change(int $now, int $before): string
|
||||
{
|
||||
if ($before === 0) {
|
||||
return $now > 0 ? __('panel.statistics.new') : '—';
|
||||
}
|
||||
|
||||
$percent = (int) round(($now - $before) * 100 / $before);
|
||||
|
||||
return ($percent > 0 ? '+' : ($percent < 0 ? '−' : '')).abs($percent).' %';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user