2.x - firewall/metrics/services/cronjobs

This commit is contained in:
Saeed Vaziry
2024-10-01 19:09:38 +02:00
parent 2e9620409b
commit 906ddc38de
58 changed files with 1625 additions and 631 deletions

View File

@ -0,0 +1,39 @@
<?php
namespace App\Web\Pages\Servers\Metrics;
use App\Models\Metric;
use App\Models\Server;
use App\Web\Components\Page;
use App\Web\Traits\PageHasServer;
class Index extends Page
{
use PageHasServer;
protected static ?string $slug = 'servers/{server}/metrics';
protected static bool $shouldRegisterNavigation = false;
protected static ?string $title = 'Metrics';
public Server $server;
public static function canAccess(): bool
{
return auth()->user()?->can('viewAny', [Metric::class, static::getServerFromRoute()]) ?? false;
}
public function getWidgets(): array
{
return [
[Widgets\FilterForm::class, ['server' => $this->server]],
[Widgets\MetricDetails::class, ['server' => $this->server]],
];
}
protected function getHeaderActions(): array
{
return [];
}
}

View File

@ -0,0 +1,86 @@
<?php
namespace App\Web\Pages\Servers\Metrics\Widgets;
use App\Actions\Monitoring\GetMetrics;
use App\Models\Server;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\ViewField;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Widgets\Widget;
class FilterForm extends Widget implements HasForms
{
use InteractsWithForms;
protected static string $view = 'web.components.form';
public ?array $data = [
'period' => '1h',
'from' => null,
'to' => null,
];
public function updated($name, $value): void
{
if ($value !== 'custom') {
$this->dispatch('updateFilters', filters: $this->data);
}
if ($value === 'custom' && $this->data['from'] && $this->data['to']) {
$this->dispatch('updateFilters', filters: $this->data);
}
}
public Server $server;
public function form(Form $form): Form
{
return $form
->schema([
Grid::make()
->columns(3)
->schema([
Select::make('period')
->live()
->reactive()
->options([
'10m' => '10 Minutes',
'30m' => '30 Minutes',
'1h' => '1 Hour',
'12h' => '12 Hours',
'1d' => '1 Day',
'7d' => '7 Days',
'custom' => 'Custom',
])
->rules(fn (Get $get) => GetMetrics::rules($get())['period']),
DatePicker::make('from')
->reactive()
->visible(fn (Get $get) => $get('period') === 'custom')
->maxDate(fn (Get $get) => now())
->rules(fn (Get $get) => GetMetrics::rules($get())['from']),
DatePicker::make('to')
->reactive()
->visible(fn (Get $get) => $get('period') === 'custom')
->minDate(fn (Get $get) => $get('from') ?: now())
->maxDate(now())
->rules(fn (Get $get) => GetMetrics::rules($get())['to']),
]),
ViewField::make('data')
->reactive()
->view('web.components.dynamic-widget', [
'widget' => Metrics::class,
'params' => [
'server' => $this->server,
'filters' => $this->data,
],
]),
])
->statePath('data');
}
}

View File

@ -0,0 +1,83 @@
<?php
namespace App\Web\Pages\Servers\Metrics\Widgets;
use App\Models\Metric;
use App\Models\Server;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Infolists\Components\Grid;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Concerns\InteractsWithInfolists;
use Filament\Infolists\Contracts\HasInfolists;
use Filament\Infolists\Infolist;
use Filament\Widgets\Widget;
use Illuminate\Support\Number;
class MetricDetails extends Widget implements HasForms, HasInfolists
{
use InteractsWithForms;
use InteractsWithInfolists;
protected $listeners = ['$refresh'];
protected static bool $isLazy = false;
protected static string $view = 'web.components.infolist';
public Server $server;
public function infolist(Infolist $infolist): Infolist
{
return $infolist
->record($this->server->metrics()->latest()->first())
->schema([
Grid::make()
->schema([
Section::make()
->heading('Memory')
->description('More details on memory')
->columnSpan(1)
->schema([
TextEntry::make('memory_total')
->label('Total Memory')
->alignRight()
->formatStateUsing(fn (Metric $record) => Number::fileSize($record->memory_total_in_bytes, 2))
->inlineLabel(),
TextEntry::make('memory_used')
->label('Used Memory')
->alignRight()
->formatStateUsing(fn (Metric $record) => Number::fileSize($record->memory_used_in_bytes, 2))
->inlineLabel(),
TextEntry::make('memory_free')
->label('Free Memory')
->formatStateUsing(fn (Metric $record) => Number::fileSize($record->memory_free_in_bytes, 2))
->alignRight()
->inlineLabel(),
]),
Section::make()
->heading('Disk')
->description('More details on disk')
->columnSpan(1)
->schema([
TextEntry::make('disk_total')
->label('Total Disk')
->formatStateUsing(fn (Metric $record) => Number::fileSize($record->disk_total_in_bytes, 2))
->alignRight()
->inlineLabel(),
TextEntry::make('disk_used')
->label('Used Disk')
->formatStateUsing(fn (Metric $record) => Number::fileSize($record->disk_used_in_bytes, 2))
->alignRight()
->inlineLabel(),
TextEntry::make('disk_free')
->label('Free Disk')
->formatStateUsing(fn (Metric $record) => Number::fileSize($record->disk_free_in_bytes, 2))
->alignRight()
->inlineLabel(),
]),
]),
]);
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Web\Pages\Servers\Metrics\Widgets;
use App\Actions\Monitoring\GetMetrics;
use App\Models\Metric;
use App\Models\Server;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
use Filament\Widgets\StatsOverviewWidget\Stat;
use Illuminate\Support\Number;
use Livewire\Attributes\On;
class Metrics extends BaseWidget
{
public Server $server;
public array $filters = [];
protected static bool $isLazy = false;
#[On('updateFilters')]
public function updateFilters(array $filters): void
{
$this->filters = $filters;
}
protected function getStats(): array
{
/** @var Metric $lastMetric */
$lastMetric = $this->server
->metrics()
->latest()
->first();
$metrics = app(GetMetrics::class)->filter($this->server, $this->filters);
return [
Stat::make('CPU Load', $lastMetric?->load ?? 0)
->color('success')
->chart($metrics->pluck('load')->toArray()),
Stat::make('Memory Usage', Number::fileSize($lastMetric->memory_used_in_bytes, 2))
->color('warning')
->chart($metrics->pluck('memory_used')->toArray()),
Stat::make('Disk Usage', Number::fileSize($lastMetric->disk_used_in_bytes, 2))
->color('primary')
->chart($metrics->pluck('disk_used')->toArray()),
];
}
}