Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions app/Filament/Pages/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,37 @@
use App\Filament\Widgets\RecentSpeedChart;
use App\Filament\Widgets\StatsOverview;
use App\Jobs\ExecSpeedtest;
use App\Models\Result;
use App\Settings\GeneralSettings;
use Filament\Notifications\Notification;
use Filament\Pages\Actions\Action;
use Filament\Pages\Dashboard as BasePage;

class Dashboard extends BasePage
{
public string $lastResult = 'never';

public int $resultsCount;

protected static string $view = 'filament.pages.dashboard';

public function mount()
{
$this->resultsCount = Result::count();

if ($this->resultsCount) {
$result = Result::latest()
->first();

$settings = new GeneralSettings();

$this->lastResult = $result->created_at
->timezone($settings->timezone)
->format($settings->time_format);
}

}

protected function getActions(): array
{
return [
Expand All @@ -27,8 +49,22 @@ protected function getActions(): array

public function getHeaderWidgets(): array
{
if (! $this->resultsCount) {
return [];
}

return [
StatsOverview::class,
];
}

public function getFooterWidgets(): array
{
if (! $this->resultsCount) {
return [];
}

return [
RecentSpeedChart::class,
RecentPingChart::class,
RecentJitterChart::class,
Expand Down
37 changes: 33 additions & 4 deletions app/Filament/Widgets/StatsOverview.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,43 @@ protected function getCards(): array

$settings = new GeneralSettings();

if (! $result) {
return [];
}

$previous = $result->previous();

if (! $previous) {
return [
Card::make('Latest download', fn (): string => ! blank($result) ? formatBits(formatBytesToBits($result->download)).'ps' : 'n/a')
->icon('heroicon-o-download'),
Card::make('Latest upload', fn (): string => ! blank($result) ? formatBits(formatBytesToBits($result->upload)).'ps' : 'n/a')
->icon('heroicon-o-upload'),
Card::make('Latest ping', fn (): string => ! blank($result) ? round($result->ping, 2).'ms' : 'n/a')
->icon('heroicon-o-clock'),
];
}

$downloadChange = percentChange($result->download, $previous->download, 2);
$uploadChange = percentChange($result->upload, $previous->upload, 2);
$pingChange = percentChange($result->ping, $previous->ping, 2);

return [
Card::make('Latest download', fn (): string => ! blank($result) ? formatBits(formatBytesToBits($result->download)).'ps' : 'n/a')
->description(! blank($result) ? 'Tested at: '.$result->created_at->timezone($settings->timezone)->format($settings->time_format) : 'No tests')
->icon('heroicon-o-download'),
->icon('heroicon-o-download')
->description($downloadChange > 0 ? $downloadChange.'% faster' : abs($downloadChange).'% slower')
->descriptionIcon($downloadChange > 0 ? 'heroicon-s-trending-up' : 'heroicon-s-trending-down')
->color($downloadChange > 0 ? 'success' : 'danger'),
Card::make('Latest upload', fn (): string => ! blank($result) ? formatBits(formatBytesToBits($result->upload)).'ps' : 'n/a')
->icon('heroicon-o-upload'),
->icon('heroicon-o-upload')
->description($uploadChange > 0 ? $uploadChange.'% faster' : abs($uploadChange).'% slower')
->descriptionIcon($uploadChange > 0 ? 'heroicon-s-trending-up' : 'heroicon-s-trending-down')
->color($uploadChange > 0 ? 'success' : 'danger'),
Card::make('Latest ping', fn (): string => ! blank($result) ? round($result->ping, 2).'ms' : 'n/a')
->icon('heroicon-o-clock'),
->icon('heroicon-o-clock')
->description($pingChange > 0 ? $pingChange.'% slower' : abs($pingChange).'% faster')
->descriptionIcon($pingChange > 0 ? 'heroicon-s-trending-up' : 'heroicon-s-trending-down')
->color($pingChange > 0 ? 'danger' : 'success'),
];
}
}
12 changes: 12 additions & 0 deletions app/Models/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,16 @@ public function getJitterData(): array
'ping' => $data['ping']['jitter'] ?? null,
];
}

/**
* Return the previous test result .
*
* @return self|null
*/
public function previous()
{
return static::orderBy('id', 'desc')
->where('id', '<', $this->id)
->first();
}
}
9 changes: 9 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ function formatBytestoBits(int $bytes)
}
}

if (! function_exists('percentChange')) {
function percentChange(float $dividend, float $divisor, int $precision = 0): float
{
$quotient = ($dividend - $divisor) / $divisor;

return number_format(round($quotient * 100, $precision), $precision);
}
}

if (! function_exists('absoluteDownloadThresholdFailed')) {
function absoluteDownloadThresholdFailed(float $threshold, float $download): bool
{
Expand Down
8 changes: 7 additions & 1 deletion resources/views/filament/pages/dashboard.blade.php
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
<x-filament::page />
<x-filament::page>

<div>
<p class="text-center text-sm">Last speedtest run at: <strong>{{ $lastResult }}</strong></p>
</div>

</x-filament::page>