Advertisement
svenvg93

LatencyChartWidgetForUrl.php

Aug 26th, 2024
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.45 KB | Source Code | 0 0
  1. <?php
  2.  
  3. namespace App\Filament\Widgets;
  4.  
  5. use App\Models\PingResult;
  6. use Filament\Widgets\ChartWidget;
  7.  
  8. class LatencyChartWidgetForUrl extends ChartWidget
  9. {
  10.     protected int|string|array $columnSpan = 'full';
  11.     protected static ?string $maxHeight = '250px';
  12.  
  13.     public ?string $url = null; // Public property for URL
  14.     public ?string $filter = '24h';
  15.  
  16.     protected function getPollingInterval(): ?string
  17.     {
  18.         return config('speedtest.dashboard_polling');
  19.     }
  20.  
  21.     protected function getFilters(): ?array
  22.     {
  23.         return [
  24.             '24h' => 'Last 24h',
  25.             'week' => 'Last week',
  26.             'month' => 'Last month',
  27.         ];
  28.     }
  29.  
  30.     protected function getData(): array
  31.     {
  32.         \Log::info("Widget URL in getData: " . ($this->url ?? 'null'));
  33.  
  34.         if (!$this->url) {
  35.             \Log::info("No URL provided for widget.");
  36.             return [];
  37.         }
  38.  
  39.         // Fetch results based on the URL and filter
  40.         $results = PingResult::query()
  41.             ->select(['id', 'avg_latency', 'packet_loss', 'created_at'])
  42.             ->where('url', $this->url)
  43.             ->when($this->filter == '24h', function ($query) {
  44.                 $query->where('created_at', '>=', now()->subDay());
  45.             })
  46.             ->when($this->filter == 'week', function ($query) {
  47.                 $query->where('created_at', '>=', now()->subWeek());
  48.             })
  49.             ->when($this->filter == 'month', function ($query) {
  50.                 $query->where('created_at', '>=', now()->subMonth());
  51.             })
  52.             ->orderBy('created_at')
  53.             ->get();
  54.  
  55.         \Log::info("Query Results: ", $results->toArray());
  56.  
  57.         if ($results->isEmpty()) {
  58.             \Log::info("No results found for URL: " . $this->url);
  59.         } else {
  60.             \Log::info("Results found: " . $results->count());
  61.         }
  62.  
  63.         return [
  64.             'datasets' => [
  65.                 [
  66.                     'label' => 'Average (ms)',
  67.                     'data' => $results->map(fn ($item) => $item->avg_latency ?? 0)->toArray(),
  68.                     'borderColor' => 'rgb(51, 181, 229)',
  69.                     'backgroundColor' => 'rgba(51, 181, 229, 0.2)',
  70.                     'pointBackgroundColor' => 'rgb(51, 181, 229)',
  71.                     'fill' => true,
  72.                     'yAxisID' => 'left-y-axis',
  73.                     'tension' => 0.4,
  74.                 ],
  75.                 [
  76.                     'label' => 'Packet Loss (%)',
  77.                     'data' => $results->map(fn ($item) => $item->packet_loss ?? 0)->toArray(),
  78.                     'borderColor' => 'rgb(255, 87, 51)',
  79.                     'backgroundColor' => 'rgba(255, 87, 51, 0.2)',
  80.                     'pointBackgroundColor' => 'rgb(255, 87, 51)',
  81.                     'fill' => true,
  82.                     'yAxisID' => 'right-y-axis',
  83.                     'tension' => 0.4,
  84.                 ],
  85.             ],
  86.             'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format')))->toArray(),
  87.             'heading' => $this->url,
  88.         ];
  89.     }
  90.  
  91.     protected function getOptions(): array
  92.     {
  93.         return [
  94.             'plugins' => [
  95.                 'legend' => [
  96.                     'display' => true,
  97.                 ],
  98.             ],
  99.             'scales' => [
  100.                 'left-y-axis' => [
  101.                     'type' => 'linear',
  102.                     'position' => 'left',
  103.                     'beginAtZero' => false,
  104.                     'title' => [
  105.                         'display' => true,
  106.                         'text' => 'Average (ms)',
  107.                     ],
  108.                     'grid' => [
  109.                         'display' => false,
  110.                         'drawBorder' => false,
  111.                     ],
  112.                 ],
  113.                 'right-y-axis' => [
  114.                     'type' => 'linear',
  115.                     'position' => 'right',
  116.                     'beginAtZero' => true,
  117.                     'title' => [
  118.                         'display' => true,
  119.                         'text' => 'Packet Loss (%)',
  120.                     ],
  121.                     'grid' => [
  122.                         'display' => false,
  123.                         'drawBorder' => false,
  124.                     ],
  125.                 ],
  126.             ],
  127.         ];
  128.     }
  129.  
  130.     protected function getType(): string
  131.     {
  132.         return 'line';
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement