Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Unified public dashboard with charts and results table #1870

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
22 changes: 22 additions & 0 deletions app/Helpers/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ public static function bitsToHuman(int|float $bits, int $precision = 0, ?int $ma
return sprintf('%s %s', static::format($bits, $precision, $maxPrecision), $units[$i]);
}

/**
* Calculate percentage between two numbers.
*/
public static function calculatePercentage($value, $total, int $decimals = 2, bool $formatOutput = false)
{
// Handle division by zero
if ($total == 0) {
return $formatOutput ? '0%' : 0;
}

// Calculate percentage
$percentage = ($value / $total) * 100;

// Round to specified decimal places
$percentage = round($percentage, $decimals);

// Return formatted or raw value
return $formatOutput
? SupportNumber::percentage($percentage)
: $percentage;
}

/**
* Convert the given number to its largest bit rate order of magnitude.
*
Expand Down
15 changes: 15 additions & 0 deletions app/Http/Controllers/PageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PageController extends Controller
{
public function results(Request $request)
{
// TODO: authorize viewing this page

return view('results');
}
}
183 changes: 183 additions & 0 deletions app/Livewire/ListResults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php

namespace App\Livewire;

use App\Enums\ResultStatus;
use App\Models\Result;
use Filament\Forms\Components\TagsInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Tables;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use Livewire\Component;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;

class ListResults extends Component implements HasForms, HasTable
{
use InteractsWithForms, InteractsWithTable;

public function table(Table $table): Table
{
return $table
->query(Result::query()->whereDate('created_at', '>=', now()->subDays(30)))
->poll('5s')
->defaultSort('id', 'desc')
->columns([
Tables\Columns\TextColumn::make('id')
->label('ID')
->sortable(),

Tables\Columns\TextColumn::make('data.server.id')
->label('Server ID'),

Tables\Columns\TextColumn::make('data.server.name')
->label('Server name')
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('data.interface.externalIp')
->label('External IP address')
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('download')
->numeric()
->sortable(),

Tables\Columns\TextColumn::make('upload')
->numeric()
->sortable(),

Tables\Columns\TextColumn::make('ping')
->numeric()
->sortable(),

Tables\Columns\TextColumn::make('data.download.latency.jitter')
->label('Download jitter')
->formatStateUsing(function ($state) {
return number_format((float) $state, 0, '.', '').' ms';
})
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('data.download.latency.high')
->label('Download latency high')
->formatStateUsing(function ($state) {
return number_format((float) $state, 0, '.', '').' ms';
})
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('data.download.latency.low')
->label('Download latency low')
->formatStateUsing(function ($state) {
return number_format((float) $state, 0, '.', '').' ms';
})
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('data.download.latency.iqm')
->label('Download latency iqm')
->formatStateUsing(function ($state) {
return number_format((float) $state, 0, '.', '').' ms';
})
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('data.upload.latency.jitter')
->label('Upload jitter')
->formatStateUsing(function ($state) {
return number_format((float) $state, 0, '.', '').' ms';
})
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('data.upload.latency.high')
->label('Upload latency high')
->formatStateUsing(function ($state) {
return number_format((float) $state, 0, '.', '').' ms';
})
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('data.upload.latency.low')
->label('Upload latency low')
->formatStateUsing(function ($state) {
return number_format((float) $state, 0, '.', '').' ms';
})
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('data.upload.latency.iqm')
->label('Upload latency iqm')
->formatStateUsing(function ($state) {
return number_format((float) $state, 0, '.', '').' ms';
})
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('data.ping.jitter')
->label('Ping jitter')
->formatStateUsing(function ($state) {
return number_format((float) $state, 0, '.', '').' ms';
})
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('data.packetLoss')
->formatStateUsing(function ($state) {
return number_format((float) $state, 2, '.', '').' %';
})
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('status')
->badge(),

Tables\Columns\IconColumn::make('scheduled')
->alignCenter()
->boolean()
->toggleable(isToggledHiddenByDefault: true),

Tables\Columns\TextColumn::make('created_at')
->alignEnd()
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: false),

Tables\Columns\TextColumn::make('updated_at')
->alignEnd()
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
// TODO: external IP address

TernaryFilter::make('scheduled'),

Filter::make('server_id')
->form([
TagsInput::make('server_ids'),
])
->query(function (Builder $query, array $data): Builder {
return $query
->when(
$data['server_ids'],
fn (Builder $query): Builder => $query->whereIn('data->server->id', $data['server_ids']),
);
}),

SelectFilter::make('status')
->multiple()
->options(ResultStatus::class),
])
->actions([
//
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
//
]),
]);
}

public function render(): View
{
return view('livewire.list-results');
}
}
40 changes: 40 additions & 0 deletions app/Livewire/ResultStats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Livewire;

use App\Enums\ResultStatus;
use App\Helpers\Number;
use App\Models\Result;
use Illuminate\Support\Facades\DB;
use Livewire\Component;

class ResultStats extends Component
{

public function render()
{
$metrics = Result::query()
->select([
DB::raw('AVG(download) as average_download'),
DB::raw('AVG(upload) as average_upload'),
DB::raw('AVG(ping) as average_ping'),
])
->where('status', '=', ResultStatus::Completed)
->whereDate('created_at', '>=', now()->subDays(30))
->first();

$totals = Result::select([
DB::raw('COUNT(CASE WHEN status = "completed" THEN 1 END) as completed_count'),
DB::raw('COUNT(CASE WHEN status = "failed" THEN 1 END) as failed_count'),
DB::raw('COUNT(*) as total_count')
])->first();

return view('livewire.result-stats', [
'totals' => $totals,
'avgDownload' => Number::toBitRate($metrics['average_download'], 2),
'avgUpload' => Number::toBitRate($metrics['average_upload'], 2),
'avgPing' => round($metrics['average_ping'], 2),
'successRate' => Number::calculatePercentage($totals['completed_count'], $totals['total_count'], formatOutput: true)
]);
}
}
3 changes: 3 additions & 0 deletions resources/views/livewire/list-results.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
{{ $this->table }}
</div>
51 changes: 51 additions & 0 deletions resources/views/livewire/result-stats.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<div wire:poll.5s>
<h3 class="text-base font-semibold text-gray-100">Latest</h3>


<h3 class="mt-10 text-base font-semibold text-gray-100">Last 30 days</h3>
<dl class="grid grid-cols-1 gap-5 mt-5 sm:grid-cols-2 lg:grid-cols-4">
<div class="px-4 py-5 overflow-hidden bg-gray-900 rounded-lg shadow-sm sm:p-6 ring-1 ring-white/10">
<dt class="text-sm font-medium text-gray-400 truncate">Total tests</dt>
<dd class="mt-1 text-3xl font-semibold tracking-tight text-white">{{ $totals['total_count'] }}</dd>
</div>

<div class="px-4 py-5 overflow-hidden bg-gray-900 rounded-lg shadow sm:p-6 ring-1 ring-white/10">
<dt class="text-sm font-medium text-gray-400 truncate">Completed tests</dt>
<dd class="mt-1 text-3xl font-semibold tracking-tight text-white">{{ $totals['completed_count'] }}</dd>
</div>

<div class="px-4 py-5 overflow-hidden bg-gray-900 rounded-lg shadow sm:p-6 ring-1 ring-white/10">
<dt class="text-sm font-medium text-gray-400 truncate">Failed tests</dt>
<dd class="mt-1 text-3xl font-semibold tracking-tight text-white">{{ $totals['failed_count'] }}</dd>
</div>

<div class="px-4 py-5 overflow-hidden bg-gray-900 rounded-lg shadow sm:p-6 ring-1 ring-white/10">
<dt class="text-sm font-medium text-gray-400 truncate">Success Rate</dt>
<dd class="mt-1 text-3xl font-semibold tracking-tight text-white">{{ $successRate }}</dd>
</div>

<div class="px-4 py-5 overflow-hidden bg-gray-900 rounded-lg shadow sm:p-6 ring-1 ring-white/10">
<dt class="text-sm font-medium text-gray-400 truncate">Avg. download</dt>
<dd class="flex items-baseline mt-1 gap-x-2">
<span class="text-3xl font-semibold tracking-tight text-white">{{ array_map('trim', explode(' ', $avgDownload))[0] }}</span>
<span class="text-sm text-gray-400">{{ array_map('trim', explode(' ', $avgDownload))[1] }}</span>
</dd>
</div>

<div class="px-4 py-5 overflow-hidden bg-gray-900 rounded-lg shadow sm:p-6 ring-1 ring-white/10">
<dt class="text-sm font-medium text-gray-400 truncate">Avg. upload</dt>
<dd class="flex items-baseline mt-1 gap-x-2">
<span class="text-3xl font-semibold tracking-tight text-white">{{ array_map('trim', explode(' ', $avgUpload))[0] }}</span>
<span class="text-sm text-gray-400">{{ array_map('trim', explode(' ', $avgUpload))[1] }}</span>
</dd>
</div>

<div class="px-4 py-5 overflow-hidden bg-gray-900 rounded-lg shadow sm:p-6 ring-1 ring-white/10">
<dt class="text-sm font-medium text-gray-400 truncate">Avg. ping</dt>
<dd class="flex items-baseline mt-1 gap-x-2">
<span class="text-3xl font-semibold tracking-tight text-white">{{ $avgPing }}</span>
<span class="text-sm text-gray-400">ms</span>
</dd>
</div>
</dl>
</div>
9 changes: 9 additions & 0 deletions resources/views/results.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<x-guest-layout>
<div class="py-12 mx-auto max-w-7xl">
<div class="space-y-6">
<livewire:result-stats />

<livewire:list-results />
</div>
</div>
</x-guest-layout>
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use App\Http\Controllers\HomeController;
use App\Http\Controllers\PageController;
use Illuminate\Support\Facades\Route;

/*
Expand All @@ -19,6 +20,9 @@
->name('home');
});

Route::get('/results', [PageController::class, 'results'])
->name('results');

Route::redirect('/login', '/admin/login')
->name('login');

Expand Down