mirror of
https://github.com/alexjustesen/speedtest-tracker.git
synced 2026-06-23 04:20:08 +00:00
116f83a367
Co-authored-by: Alex Justesen <alexjustesen@users.noreply.github.com>
119 lines
4.0 KiB
PHP
119 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets;
|
|
|
|
use App\Enums\ResultStatus;
|
|
use App\Filament\Widgets\Concerns\HasChartFilters;
|
|
use App\Models\Result;
|
|
use Filament\Widgets\ChartWidget;
|
|
|
|
class RecentDownloadLatencyChartWidget extends ChartWidget
|
|
{
|
|
use HasChartFilters;
|
|
|
|
protected ?string $heading = null;
|
|
|
|
public function getHeading(): ?string
|
|
{
|
|
return __('general.download_latency');
|
|
}
|
|
|
|
protected int|string|array $columnSpan = 'full';
|
|
|
|
protected ?string $maxHeight = '250px';
|
|
|
|
protected ?string $pollingInterval = '60s';
|
|
|
|
public ?string $filter = null;
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->filter = $this->filter ?? config('speedtest.default_chart_range', '24h');
|
|
}
|
|
|
|
protected function getData(): array
|
|
{
|
|
$results = Result::query()
|
|
->select(['id', 'data', 'created_at'])
|
|
->where('status', '=', ResultStatus::Completed)
|
|
->when($this->filter == '24h', function ($query) {
|
|
$query->where('created_at', '>=', now()->subDay());
|
|
})
|
|
->when($this->filter == 'week', function ($query) {
|
|
$query->where('created_at', '>=', now()->subWeek());
|
|
})
|
|
->when($this->filter == 'month', function ($query) {
|
|
$query->where('created_at', '>=', now()->subMonth());
|
|
})
|
|
->orderBy('created_at')
|
|
->get();
|
|
|
|
return [
|
|
'datasets' => [
|
|
[
|
|
'label' => __('general.average_ms'),
|
|
'data' => $results->map(fn ($item) => $item->download_latency_iqm),
|
|
'borderColor' => 'rgba(16, 185, 129)',
|
|
'backgroundColor' => 'rgba(16, 185, 129, 0.1)',
|
|
'pointBackgroundColor' => 'rgba(16, 185, 129)',
|
|
'fill' => true,
|
|
'cubicInterpolationMode' => 'monotone',
|
|
'tension' => 0.4,
|
|
'pointRadius' => count($results) <= 24 ? 3 : 0,
|
|
],
|
|
[
|
|
'label' => __('general.high_ms'),
|
|
'data' => $results->map(fn ($item) => $item->download_latency_high),
|
|
'borderColor' => 'rgba(14, 165, 233)',
|
|
'backgroundColor' => 'rgba(14, 165, 233, 0.1)',
|
|
'pointBackgroundColor' => 'rgba(14, 165, 233)',
|
|
'fill' => true,
|
|
'cubicInterpolationMode' => 'monotone',
|
|
'tension' => 0.4,
|
|
'pointRadius' => count($results) <= 24 ? 3 : 0,
|
|
],
|
|
[
|
|
'label' => __('general.low_ms'),
|
|
'data' => $results->map(fn ($item) => $item->download_latency_low),
|
|
'borderColor' => 'rgba(139, 92, 246)',
|
|
'backgroundColor' => 'rgba(139, 92, 246, 0.1)',
|
|
'pointBackgroundColor' => 'rgba(139, 92, 246)',
|
|
'fill' => true,
|
|
'cubicInterpolationMode' => 'monotone',
|
|
'tension' => 0.4,
|
|
'pointRadius' => count($results) <= 24 ? 3 : 0,
|
|
],
|
|
],
|
|
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
|
|
];
|
|
}
|
|
|
|
protected function getOptions(): array
|
|
{
|
|
return [
|
|
'plugins' => [
|
|
'legend' => [
|
|
'display' => true,
|
|
],
|
|
'tooltip' => [
|
|
'enabled' => true,
|
|
'mode' => 'index',
|
|
'intersect' => false,
|
|
'position' => 'nearest',
|
|
],
|
|
],
|
|
'scales' => [
|
|
'y' => [
|
|
'beginAtZero' => config('app.chart_begin_at_zero'),
|
|
'grace' => 2,
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
protected function getType(): string
|
|
{
|
|
return 'line';
|
|
}
|
|
}
|