Skip to content

Commit 6bb6c9d

Browse files
authored
Always format to megabits in recent speed chart (alexjustesen#268)
1 parent b7598d4 commit 6bb6c9d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

app/Filament/Widgets/RecentSpeedChart.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected function getData(): array
4949
'datasets' => [
5050
[
5151
'label' => 'Download',
52-
'data' => $results->map(fn ($item) => formatBits(formatBytesToBits($item->download), 2, false)),
52+
'data' => $results->map(fn ($item) => roundBytesToMegabits($item->download)),
5353
'borderColor' => '#0ea5e9',
5454
'backgroundColor' => '#0ea5e9',
5555
'fill' => false,
@@ -58,7 +58,7 @@ protected function getData(): array
5858
],
5959
[
6060
'label' => 'Upload',
61-
'data' => $results->map(fn ($item) => formatBits(formatBytesToBits($item->upload), 2, false)),
61+
'data' => $results->map(fn ($item) => roundBytesToMegabits($item->upload)),
6262
'borderColor' => '#8b5cf6',
6363
'backgroundColor' => '#8b5cf6',
6464
'fill' => false,

app/helpers.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ function formatBytestoBits(int $bytes)
4545
}
4646
}
4747

48+
if (! function_exists('roundBytesToMegabits')) {
49+
function roundBytesToMegabits(int $bytes)
50+
{
51+
if ($bytes > 0) {
52+
$bits = formatBytesToBits($bytes);
53+
$megabit_exponent = 2;
54+
$precision = 2;
55+
56+
return round($bits / pow(1000, $megabit_exponent), $precision);
57+
}
58+
59+
return 0;
60+
}
61+
}
62+
4863
if (! function_exists('percentChange')) {
4964
function percentChange(float $dividend, float $divisor, int $precision = 0): string
5065
{

0 commit comments

Comments
 (0)