Skip to content

Commit 0827861

Browse files
authored
[Chore] Bit number helpers (alexjustesen#1199)
1 parent 04ea197 commit 0827861

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

app/Filament/Resources/ResultResource.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public static function form(Form $form): Form
5959
Forms\Components\TextInput::make('download')
6060
->label('Download')
6161
->afterStateHydrated(function (TextInput $component, Result $record) {
62-
$component->state(! blank($record->download) ? Number::fileSizeBits(bits: $record->download_bits, precision: 2, perSecond: true) : '');
62+
$component->state(! blank($record->download) ? Number::toBitRate(bits: $record->download_bits, precision: 2) : '');
6363
}),
6464
Forms\Components\TextInput::make('upload')
6565
->label('Upload')
6666
->afterStateHydrated(function (TextInput $component, Result $record) {
67-
$component->state(! blank($record->upload) ? Number::fileSizeBits(bits: $record->upload_bits, precision: 2, perSecond: true) : '');
67+
$component->state(! blank($record->upload) ? Number::toBitRate(bits: $record->upload_bits, precision: 2) : '');
6868
}),
6969
Forms\Components\TextInput::make('ping')
7070
->label('Ping (ms)'),
@@ -132,10 +132,10 @@ public static function table(Table $table): Table
132132
->toggleable()
133133
->sortable(),
134134
Tables\Columns\TextColumn::make('download')
135-
->getStateUsing(fn (Result $record): ?string => ! blank($record->download) ? Number::fileSizeBits(bits: $record->download_bits, precision: 2, perSecond: true) : null)
135+
->getStateUsing(fn (Result $record): ?string => ! blank($record->download) ? Number::toBitRate(bits: $record->download_bits, precision: 2) : null)
136136
->sortable(),
137137
Tables\Columns\TextColumn::make('upload')
138-
->getStateUsing(fn (Result $record): ?string => ! blank($record->upload) ? Number::fileSizeBits(bits: $record->upload_bits, precision: 2, perSecond: true) : null)
138+
->getStateUsing(fn (Result $record): ?string => ! blank($record->upload) ? Number::toBitRate(bits: $record->upload_bits, precision: 2) : null)
139139
->sortable(),
140140
Tables\Columns\TextColumn::make('ping')
141141
->toggleable()

app/Filament/Widgets/StatsOverviewWidget.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ protected function getCards(): array
4545

4646
if (! $previous) {
4747
return [
48-
Stat::make('Latest download', fn (): string => ! blank($this->result) ? Number::fileSizeBits(bits: $this->result->download_bits, precision: 2, perSecond: true) : 'n/a')
48+
Stat::make('Latest download', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->download_bits, precision: 2) : 'n/a')
4949
->icon('heroicon-o-arrow-down-tray'),
50-
Stat::make('Latest upload', fn (): string => ! blank($this->result) ? Number::fileSizeBits(bits: $this->result->upload_bits, precision: 2, perSecond: true) : 'n/a')
50+
Stat::make('Latest upload', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->upload_bits, precision: 2) : 'n/a')
5151
->icon('heroicon-o-arrow-up-tray'),
5252
Stat::make('Latest ping', fn (): string => ! blank($this->result) ? number_format($this->result->ping, 2).' ms' : 'n/a')
5353
->icon('heroicon-o-clock'),
@@ -59,12 +59,12 @@ protected function getCards(): array
5959
$pingChange = percentChange($this->result->ping, $previous->ping, 2);
6060

6161
return [
62-
Stat::make('Latest download', fn (): string => ! blank($this->result) ? Number::fileSizeBits(bits: $this->result->download_bits, precision: 2, perSecond: true) : 'n/a')
62+
Stat::make('Latest download', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->download_bits, precision: 2) : 'n/a')
6363
->icon('heroicon-o-arrow-down-tray')
6464
->description($downloadChange > 0 ? $downloadChange.'% faster' : abs($downloadChange).'% slower')
6565
->descriptionIcon($downloadChange > 0 ? 'heroicon-m-arrow-trending-up' : 'heroicon-m-arrow-trending-down')
6666
->color($downloadChange > 0 ? 'success' : 'danger'),
67-
Stat::make('Latest upload', fn (): string => ! blank($this->result) ? Number::fileSizeBits(bits: $this->result->upload_bits, precision: 2, perSecond: true) : 'n/a')
67+
Stat::make('Latest upload', fn (): string => ! blank($this->result) ? Number::toBitRate(bits: $this->result->upload_bits, precision: 2) : 'n/a')
6868
->icon('heroicon-o-arrow-up-tray')
6969
->description($uploadChange > 0 ? $uploadChange.'% faster' : abs($uploadChange).'% slower')
7070
->descriptionIcon($uploadChange > 0 ? 'heroicon-m-arrow-trending-up' : 'heroicon-m-arrow-trending-down')

app/Helpers/Number.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,29 @@
77
class Number extends SupportNumber
88
{
99
/**
10-
* Convert the given number to its file size equivalent in bits.
10+
* Convert the given number to its largest bit order of magnitude.
11+
*
12+
* Reference: https://en.wikipedia.org/wiki/Bit
1113
*/
12-
public static function fileSizeBits(int|float $bits, int $precision = 0, ?int $maxPrecision = null, bool $perSecond = false): string
14+
public static function bitsToHuman(int|float $bits, int $precision = 0, ?int $maxPrecision = null): string
1315
{
14-
$units = match ($perSecond) {
15-
true => ['Bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'],
16-
default => ['B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb']
17-
};
16+
$units = ['B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb'];
17+
18+
for ($i = 0; ($bits / 1000) > 0.9 && ($i < count($units) - 1); $i++) {
19+
$bits /= 1000;
20+
}
21+
22+
return sprintf('%s %s', static::format($bits, $precision, $maxPrecision), $units[$i]);
23+
}
24+
25+
/**
26+
* Convert the given number to its largest bit rate order of magnitude.
27+
*
28+
* Reference: https://en.wikipedia.org/wiki/Bit_rate
29+
*/
30+
public static function toBitRate(int|float $bits, int $precision = 0, ?int $maxPrecision = null): string
31+
{
32+
$units = ['Bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'];
1833

1934
for ($i = 0; ($bits / 1000) > 0.9 && ($i < count($units) - 1); $i++) {
2035
$bits /= 1000;

0 commit comments

Comments
 (0)