Skip to content

Commit 6f9a51f

Browse files
authored
[Feature] Add Ping and server data to results (alexjustesen#2195)
1 parent 44e67e3 commit 6f9a51f

File tree

3 files changed

+197
-114
lines changed

3 files changed

+197
-114
lines changed

app/Filament/Exports/ResultExporter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ public static function getColumns(): array
8383
return $record->ping_jitter;
8484
})
8585
->enabledByDefault(false),
86+
ExportColumn::make('ping_low')
87+
->state(function (Result $record): ?string {
88+
return $record->ping_low;
89+
})
90+
->enabledByDefault(false),
91+
ExportColumn::make('ping_high')
92+
->state(function (Result $record): ?string {
93+
return $record->ping_high;
94+
})
95+
->enabledByDefault(false),
8696
ExportColumn::make('upload_latency_high')
8797
->state(function (Result $record): ?string {
8898
return $record->upload_latency_high;

app/Filament/Resources/ResultResource.php

Lines changed: 117 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -29,125 +29,108 @@ class ResultResource extends Resource
2929

3030
public static function form(Form $form): Form
3131
{
32-
return $form
33-
->schema([
34-
Forms\Components\Grid::make([
35-
'default' => 2,
36-
'md' => 3,
37-
])->schema([
38-
Forms\Components\Grid::make([
39-
'default' => 2,
40-
'md' => 3,
41-
])
32+
return $form->schema([
33+
Forms\Components\Grid::make(['default' => 2, 'md' => 3])->schema([
34+
Forms\Components\Grid::make()->schema([
35+
Forms\Components\Section::make('Result Overview')->schema([
36+
Forms\Components\TextInput::make('id')
37+
->label('ID'),
38+
Forms\Components\TextInput::make('created_at')
39+
->label('Created')
40+
->afterStateHydrated(function (TextInput $component, $state) {
41+
$component->state(Carbon::parse($state)
42+
->timezone(config('app.display_timezone'))
43+
->format(config('app.datetime_format')));
44+
}),
45+
Forms\Components\TextInput::make('download')
46+
->label('Download')
47+
->afterStateHydrated(fn ($component, Result $record) => $component->state(! blank($record->download) ? Number::toBitRate(bits: $record->download_bits, precision: 2) : '')),
48+
Forms\Components\TextInput::make('upload')
49+
->label('Upload')
50+
->afterStateHydrated(fn ($component, Result $record) => $component->state(! blank($record->upload) ? Number::toBitRate(bits: $record->upload_bits, precision: 2) : '')),
51+
Forms\Components\TextInput::make('ping')
52+
->label('Ping')
53+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
54+
Forms\Components\TextInput::make('data.packetLoss')
55+
->label('Packet Loss')
56+
->formatStateUsing(fn ($state) => number_format((float) $state, 2, '.', '').' %'),
57+
])->columns(2),
58+
59+
Forms\Components\Section::make('Download Latency')
4260
->schema([
43-
Forms\Components\TextInput::make('id')
44-
->label('ID'),
45-
Forms\Components\TextInput::make('created_at')
46-
->label('Created')
47-
->afterStateHydrated(function (TextInput $component, $state) {
48-
$component->state(Carbon::parse($state)->timezone(config('app.display_timezone'))->format(config('app.datetime_format')));
49-
})
50-
->columnSpan(2),
51-
Forms\Components\TextInput::make('download')
52-
->label('Download')
53-
->afterStateHydrated(function (TextInput $component, Result $record) {
54-
$component->state(! blank($record->download) ? Number::toBitRate(bits: $record->download_bits, precision: 2) : '');
55-
}),
56-
Forms\Components\TextInput::make('upload')
57-
->label('Upload')
58-
->afterStateHydrated(function (TextInput $component, Result $record) {
59-
$component->state(! blank($record->upload) ? Number::toBitRate(bits: $record->upload_bits, precision: 2) : '');
60-
}),
61-
Forms\Components\TextInput::make('ping')
62-
->label('Ping')
63-
->formatStateUsing(function ($state) {
64-
return number_format((float) $state, 0, '.', '').' ms';
65-
}),
66-
Forms\Components\TextInput::make('data.download.latency.jitter')
67-
->label('Download Jitter (ms)')
68-
->formatStateUsing(function ($state) {
69-
return number_format((float) $state, 0, '.', '').' ms';
70-
}),
71-
Forms\Components\TextInput::make('data.download.latency.high')
72-
->label('Download Latency High')
73-
->formatStateUsing(function ($state) {
74-
return number_format((float) $state, 0, '.', '').' ms';
75-
}),
76-
Forms\Components\TextInput::make('data.download.latency.low')
77-
->label('Download Latency low')
78-
->formatStateUsing(function ($state) {
79-
return number_format((float) $state, 0, '.', '').' ms';
80-
}),
81-
Forms\Components\TextInput::make('data.download.latency.iqm')
82-
->label('Download Latency iqm')
83-
->formatStateUsing(function ($state) {
84-
return number_format((float) $state, 0, '.', '').' ms';
85-
}),
86-
Forms\Components\TextInput::make('data.upload.latency.jitter')
87-
->label('Upload Jitter')
88-
->formatStateUsing(function ($state) {
89-
return number_format((float) $state, 0, '.', '').' ms';
90-
}),
91-
Forms\Components\TextInput::make('data.upload.latency.high')
92-
->label('Upload Latency High')
93-
->formatStateUsing(function ($state) {
94-
return number_format((float) $state, 0, '.', '').' ms';
95-
}),
96-
Forms\Components\TextInput::make('data.upload.latency.low')
97-
->label('Upload Latency low')
98-
->formatStateUsing(function ($state) {
99-
return number_format((float) $state, 0, '.', '').' ms';
100-
}),
101-
Forms\Components\TextInput::make('data.upload.latency.iqm')
102-
->label('Upload Latency iqm')
103-
->formatStateUsing(function ($state) {
104-
return number_format((float) $state, 0, '.', '').' ms';
105-
}),
106-
Forms\Components\TextInput::make('data.ping.jitter')
107-
->label('Ping Jitter')
108-
->formatStateUsing(function ($state) {
109-
return number_format((float) $state, 0, '.', '').' ms';
110-
}),
111-
Forms\Components\TextInput::make('data.packetLoss')
112-
->label('Packet Loss')
113-
->formatStateUsing(function ($state) {
114-
return number_format((float) $state, 2, '.', '').' %';
115-
}),
116-
Forms\Components\Textarea::make('data.message')
117-
->label('Message')
118-
->hint(new HtmlString('&#x1f517;<a href="https://docs.speedtest-tracker.dev/help/error-messages" target="_blank" rel="nofollow">Error Messages</a>'))
119-
->columnSpanFull(),
61+
Forms\Components\TextInput::make('data.download.latency.jitter')->label('Jitter')
62+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
63+
Forms\Components\TextInput::make('data.download.latency.high')->label('High')
64+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
65+
Forms\Components\TextInput::make('data.download.latency.low')->label('Low')
66+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
67+
Forms\Components\TextInput::make('data.download.latency.iqm')->label('IQM')
68+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
12069
])
121-
->columnSpan(2),
122-
Forms\Components\Section::make()
70+
->columns(2)
71+
->collapsed(),
72+
73+
Forms\Components\Section::make('Upload Latency')
12374
->schema([
124-
Forms\Components\Placeholder::make('service')
125-
->content(fn (Result $result): string => $result->service->getLabel()),
126-
Forms\Components\Placeholder::make('server_name')
127-
->content(fn (Result $result): ?string => $result->server_name),
128-
Forms\Components\Placeholder::make('server_id')
129-
->label('Server ID')
130-
->content(fn (Result $result): ?string => $result->server_id),
131-
Forms\Components\Placeholder::make('isp')
132-
->label('ISP')
133-
->content(fn (Result $result): ?string => $result->isp),
134-
Forms\Components\Placeholder::make('server_location')
135-
->label('Server Location')
136-
->content(fn (Result $result): ?string => $result->server_location),
137-
Forms\Components\Placeholder::make('server_host')
138-
->content(fn (Result $result): ?string => $result->server_host),
139-
Forms\Components\Placeholder::make('comment')
140-
->content(fn (Result $result): ?string => $result->comments),
141-
Forms\Components\Checkbox::make('scheduled'),
142-
Forms\Components\Checkbox::make('healthy'),
75+
Forms\Components\TextInput::make('data.upload.latency.jitter')->label('Jitter')
76+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
77+
Forms\Components\TextInput::make('data.upload.latency.high')->label('High')
78+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
79+
Forms\Components\TextInput::make('data.upload.latency.low')->label('Low')
80+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
81+
Forms\Components\TextInput::make('data.upload.latency.iqm')->label('IQM')
82+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
14383
])
144-
->columns(1)
145-
->columnSpan([
146-
'default' => 2,
147-
'md' => 1,
148-
]),
84+
->columns(2)
85+
->collapsed(),
86+
87+
Forms\Components\Section::make('Ping Details')
88+
->schema([
89+
Forms\Components\TextInput::make('data.ping.jitter')->label('Jitter')
90+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
91+
Forms\Components\TextInput::make('data.ping.low')->label('Low')
92+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
93+
Forms\Components\TextInput::make('data.ping.high')->label('High')
94+
->formatStateUsing(fn ($state) => number_format((float) $state, 0, '.', '').' ms'),
95+
])
96+
->columns(2)
97+
->collapsed(),
98+
99+
Forms\Components\Textarea::make('data.message')
100+
->label('Message')
101+
->hint(new HtmlString('&#x1f517;<a href="https://docs.speedtest-tracker.dev/help/error-messages" target="_blank" rel="nofollow">Error Messages</a>'))
102+
->columnSpanFull(),
103+
])->columnSpan([
104+
'default' => 2,
105+
'md' => 2,
106+
]),
107+
108+
Forms\Components\Section::make('Server & Metadata')->schema([
109+
Forms\Components\Placeholder::make('service')
110+
->content(fn (Result $result): string => $result->service->getLabel()),
111+
Forms\Components\Placeholder::make('server_name')
112+
->content(fn (Result $result): ?string => $result->server_name),
113+
Forms\Components\Placeholder::make('server_id')
114+
->label('Server ID')
115+
->content(fn (Result $result): ?string => $result->server_id),
116+
Forms\Components\Placeholder::make('isp')
117+
->label('ISP')
118+
->content(fn (Result $result): ?string => $result->isp),
119+
Forms\Components\Placeholder::make('server_location')
120+
->label('Server Location')
121+
->content(fn (Result $result): ?string => $result->server_location),
122+
Forms\Components\Placeholder::make('server_host')
123+
->content(fn (Result $result): ?string => $result->server_host),
124+
Forms\Components\Placeholder::make('comment')
125+
->content(fn (Result $result): ?string => $result->comments),
126+
Forms\Components\Checkbox::make('scheduled'),
127+
Forms\Components\Checkbox::make('healthy'),
128+
])->columns(1)->columnSpan([
129+
'default' => 2,
130+
'md' => 1,
149131
]),
150-
]);
132+
]),
133+
]);
151134
}
152135

153136
public static function table(Table $table): Table
@@ -295,6 +278,26 @@ public static function table(Table $table): Table
295278
->formatStateUsing(function ($state) {
296279
return number_format((float) $state, 0, '.', '').' ms';
297280
}),
281+
Tables\Columns\TextColumn::make('data.ping.low')
282+
->label('Ping low')
283+
->toggleable()
284+
->toggledHiddenByDefault()
285+
->sortable(query: function (Builder $query, string $direction): Builder {
286+
return $query->orderBy('data->ping->low', $direction);
287+
})
288+
->formatStateUsing(function ($state) {
289+
return number_format((float) $state, 0, '.', '').' ms';
290+
}),
291+
Tables\Columns\TextColumn::make('data.ping.high')
292+
->label('Ping high')
293+
->toggleable()
294+
->toggledHiddenByDefault()
295+
->sortable(query: function (Builder $query, string $direction): Builder {
296+
return $query->orderBy('data->ping->high', $direction);
297+
})
298+
->formatStateUsing(function ($state) {
299+
return number_format((float) $state, 0, '.', '').' ms';
300+
}),
298301
Tables\Columns\TextColumn::make('packet_loss')
299302
->toggleable()
300303
->toggledHiddenByDefault()

app/Models/Traits/ResultDataAttributes.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,74 @@ protected function uploadlatencyiqm(): Attribute
207207
get: fn () => Arr::get($this->data, 'upload.latency.iqm'),
208208
);
209209
}
210+
211+
/**
212+
* Get the result's ping low latency in milliseconds.
213+
*/
214+
protected function pingLow(): Attribute
215+
{
216+
return Attribute::make(
217+
get: fn () => Arr::get($this->data, 'ping.low'),
218+
);
219+
}
220+
221+
/**
222+
* Get the result's ping high latency in milliseconds.
223+
*/
224+
protected function pingHigh(): Attribute
225+
{
226+
return Attribute::make(
227+
get: fn () => Arr::get($this->data, 'ping.high'),
228+
);
229+
}
230+
231+
/**
232+
* Get the result's download elapsed time in milliseconds.
233+
*/
234+
protected function downloadElapsed(): Attribute
235+
{
236+
return Attribute::make(
237+
get: fn () => Arr::get($this->data, 'download.elapsed'),
238+
);
239+
}
240+
241+
/**
242+
* Get the result's upload elapsed time in milliseconds.
243+
*/
244+
protected function uploadElapsed(): Attribute
245+
{
246+
return Attribute::make(
247+
get: fn () => Arr::get($this->data, 'upload.elapsed'),
248+
);
249+
}
250+
251+
/**
252+
* Get the result's server port.
253+
*/
254+
protected function serverPort(): Attribute
255+
{
256+
return Attribute::make(
257+
get: fn () => Arr::get($this->data, 'server.port'),
258+
);
259+
}
260+
261+
/**
262+
* Get the result's server IP address.
263+
*/
264+
protected function serverIp(): Attribute
265+
{
266+
return Attribute::make(
267+
get: fn () => Arr::get($this->data, 'server.ip'),
268+
);
269+
}
270+
271+
/**
272+
* Get the result's server country.
273+
*/
274+
protected function serverCountry(): Attribute
275+
{
276+
return Attribute::make(
277+
get: fn () => Arr::get($this->data, 'server.country'),
278+
);
279+
}
210280
}

0 commit comments

Comments
 (0)