Skip to content

Commit fbf271f

Browse files
[Feature] Add Average for Download, Upload, Ping (#1837)
* Add helper and average line * Fix query --------- Co-authored-by: Alex Justesen <[email protected]>
1 parent 5305b4f commit fbf271f

File tree

7 files changed

+168
-49
lines changed

7 files changed

+168
-49
lines changed

app/Filament/Widgets/RecentDownloadChartWidget.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Filament\Widgets;
44

55
use App\Enums\ResultStatus;
6+
use App\Helpers\Average;
67
use App\Helpers\Number;
78
use App\Models\Result;
89
use Filament\Widgets\ChartWidget;
@@ -53,12 +54,22 @@ protected function getData(): array
5354
[
5455
'label' => 'Download',
5556
'data' => $results->map(fn ($item) => ! blank($item->download) ? Number::bitsToMagnitude(bits: $item->download_bits, precision: 2, magnitude: 'mbit') : null),
56-
'borderColor' => '#0ea5e9',
57-
'backgroundColor' => '#0ea5e9',
58-
'pointBackgroundColor' => '#0ea5e9',
57+
'borderColor' => 'rgba(14, 165, 233)',
58+
'backgroundColor' => 'rgba(14, 165, 233, 0.1)',
59+
'pointBackgroundColor' => 'rgba(14, 165, 233)',
60+
'cubicInterpolationMode' => 'monotone',
61+
'tension' => 0.4,
62+
],
63+
[
64+
'label' => 'Average',
65+
'data' => array_fill(0, count($results), Average::averageDownload($results)),
66+
'borderColor' => 'rgb(243, 7, 6, 1)',
67+
'pointBackgroundColor' => 'rgb(243, 7, 6, 1)',
5968
'fill' => false,
6069
'cubicInterpolationMode' => 'monotone',
6170
'tension' => 0.4,
71+
'borderDash' => [5, 5],
72+
'pointRadius' => 0,
6273
],
6374
],
6475
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
@@ -70,7 +81,13 @@ protected function getOptions(): array
7081
return [
7182
'plugins' => [
7283
'legend' => [
73-
'display' => false,
84+
'display' => true,
85+
],
86+
'tooltip' => [
87+
'enabled' => true,
88+
'mode' => 'index',
89+
'intersect' => false,
90+
'position' => 'nearest',
7491
],
7592
],
7693
'scales' => [

app/Filament/Widgets/RecentDownloadLatencyChartWidget.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,27 @@ protected function getData(): array
5252
[
5353
'label' => 'Average (ms)',
5454
'data' => $results->map(fn ($item) => $item->download_latency_iqm),
55-
'borderColor' => '#10b981',
56-
'backgroundColor' => '#10b981',
57-
'pointBackgroundColor' => '#10b981',
58-
'fill' => false,
55+
'borderColor' => 'rgba(16, 185, 129)',
56+
'backgroundColor' => 'rgba(16, 185, 129, 0.1)',
57+
'pointBackgroundColor' => 'rgba(16, 185, 129)',
5958
'cubicInterpolationMode' => 'monotone',
6059
'tension' => 0.4,
6160
],
6261
[
6362
'label' => 'High (ms)',
6463
'data' => $results->map(fn ($item) => $item->download_latency_high),
65-
'borderColor' => '#0ea5e9',
66-
'backgroundColor' => '#0ea5e9',
67-
'pointBackgroundColor' => '#0ea5e9',
68-
'fill' => false,
64+
'borderColor' => 'rgba(14, 165, 233)',
65+
'backgroundColor' => 'rgba(14, 165, 233, 0.1)',
66+
'pointBackgroundColor' => 'rgba(14, 165, 233)',
6967
'cubicInterpolationMode' => 'monotone',
7068
'tension' => 0.4,
7169
],
7270
[
7371
'label' => 'Low (ms)',
7472
'data' => $results->map(fn ($item) => $item->download_latency_low),
75-
'borderColor' => '#8b5cf6',
76-
'backgroundColor' => '#8b5cf6',
77-
'pointBackgroundColor' => '#8b5cf6',
78-
'fill' => false,
73+
'borderColor' => 'rgba(139, 92, 246)',
74+
'backgroundColor' => 'rgba(139, 92, 246, 0.1)',
75+
'pointBackgroundColor' => 'rgba(139, 92, 246)',
7976
'cubicInterpolationMode' => 'monotone',
8077
'tension' => 0.4,
8178
],
@@ -87,6 +84,17 @@ protected function getData(): array
8784
protected function getOptions(): array
8885
{
8986
return [
87+
'plugins' => [
88+
'legend' => [
89+
'display' => true,
90+
],
91+
'tooltip' => [
92+
'enabled' => true,
93+
'mode' => 'index',
94+
'intersect' => false,
95+
'position' => 'nearest',
96+
],
97+
],
9098
'scales' => [
9199
'y' => [
92100
'beginAtZero' => true,

app/Filament/Widgets/RecentJitterChartWidget.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,27 @@ protected function getData(): array
5252
[
5353
'label' => 'Download (ms)',
5454
'data' => $results->map(fn ($item) => $item->download_jitter),
55-
'borderColor' => '#0ea5e9',
56-
'backgroundColor' => '#0ea5e9',
57-
'pointBackgroundColor' => '#0ea5e9',
58-
'fill' => false,
55+
'borderColor' => 'rgba(14, 165, 233)',
56+
'backgroundColor' => 'rgba(14, 165, 233, 0.1)',
57+
'pointBackgroundColor' => 'rgba(14, 165, 233)',
5958
'cubicInterpolationMode' => 'monotone',
6059
'tension' => 0.4,
6160
],
6261
[
6362
'label' => 'Upload (ms)',
6463
'data' => $results->map(fn ($item) => $item->upload_jitter),
65-
'borderColor' => '#8b5cf6',
66-
'backgroundColor' => '#8b5cf6',
67-
'pointBackgroundColor' => '#8b5cf6',
68-
'fill' => false,
64+
'borderColor' => 'rgba(139, 92, 246)',
65+
'backgroundColor' => 'rgba(139, 92, 246, 0.1)',
66+
'pointBackgroundColor' => 'rgba(139, 92, 246)',
6967
'cubicInterpolationMode' => 'monotone',
7068
'tension' => 0.4,
7169
],
7270
[
7371
'label' => 'Ping (ms)',
7472
'data' => $results->map(fn ($item) => $item->ping_jitter),
75-
'borderColor' => '#10b981',
76-
'backgroundColor' => '#10b981',
77-
'pointBackgroundColor' => '#10b981',
78-
'fill' => false,
73+
'borderColor' => 'rgba(16, 185, 129)',
74+
'backgroundColor' => 'rgba(16, 185, 129, 0.1)',
75+
'pointBackgroundColor' => 'rgba(16, 185, 129)',
7976
'cubicInterpolationMode' => 'monotone',
8077
'tension' => 0.4,
8178
],
@@ -87,6 +84,17 @@ protected function getData(): array
8784
protected function getOptions(): array
8885
{
8986
return [
87+
'plugins' => [
88+
'legend' => [
89+
'display' => true,
90+
],
91+
'tooltip' => [
92+
'enabled' => true,
93+
'mode' => 'index',
94+
'intersect' => false,
95+
'position' => 'nearest',
96+
],
97+
],
9098
'scales' => [
9199
'y' => [
92100
'beginAtZero' => true,

app/Filament/Widgets/RecentPingChartWidget.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Filament\Widgets;
44

55
use App\Enums\ResultStatus;
6+
use App\Helpers\Average;
67
use App\Models\Result;
78
use Filament\Widgets\ChartWidget;
89

@@ -50,14 +51,24 @@ protected function getData(): array
5051
return [
5152
'datasets' => [
5253
[
53-
'label' => 'Ping (ms)',
54+
'label' => 'Ping',
5455
'data' => $results->map(fn ($item) => $item->ping),
55-
'borderColor' => '#10b981',
56-
'backgroundColor' => '#10b981',
57-
'pointBackgroundColor' => '#10b981',
56+
'borderColor' => 'rgba(16, 185, 129)',
57+
'backgroundColor' => 'rgba(16, 185, 129, 0.1)',
58+
'pointBackgroundColor' => 'rgba(16, 185, 129)',
59+
'cubicInterpolationMode' => 'monotone',
60+
'tension' => 0.4,
61+
],
62+
[
63+
'label' => 'Average',
64+
'data' => array_fill(0, count($results), Average::averagePing($results)),
65+
'borderColor' => 'rgb(243, 7, 6, 1)',
66+
'pointBackgroundColor' => 'rgb(243, 7, 6, 1)',
5867
'fill' => false,
5968
'cubicInterpolationMode' => 'monotone',
6069
'tension' => 0.4,
70+
'borderDash' => [5, 5],
71+
'pointRadius' => 0,
6172
],
6273
],
6374
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
@@ -69,7 +80,13 @@ protected function getOptions(): array
6980
return [
7081
'plugins' => [
7182
'legend' => [
72-
'display' => false,
83+
'display' => true,
84+
],
85+
'tooltip' => [
86+
'enabled' => true,
87+
'mode' => 'index',
88+
'intersect' => false,
89+
'position' => 'nearest',
7390
],
7491
],
7592
'scales' => [

app/Filament/Widgets/RecentUploadChartWidget.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Filament\Widgets;
44

55
use App\Enums\ResultStatus;
6+
use App\Helpers\Average;
67
use App\Helpers\Number;
78
use App\Models\Result;
89
use Filament\Widgets\ChartWidget;
@@ -53,12 +54,22 @@ protected function getData(): array
5354
[
5455
'label' => 'Upload',
5556
'data' => $results->map(fn ($item) => ! blank($item->upload) ? Number::bitsToMagnitude(bits: $item->upload_bits, precision: 2, magnitude: 'mbit') : null),
56-
'borderColor' => '#8b5cf6',
57-
'backgroundColor' => '#8b5cf6',
58-
'pointBackgroundColor' => '#8b5cf6',
57+
'borderColor' => 'rgba(139, 92, 246)',
58+
'backgroundColor' => 'rgba(139, 92, 246, 0.1)',
59+
'pointBackgroundColor' => 'rgba(139, 92, 246)',
60+
'cubicInterpolationMode' => 'monotone',
61+
'tension' => 0.4,
62+
],
63+
[
64+
'label' => 'Average',
65+
'data' => array_fill(0, count($results), Average::averageUpload($results)),
66+
'borderColor' => 'rgb(243, 7, 6, 1)',
67+
'pointBackgroundColor' => 'rgb(243, 7, 6, 1)',
5968
'fill' => false,
6069
'cubicInterpolationMode' => 'monotone',
6170
'tension' => 0.4,
71+
'borderDash' => [5, 5],
72+
'pointRadius' => 0,
6273
],
6374
],
6475
'labels' => $results->map(fn ($item) => $item->created_at->timezone(config('app.display_timezone'))->format(config('app.chart_datetime_format'))),
@@ -70,7 +81,13 @@ protected function getOptions(): array
7081
return [
7182
'plugins' => [
7283
'legend' => [
73-
'display' => false,
84+
'display' => true,
85+
],
86+
'tooltip' => [
87+
'enabled' => true,
88+
'mode' => 'index',
89+
'intersect' => false,
90+
'position' => 'nearest',
7491
],
7592
],
7693
'scales' => [

app/Filament/Widgets/RecentUploadLatencyChartWidget.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,27 @@ protected function getData(): array
5252
[
5353
'label' => 'Average (ms)',
5454
'data' => $results->map(fn ($item) => $item->upload_latency_iqm),
55-
'borderColor' => '#10b981',
56-
'backgroundColor' => '#10b981',
57-
'pointBackgroundColor' => '#10b981',
58-
'fill' => false,
55+
'borderColor' => 'rgba(16, 185, 129)',
56+
'backgroundColor' => 'rgba(16, 185, 129, 0.1)',
57+
'pointBackgroundColor' => 'rgba(16, 185, 129)',
5958
'cubicInterpolationMode' => 'monotone',
6059
'tension' => 0.4,
6160
],
6261
[
6362
'label' => 'High (ms)',
6463
'data' => $results->map(fn ($item) => $item->upload_latency_high),
65-
'borderColor' => '#0ea5e9',
66-
'backgroundColor' => '#0ea5e9',
67-
'pointBackgroundColor' => '#0ea5e9',
68-
'fill' => false,
64+
'borderColor' => 'rgba(14, 165, 233)',
65+
'backgroundColor' => 'rgba(14, 165, 233, 0.1)',
66+
'pointBackgroundColor' => 'rgba(14, 165, 233)',
6967
'cubicInterpolationMode' => 'monotone',
7068
'tension' => 0.4,
7169
],
7270
[
7371
'label' => 'Low (ms)',
7472
'data' => $results->map(fn ($item) => $item->upload_latency_low),
75-
'borderColor' => '#8b5cf6',
76-
'backgroundColor' => '#8b5cf6',
77-
'pointBackgroundColor' => '#8b5cf6',
78-
'fill' => false,
73+
'borderColor' => 'rgba(139, 92, 246)',
74+
'backgroundColor' => 'rgba(139, 92, 246, 0.1)',
75+
'pointBackgroundColor' => 'rgba(139, 92, 246)',
7976
'cubicInterpolationMode' => 'monotone',
8077
'tension' => 0.4,
8178
],
@@ -87,6 +84,17 @@ protected function getData(): array
8784
protected function getOptions(): array
8885
{
8986
return [
87+
'plugins' => [
88+
'legend' => [
89+
'display' => true,
90+
],
91+
'tooltip' => [
92+
'enabled' => true,
93+
'mode' => 'index',
94+
'intersect' => false,
95+
'position' => 'nearest',
96+
],
97+
],
9098
'scales' => [
9199
'y' => [
92100
'beginAtZero' => true,

app/Helpers/Average.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Helpers;
4+
5+
use Illuminate\Support\Collection;
6+
7+
class Average
8+
{
9+
/**
10+
* Calculate the average download speed from a collection of results.
11+
*/
12+
public static function averageDownload(Collection $results, int $precision = 2, string $magnitude = 'mbit'): float
13+
{
14+
return round(
15+
$results->map(function ($item) use ($magnitude, $precision) {
16+
return ! blank($item->download)
17+
? Number::bitsToMagnitude(bits: $item->download_bits, precision: $precision, magnitude: $magnitude)
18+
: 0;
19+
})->avg(),
20+
$precision
21+
);
22+
}
23+
24+
public static function averageUpload(Collection $results, int $precision = 2, string $magnitude = 'mbit'): float
25+
{
26+
return round(
27+
$results->map(function ($item) use ($magnitude, $precision) {
28+
return ! blank($item->upload)
29+
? Number::bitsToMagnitude(bits: $item->upload_bits, precision: $precision, magnitude: $magnitude)
30+
: 0;
31+
})->avg(),
32+
$precision
33+
);
34+
}
35+
36+
public static function averagePing(Collection $results, int $precision = 2): float
37+
{
38+
$avgPing = $results->filter(function ($item) {
39+
return ! blank($item->ping);
40+
})->avg('ping');
41+
42+
return round($avgPing, $precision);
43+
}
44+
}

0 commit comments

Comments
 (0)