Skip to content

Commit

Permalink
[Feature] Add Average for Download, Upload, Ping (#1837)
Browse files Browse the repository at this point in the history
* Add helper and average line

* Fix query

---------

Co-authored-by: Alex Justesen <[email protected]>
  • Loading branch information
svenvg93 and alexjustesen authored Nov 27, 2024
1 parent 5305b4f commit fbf271f
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 49 deletions.
25 changes: 21 additions & 4 deletions app/Filament/Widgets/RecentDownloadChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Widgets;

use App\Enums\ResultStatus;
use App\Helpers\Average;
use App\Helpers\Number;
use App\Models\Result;
use Filament\Widgets\ChartWidget;
Expand Down Expand Up @@ -53,12 +54,22 @@ protected function getData(): array
[
'label' => 'Download',
'data' => $results->map(fn ($item) => ! blank($item->download) ? Number::bitsToMagnitude(bits: $item->download_bits, precision: 2, magnitude: 'mbit') : null),
'borderColor' => '#0ea5e9',
'backgroundColor' => '#0ea5e9',
'pointBackgroundColor' => '#0ea5e9',
'borderColor' => 'rgba(14, 165, 233)',
'backgroundColor' => 'rgba(14, 165, 233, 0.1)',
'pointBackgroundColor' => 'rgba(14, 165, 233)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
[
'label' => 'Average',
'data' => array_fill(0, count($results), Average::averageDownload($results)),
'borderColor' => 'rgb(243, 7, 6, 1)',
'pointBackgroundColor' => 'rgb(243, 7, 6, 1)',
'fill' => false,
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
'borderDash' => [5, 5],
'pointRadius' => 0,
],
],
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
Expand All @@ -70,7 +81,13 @@ protected function getOptions(): array
return [
'plugins' => [
'legend' => [
'display' => false,
'display' => true,
],
'tooltip' => [
'enabled' => true,
'mode' => 'index',
'intersect' => false,
'position' => 'nearest',
],
],
'scales' => [
Expand Down
32 changes: 20 additions & 12 deletions app/Filament/Widgets/RecentDownloadLatencyChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,27 @@ protected function getData(): array
[
'label' => 'Average (ms)',
'data' => $results->map(fn ($item) => $item->download_latency_iqm),
'borderColor' => '#10b981',
'backgroundColor' => '#10b981',
'pointBackgroundColor' => '#10b981',
'fill' => false,
'borderColor' => 'rgba(16, 185, 129)',
'backgroundColor' => 'rgba(16, 185, 129, 0.1)',
'pointBackgroundColor' => 'rgba(16, 185, 129)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
[
'label' => 'High (ms)',
'data' => $results->map(fn ($item) => $item->download_latency_high),
'borderColor' => '#0ea5e9',
'backgroundColor' => '#0ea5e9',
'pointBackgroundColor' => '#0ea5e9',
'fill' => false,
'borderColor' => 'rgba(14, 165, 233)',
'backgroundColor' => 'rgba(14, 165, 233, 0.1)',
'pointBackgroundColor' => 'rgba(14, 165, 233)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
[
'label' => 'Low (ms)',
'data' => $results->map(fn ($item) => $item->download_latency_low),
'borderColor' => '#8b5cf6',
'backgroundColor' => '#8b5cf6',
'pointBackgroundColor' => '#8b5cf6',
'fill' => false,
'borderColor' => 'rgba(139, 92, 246)',
'backgroundColor' => 'rgba(139, 92, 246, 0.1)',
'pointBackgroundColor' => 'rgba(139, 92, 246)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
Expand All @@ -87,6 +84,17 @@ protected function getData(): array
protected function getOptions(): array
{
return [
'plugins' => [
'legend' => [
'display' => true,
],
'tooltip' => [
'enabled' => true,
'mode' => 'index',
'intersect' => false,
'position' => 'nearest',
],
],
'scales' => [
'y' => [
'beginAtZero' => true,
Expand Down
32 changes: 20 additions & 12 deletions app/Filament/Widgets/RecentJitterChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,27 @@ protected function getData(): array
[
'label' => 'Download (ms)',
'data' => $results->map(fn ($item) => $item->download_jitter),
'borderColor' => '#0ea5e9',
'backgroundColor' => '#0ea5e9',
'pointBackgroundColor' => '#0ea5e9',
'fill' => false,
'borderColor' => 'rgba(14, 165, 233)',
'backgroundColor' => 'rgba(14, 165, 233, 0.1)',
'pointBackgroundColor' => 'rgba(14, 165, 233)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
[
'label' => 'Upload (ms)',
'data' => $results->map(fn ($item) => $item->upload_jitter),
'borderColor' => '#8b5cf6',
'backgroundColor' => '#8b5cf6',
'pointBackgroundColor' => '#8b5cf6',
'fill' => false,
'borderColor' => 'rgba(139, 92, 246)',
'backgroundColor' => 'rgba(139, 92, 246, 0.1)',
'pointBackgroundColor' => 'rgba(139, 92, 246)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
[
'label' => 'Ping (ms)',
'data' => $results->map(fn ($item) => $item->ping_jitter),
'borderColor' => '#10b981',
'backgroundColor' => '#10b981',
'pointBackgroundColor' => '#10b981',
'fill' => false,
'borderColor' => 'rgba(16, 185, 129)',
'backgroundColor' => 'rgba(16, 185, 129, 0.1)',
'pointBackgroundColor' => 'rgba(16, 185, 129)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
Expand All @@ -87,6 +84,17 @@ protected function getData(): array
protected function getOptions(): array
{
return [
'plugins' => [
'legend' => [
'display' => true,
],
'tooltip' => [
'enabled' => true,
'mode' => 'index',
'intersect' => false,
'position' => 'nearest',
],
],
'scales' => [
'y' => [
'beginAtZero' => true,
Expand Down
27 changes: 22 additions & 5 deletions app/Filament/Widgets/RecentPingChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Widgets;

use App\Enums\ResultStatus;
use App\Helpers\Average;
use App\Models\Result;
use Filament\Widgets\ChartWidget;

Expand Down Expand Up @@ -50,14 +51,24 @@ protected function getData(): array
return [
'datasets' => [
[
'label' => 'Ping (ms)',
'label' => 'Ping',
'data' => $results->map(fn ($item) => $item->ping),
'borderColor' => '#10b981',
'backgroundColor' => '#10b981',
'pointBackgroundColor' => '#10b981',
'borderColor' => 'rgba(16, 185, 129)',
'backgroundColor' => 'rgba(16, 185, 129, 0.1)',
'pointBackgroundColor' => 'rgba(16, 185, 129)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
[
'label' => 'Average',
'data' => array_fill(0, count($results), Average::averagePing($results)),
'borderColor' => 'rgb(243, 7, 6, 1)',
'pointBackgroundColor' => 'rgb(243, 7, 6, 1)',
'fill' => false,
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
'borderDash' => [5, 5],
'pointRadius' => 0,
],
],
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
Expand All @@ -69,7 +80,13 @@ protected function getOptions(): array
return [
'plugins' => [
'legend' => [
'display' => false,
'display' => true,
],
'tooltip' => [
'enabled' => true,
'mode' => 'index',
'intersect' => false,
'position' => 'nearest',
],
],
'scales' => [
Expand Down
25 changes: 21 additions & 4 deletions app/Filament/Widgets/RecentUploadChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Widgets;

use App\Enums\ResultStatus;
use App\Helpers\Average;
use App\Helpers\Number;
use App\Models\Result;
use Filament\Widgets\ChartWidget;
Expand Down Expand Up @@ -53,12 +54,22 @@ protected function getData(): array
[
'label' => 'Upload',
'data' => $results->map(fn ($item) => ! blank($item->upload) ? Number::bitsToMagnitude(bits: $item->upload_bits, precision: 2, magnitude: 'mbit') : null),
'borderColor' => '#8b5cf6',
'backgroundColor' => '#8b5cf6',
'pointBackgroundColor' => '#8b5cf6',
'borderColor' => 'rgba(139, 92, 246)',
'backgroundColor' => 'rgba(139, 92, 246, 0.1)',
'pointBackgroundColor' => 'rgba(139, 92, 246)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
[
'label' => 'Average',
'data' => array_fill(0, count($results), Average::averageUpload($results)),
'borderColor' => 'rgb(243, 7, 6, 1)',
'pointBackgroundColor' => 'rgb(243, 7, 6, 1)',
'fill' => false,
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
'borderDash' => [5, 5],
'pointRadius' => 0,
],
],
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
Expand All @@ -70,7 +81,13 @@ protected function getOptions(): array
return [
'plugins' => [
'legend' => [
'display' => false,
'display' => true,
],
'tooltip' => [
'enabled' => true,
'mode' => 'index',
'intersect' => false,
'position' => 'nearest',
],
],
'scales' => [
Expand Down
32 changes: 20 additions & 12 deletions app/Filament/Widgets/RecentUploadLatencyChartWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,27 @@ protected function getData(): array
[
'label' => 'Average (ms)',
'data' => $results->map(fn ($item) => $item->upload_latency_iqm),
'borderColor' => '#10b981',
'backgroundColor' => '#10b981',
'pointBackgroundColor' => '#10b981',
'fill' => false,
'borderColor' => 'rgba(16, 185, 129)',
'backgroundColor' => 'rgba(16, 185, 129, 0.1)',
'pointBackgroundColor' => 'rgba(16, 185, 129)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
[
'label' => 'High (ms)',
'data' => $results->map(fn ($item) => $item->upload_latency_high),
'borderColor' => '#0ea5e9',
'backgroundColor' => '#0ea5e9',
'pointBackgroundColor' => '#0ea5e9',
'fill' => false,
'borderColor' => 'rgba(14, 165, 233)',
'backgroundColor' => 'rgba(14, 165, 233, 0.1)',
'pointBackgroundColor' => 'rgba(14, 165, 233)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
[
'label' => 'Low (ms)',
'data' => $results->map(fn ($item) => $item->upload_latency_low),
'borderColor' => '#8b5cf6',
'backgroundColor' => '#8b5cf6',
'pointBackgroundColor' => '#8b5cf6',
'fill' => false,
'borderColor' => 'rgba(139, 92, 246)',
'backgroundColor' => 'rgba(139, 92, 246, 0.1)',
'pointBackgroundColor' => 'rgba(139, 92, 246)',
'cubicInterpolationMode' => 'monotone',
'tension' => 0.4,
],
Expand All @@ -87,6 +84,17 @@ protected function getData(): array
protected function getOptions(): array
{
return [
'plugins' => [
'legend' => [
'display' => true,
],
'tooltip' => [
'enabled' => true,
'mode' => 'index',
'intersect' => false,
'position' => 'nearest',
],
],
'scales' => [
'y' => [
'beginAtZero' => true,
Expand Down
44 changes: 44 additions & 0 deletions app/Helpers/Average.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Helpers;

use Illuminate\Support\Collection;

class Average
{
/**
* Calculate the average download speed from a collection of results.
*/
public static function averageDownload(Collection $results, int $precision = 2, string $magnitude = 'mbit'): float
{
return round(
$results->map(function ($item) use ($magnitude, $precision) {
return ! blank($item->download)
? Number::bitsToMagnitude(bits: $item->download_bits, precision: $precision, magnitude: $magnitude)
: 0;
})->avg(),
$precision
);
}

public static function averageUpload(Collection $results, int $precision = 2, string $magnitude = 'mbit'): float
{
return round(
$results->map(function ($item) use ($magnitude, $precision) {
return ! blank($item->upload)
? Number::bitsToMagnitude(bits: $item->upload_bits, precision: $precision, magnitude: $magnitude)
: 0;
})->avg(),
$precision
);
}

public static function averagePing(Collection $results, int $precision = 2): float
{
$avgPing = $results->filter(function ($item) {
return ! blank($item->ping);
})->avg('ping');

return round($avgPing, $precision);
}
}

0 comments on commit fbf271f

Please sign in to comment.