mirror of
https://github.com/alexjustesen/speedtest-tracker.git
synced 2026-06-23 07:30:09 +00:00
[Feature] Add Average for Download, Upload, Ping (#1837)
* Add helper and average line * Fix query --------- Co-authored-by: Alex Justesen <alexjustesen@users.noreply.github.com>
This commit is contained in:
@@ -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;
|
||||
@@ -53,12 +54,22 @@ class RecentDownloadChartWidget extends ChartWidget
|
||||
[
|
||||
'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'))),
|
||||
@@ -70,7 +81,13 @@ class RecentDownloadChartWidget extends ChartWidget
|
||||
return [
|
||||
'plugins' => [
|
||||
'legend' => [
|
||||
'display' => false,
|
||||
'display' => true,
|
||||
],
|
||||
'tooltip' => [
|
||||
'enabled' => true,
|
||||
'mode' => 'index',
|
||||
'intersect' => false,
|
||||
'position' => 'nearest',
|
||||
],
|
||||
],
|
||||
'scales' => [
|
||||
|
||||
@@ -52,30 +52,27 @@ class RecentDownloadLatencyChartWidget extends ChartWidget
|
||||
[
|
||||
'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,
|
||||
],
|
||||
@@ -87,6 +84,17 @@ class RecentDownloadLatencyChartWidget extends ChartWidget
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'plugins' => [
|
||||
'legend' => [
|
||||
'display' => true,
|
||||
],
|
||||
'tooltip' => [
|
||||
'enabled' => true,
|
||||
'mode' => 'index',
|
||||
'intersect' => false,
|
||||
'position' => 'nearest',
|
||||
],
|
||||
],
|
||||
'scales' => [
|
||||
'y' => [
|
||||
'beginAtZero' => true,
|
||||
|
||||
@@ -52,30 +52,27 @@ class RecentJitterChartWidget extends ChartWidget
|
||||
[
|
||||
'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,
|
||||
],
|
||||
@@ -87,6 +84,17 @@ class RecentJitterChartWidget extends ChartWidget
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'plugins' => [
|
||||
'legend' => [
|
||||
'display' => true,
|
||||
],
|
||||
'tooltip' => [
|
||||
'enabled' => true,
|
||||
'mode' => 'index',
|
||||
'intersect' => false,
|
||||
'position' => 'nearest',
|
||||
],
|
||||
],
|
||||
'scales' => [
|
||||
'y' => [
|
||||
'beginAtZero' => true,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Filament\Widgets;
|
||||
|
||||
use App\Enums\ResultStatus;
|
||||
use App\Helpers\Average;
|
||||
use App\Models\Result;
|
||||
use Filament\Widgets\ChartWidget;
|
||||
|
||||
@@ -50,14 +51,24 @@ class RecentPingChartWidget extends ChartWidget
|
||||
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'))),
|
||||
@@ -69,7 +80,13 @@ class RecentPingChartWidget extends ChartWidget
|
||||
return [
|
||||
'plugins' => [
|
||||
'legend' => [
|
||||
'display' => false,
|
||||
'display' => true,
|
||||
],
|
||||
'tooltip' => [
|
||||
'enabled' => true,
|
||||
'mode' => 'index',
|
||||
'intersect' => false,
|
||||
'position' => 'nearest',
|
||||
],
|
||||
],
|
||||
'scales' => [
|
||||
|
||||
@@ -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;
|
||||
@@ -53,12 +54,22 @@ class RecentUploadChartWidget extends ChartWidget
|
||||
[
|
||||
'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'))),
|
||||
@@ -70,7 +81,13 @@ class RecentUploadChartWidget extends ChartWidget
|
||||
return [
|
||||
'plugins' => [
|
||||
'legend' => [
|
||||
'display' => false,
|
||||
'display' => true,
|
||||
],
|
||||
'tooltip' => [
|
||||
'enabled' => true,
|
||||
'mode' => 'index',
|
||||
'intersect' => false,
|
||||
'position' => 'nearest',
|
||||
],
|
||||
],
|
||||
'scales' => [
|
||||
|
||||
@@ -52,30 +52,27 @@ class RecentUploadLatencyChartWidget extends ChartWidget
|
||||
[
|
||||
'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,
|
||||
],
|
||||
@@ -87,6 +84,17 @@ class RecentUploadLatencyChartWidget extends ChartWidget
|
||||
protected function getOptions(): array
|
||||
{
|
||||
return [
|
||||
'plugins' => [
|
||||
'legend' => [
|
||||
'display' => true,
|
||||
],
|
||||
'tooltip' => [
|
||||
'enabled' => true,
|
||||
'mode' => 'index',
|
||||
'intersect' => false,
|
||||
'position' => 'nearest',
|
||||
],
|
||||
],
|
||||
'scales' => [
|
||||
'y' => [
|
||||
'beginAtZero' => true,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user