Skip to content

Commit

Permalink
Filament admin panel (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjustesen authored Sep 27, 2022
1 parent 1ded5d2 commit a1087ac
Show file tree
Hide file tree
Showing 22 changed files with 4,794 additions and 1,377 deletions.
132 changes: 110 additions & 22 deletions .phpstorm.meta.php

Large diffs are not rendered by default.

2,407 changes: 2,090 additions & 317 deletions _ide_helper.php

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/Console/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ public function handle()

$this->checkAppKey();

$this->line('⏳ Optimizing the cache');
$this->line('⏳ Optimizing the cache...');

Artisan::call('optimize:clear');

$this->line('✅ Optimized cache');

$this->newLine();

$this->line('⏳ Migrating the database');
$this->line('⏳ Migrating the database...');

try {
Artisan::call('migrate:fresh', [
Expand Down
70 changes: 70 additions & 0 deletions app/Filament/Resources/ResultResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ResultResource\Pages;
use App\Filament\Resources\ResultResource\Widgets\StatsOverview;
use App\Models\Result;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ViewColumn;

class ResultResource extends Resource
{
protected static ?string $model = Result::class;

protected static ?string $navigationGroup = 'Data';

protected static ?string $navigationIcon = 'heroicon-o-table';

protected static ?string $navigationLabel = 'Speedtest Results';

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')
->label('ID'),
ViewColumn::make('download')
->view('tables.columns.bytes-column'),
ViewColumn::make('upload')
->view('tables.columns.bytes-column'),
TextColumn::make('ping'),
TextColumn::make('created_at')
->dateTime(),
])
->filters([
//
])
->actions([
// Tables\Actions\ViewAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListResults::route('/'),
];
}

public static function getWidgets(): array
{
return [
StatsOverview::class,
];
}
}
16 changes: 16 additions & 0 deletions app/Filament/Resources/ResultResource/Pages/ListResults.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Filament\Resources\ResultResource\Pages;

use App\Filament\Resources\ResultResource;
use Filament\Resources\Pages\ListRecords;

class ListResults extends ListRecords
{
protected static string $resource = ResultResource::class;

protected function getHeaderWidgets(): array
{
return ResultResource::getWidgets();
}
}
21 changes: 21 additions & 0 deletions app/Filament/Resources/ResultResource/Widgets/StatsOverview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Filament\Resources\ResultResource\Widgets;

use App\Models\Result;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Card;

class StatsOverview extends BaseWidget
{
protected static ?string $pollingInterval = null;

protected function getCards(): array
{
return [
Card::make('Latest download', formatBytes(Result::latest()->first()->download)),
Card::make('Latest upload', formatBytes(Result::latest()->first()->upload)),
Card::make('Latest ping', round(Result::latest()->first()->ping, 2)),
];
}
}
2 changes: 1 addition & 1 deletion app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Authenticate extends Middleware
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
return route('admin/login');
}
}
}
9 changes: 9 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class User extends Authenticatable
protected $fillable = [
'name',
'email',
'email_verified_at',
'password',
];

Expand All @@ -41,4 +42,12 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

/**
* Any user can access the Filament admin panel.
*/
public function canAccessFilament(): bool
{
return true;
}
}
94 changes: 94 additions & 0 deletions app/Policies/ResultPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Policies;

use App\Models\Result;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class ResultPolicy
{
use HandlesAuthorization;

/**
* Determine whether the user can view any models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function viewAny(User $user)
{
return true;
}

/**
* Determine whether the user can view the model.
*
* @param \App\Models\User $user
* @param \App\Models\Result $result
* @return \Illuminate\Auth\Access\Response|bool
*/
public function view(User $user, Result $result)
{
return true;
}

/**
* Determine whether the user can create models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function create(User $user)
{
//
}

/**
* Determine whether the user can update the model.
*
* @param \App\Models\User $user
* @param \App\Models\Result $result
* @return \Illuminate\Auth\Access\Response|bool
*/
public function update(User $user, Result $result)
{
//
}

/**
* Determine whether the user can delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\Result $result
* @return \Illuminate\Auth\Access\Response|bool
*/
public function delete(User $user, Result $result)
{
return true;
}

/**
* Determine whether the user can restore the model.
*
* @param \App\Models\User $user
* @param \App\Models\Result $result
* @return \Illuminate\Auth\Access\Response|bool
*/
public function restore(User $user, Result $result)
{
//
}

/**
* Determine whether the user can permanently delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\Result $result
* @return \Illuminate\Auth\Access\Response|bool
*/
public function forceDelete(User $user, Result $result)
{
//
}
}
53 changes: 53 additions & 0 deletions app/Providers/FilamentServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Providers;

use Filament\Facades\Filament;
use Filament\Navigation\NavigationItem;
use Illuminate\Support\ServiceProvider;

class FilamentServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}

/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
Filament::serving(function () {
Filament::registerNavigationGroups([
'Data',
'Links',
]);

Filament::registerNavigationItems([
NavigationItem::make('Documenation')
->url('https://docs.speedtest-tracker.dev/', shouldOpenInNewTab: true)
->icon('heroicon-o-external-link')
->group('Links')
->sort(0),
NavigationItem::make('Donate')
->url('https://github.com/sponsors/alexjustesen', shouldOpenInNewTab: true)
->icon('heroicon-o-external-link')
->group('Links')
->sort(1),
NavigationItem::make('Source Code')
->url('https://github.com/alexjustesen/speedtest-tracker', shouldOpenInNewTab: true)
->icon('heroicon-o-external-link')
->group('Links')
->sort(2),
]);
});
}
}
10 changes: 10 additions & 0 deletions app/Tables/Columns/BytesColumn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Tables\Columns;

use Filament\Tables\Columns\Column;

class BytesColumn extends Column
{
protected string $view = 'tables.columns.bytes-column';
}
11 changes: 11 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

if (! function_exists('formatBytes')) {
function formatBytes(int $bytes, $precision = 2)
{
$base = log($bytes, 1024);
$suffixes = array('', 'Kbps', 'Mbps', 'Gbps', 'Tbps');

return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)];
}
}
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"license": "MIT",
"require": {
"php": "^8.0.2",
"doctrine/dbal": "^3.4",
"dragonmantank/cron-expression": "^3.3",
"filament/filament": "^2.0",
"guzzlehttp/guzzle": "^7.2",
"influxdata/influxdb-client-php": "^2.9",
"laravel/framework": "^9.19",
Expand All @@ -25,6 +27,9 @@
"spatie/laravel-ignition": "^1.0"
},
"autoload": {
"files": [
"app/helpers.php"
],
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
Expand All @@ -43,6 +48,7 @@
],
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force",
"@php artisan filament:upgrade",
"@php artisan ide-helper:generate",
"@php artisan ide-helper:meta"
],
Expand Down
Loading

0 comments on commit a1087ac

Please sign in to comment.