Add basic widgets for now

This commit is contained in:
Lance Pioch 2024-05-11 15:25:37 -04:00
parent f58c697d28
commit 585fe8d1a1
4 changed files with 165 additions and 0 deletions

View File

@ -3,6 +3,8 @@
namespace App\Filament\Resources\NodeResource\Pages;
use App\Filament\Resources\NodeResource;
use App\Filament\Resources\NodeResource\Widgets\NodeMemoryChart;
use App\Filament\Resources\NodeResource\Widgets\NodeStorageChart;
use App\Models\Node;
use Filament\Actions;
use Filament\Forms;
@ -78,4 +80,12 @@ class EditNode extends EditRecord
->label(fn (Node $node) => $node->servers()->count() > 0 ? 'Node Has Servers' : 'Delete'),
];
}
protected function getFooterWidgets(): array
{
return [
NodeStorageChart::class,
NodeMemoryChart::class,
];
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace App\Filament\Resources\NodeResource\Widgets;
use App\Models\Node;
use Filament\Widgets\ChartWidget;
use Illuminate\Database\Eloquent\Model;
class NodeMemoryChart extends ChartWidget
{
protected static ?string $heading = 'Memory';
protected static ?string $pollingInterval = '60s';
public ?Model $record = null;
protected static ?array $options = [
'scales' => [
'x' => [
'grid' => [
'display' => false,
],
'ticks' => [
'display' => false,
],
],
'y' => [
'grid' => [
'display' => false,
],
'ticks' => [
'display' => false,
],
],
],
];
protected function getData(): array
{
/** @var Node $node */
$node = $this->record;
$total = $node->statistics()['memory_total'] ?? 0;
$used = $node->statistics()['memory_used'] ?? 0;
$unused = $total - $used;
return [
'datasets' => [
[
'label' => 'Data Cool',
'data' => [$used, $unused],
'backgroundColor' => [
'rgb(255, 99, 132)',
'rgb(54, 162, 235)',
'rgb(255, 205, 86)',
],
],
// 'backgroundColor' => [],
],
'labels' => ['Used', 'Unused'],
];
}
protected function getType(): string
{
return 'pie';
}
}

View File

@ -0,0 +1,68 @@
<?php
namespace App\Filament\Resources\NodeResource\Widgets;
use App\Models\Node;
use Filament\Widgets\ChartWidget;
use Illuminate\Database\Eloquent\Model;
class NodeStorageChart extends ChartWidget
{
protected static ?string $heading = 'Storage';
protected static ?string $pollingInterval = '60s';
public ?Model $record = null;
protected static ?array $options = [
'scales' => [
'x' => [
'grid' => [
'display' => false,
],
'ticks' => [
'display' => false,
],
],
'y' => [
'grid' => [
'display' => false,
],
'ticks' => [
'display' => false,
],
],
],
];
protected function getData(): array
{
/** @var Node $node */
$node = $this->record;
$total = $node->statistics()['disk_total'] ?? 0;
$used = $node->statistics()['disk_used'] ?? 0;
$unused = $total - $used;
return [
'datasets' => [
[
'label' => 'Data Cool',
'data' => [$used, $unused],
'backgroundColor' => [
'rgb(255, 99, 132)',
'rgb(54, 162, 235)',
'rgb(255, 205, 86)',
],
],
// 'backgroundColor' => [],
],
'labels' => ['Used', 'Unused'],
];
}
protected function getType(): string
{
return 'pie';
}
}

View File

@ -309,6 +309,25 @@ class Node extends Model
return $statuses;
}
public function statistics()
{
return Http::daemon($this)->connectTimeout(1)->timeout(1)->get('/api/system/utilization')->json() ?? [];
/** Example
$stats = [
'memory_total' => 8221675520,
'memory_used' => 1670275072,
'swap_total' => 1073737728,
'swap_used' => 0,
'load_average1' => 0.67,
'load_average5' => 0.64,
'load_average15' => 0.7,
'cpu_percent' => 0.84361590728896,
'disk_total' => 62671097856,
'disk_used' => 33444728832,
]; // */
}
public function ipAddresses(): array
{
return cache()->remember("nodes.$this->id.ips", now()->addHour(), function () {