Skip to content
17 changes: 0 additions & 17 deletions app/Filament/Server/Widgets/ServerConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,6 @@ public function tokenRequest(): void
$this->dispatch('sendAuthRequest', token: $this->getToken());
}

#[On('store-stats')]
public function storeStats(string $data): void
{
$data = json_decode($data);

$timestamp = now()->getTimestamp();

foreach ($data as $key => $value) {
$cacheKey = "servers.{$this->server->id}.$key";
$cachedStats = cache()->get($cacheKey, []);

$cachedStats[$timestamp] = $value;

cache()->put($cacheKey, array_slice($cachedStats, -120), now()->addMinute());
}
}

#[On('websocket-error')]
public function websocketError(): void
{
Expand Down
17 changes: 3 additions & 14 deletions app/Filament/Server/Widgets/ServerCpuChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

namespace App\Filament\Server\Widgets;

use App\Enums\CustomizationKey;
use App\Models\Server;
use Filament\Facades\Filament;
use Filament\Support\RawJs;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Carbon;

class ServerCpuChart extends ChartWidget
{
protected ?string $pollingInterval = '1s';
protected ?string $pollingInterval = null;

protected ?string $maxHeight = '200px';

Expand All @@ -27,27 +25,18 @@ public static function canView(): bool

protected function getData(): array
{
$period = (int) user()?->getCustomization(CustomizationKey::ConsoleGraphPeriod);
$cpu = collect(cache()->get("servers.{$this->server->id}.cpu_absolute"))
->slice(-$period)
->map(fn ($value, $key) => [
'cpu' => round($value, 2),
'timestamp' => Carbon::createFromTimestamp($key, user()->timezone ?? 'UTC')->format('H:i:s'),
])
->all();

return [
'datasets' => [
[
'data' => array_column($cpu, 'cpu'),
'data' => [],
'backgroundColor' => [
'rgba(96, 165, 250, 0.3)',
],
'tension' => '0.3',
'fill' => true,
],
],
'labels' => array_column($cpu, 'timestamp'),
'labels' => [],
'locale' => user()->language ?? 'en',
];
}
Expand Down
17 changes: 3 additions & 14 deletions app/Filament/Server/Widgets/ServerMemoryChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

namespace App\Filament\Server\Widgets;

use App\Enums\CustomizationKey;
use App\Models\Server;
use Filament\Facades\Filament;
use Filament\Support\RawJs;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Carbon;

class ServerMemoryChart extends ChartWidget
{
protected ?string $pollingInterval = '1s';
protected ?string $pollingInterval = null;

protected ?string $maxHeight = '200px';

Expand All @@ -27,27 +25,18 @@ public static function canView(): bool

protected function getData(): array
{
$period = (int) user()?->getCustomization(CustomizationKey::ConsoleGraphPeriod);
$memUsed = collect(cache()->get("servers.{$this->server->id}.memory_bytes"))
->slice(-$period)
->map(fn ($value, $key) => [
'memory' => round(config('panel.use_binary_prefix') ? $value / 1024 / 1024 / 1024 : $value / 1000 / 1000 / 1000, 2),
'timestamp' => Carbon::createFromTimestamp($key, user()->timezone ?? 'UTC')->format('H:i:s'),
])
->all();

return [
'datasets' => [
[
'data' => array_column($memUsed, 'memory'),
'data' => [],
'backgroundColor' => [
'rgba(96, 165, 250, 0.3)',
],
'tension' => '0.3',
'fill' => true,
],
],
'labels' => array_column($memUsed, 'timestamp'),
'labels' => [],
'locale' => user()->language ?? 'en',
];
}
Expand Down
39 changes: 7 additions & 32 deletions app/Filament/Server/Widgets/ServerNetworkChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace App\Filament\Server\Widgets;

use App\Enums\CustomizationKey;
use App\Models\Server;
use Filament\Facades\Filament;
use Filament\Support\RawJs;
use Filament\Widgets\ChartWidget;
use Illuminate\Support\Carbon;
use Illuminate\Support\HtmlString;

class ServerNetworkChart extends ChartWidget
{
protected ?string $pollingInterval = '1s';
protected ?string $pollingInterval = null;

protected ?string $maxHeight = '200px';

Expand All @@ -27,33 +26,11 @@ public static function canView(): bool

protected function getData(): array
{
$previous = null;

$period = (int) user()?->getCustomization(CustomizationKey::ConsoleGraphPeriod);
$net = collect(cache()->get("servers.{$this->server->id}.network"))
->slice(-$period)
->map(function ($current, $timestamp) use (&$previous) {
$net = null;

if ($previous !== null) {
$net = [
'rx' => max(0, $current->rx_bytes - $previous->rx_bytes),
'tx' => max(0, $current->tx_bytes - $previous->tx_bytes),
'timestamp' => Carbon::createFromTimestamp($timestamp, user()->timezone ?? 'UTC')->format('H:i:s'),
];
}

$previous = $current;

return $net;
})
->all();

return [
'datasets' => [
[
'label' => 'Inbound',
'data' => array_column($net, 'rx'),
'data' => [],
'backgroundColor' => [
'rgba(100, 255, 105, 0.5)',
],
Expand All @@ -62,15 +39,15 @@ protected function getData(): array
],
[
'label' => 'Outbound',
'data' => array_column($net, 'tx'),
'data' => [],
'backgroundColor' => [
'rgba(96, 165, 250, 0.3)',
],
'tension' => '0.3',
'fill' => true,
],
],
'labels' => array_column($net, 'timestamp'),
'labels' => [],
];
}

Expand Down Expand Up @@ -109,10 +86,8 @@ protected function getOptions(): RawJs
JS);
}

public function getHeading(): string
public function getHeading(): HtmlString
{
$lastData = collect(cache()->get("servers.{$this->server->id}.network"))->last();

return trans('server/console.labels.network') . ' - ↓' . convert_bytes_to_readable($lastData->rx_bytes ?? 0) . ' - ↑' . convert_bytes_to_readable($lastData->tx_bytes ?? 0);
return new HtmlString(e(trans('server/console.labels.network')) . ' <span id="server-network-heading"></span>');
}
}
65 changes: 19 additions & 46 deletions app/Filament/Server/Widgets/ServerOverview.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace App\Filament\Server\Widgets;

use App\Enums\ContainerStatus;
use App\Filament\Server\Components\SmallStatBlock;
use App\Models\Server;
use Carbon\CarbonInterface;
use Filament\Widgets\StatsOverviewWidget;
use Illuminate\Support\HtmlString;

class ServerOverview extends StatsOverviewWidget
{
protected ?string $pollingInterval = '1s';
private const UNKNOWN = '—';

protected ?string $pollingInterval = null;

public ?Server $server = null;

Expand All @@ -28,64 +29,36 @@ protected function getStats(): array
];
}

private function status(): string
private function status(): HtmlString
{
$status = $this->server->condition->getLabel();
$uptime = collect(cache()->get("servers.{$this->server->id}.uptime"))->last() ?? 0;

if ($uptime === 0) {
return $status;
}

$uptime = now()->subMillis($uptime)->diffForHumans(syntax: CarbonInterface::DIFF_ABSOLUTE, short: true, parts: 2);

return "$status ($uptime)";
return new HtmlString('<span id="server-stat-status">' . e($this->statusText()) . '</span>');
}

public function cpuUsage(): string
private function statusText(): string
{
$status = $this->server->retrieveStatus();

if ($status->isOffline()) {
return ContainerStatus::Offline->getLabel();
}

$data = collect(cache()->get("servers.{$this->server->id}.cpu_absolute"))->last(default: 0);
$cpu = format_number($data, maxPrecision: 2) . ' %';

return $cpu . ($this->server->cpu > 0 ? ' / ' . format_number($this->server->cpu) . ' %' : ' / ∞');
return $this->server->condition->getLabel();
}

public function memoryUsage(): string
public function cpuUsage(): HtmlString
{
$status = $this->server->retrieveStatus();
$limit = $this->server->cpu > 0 ? ' / ' . format_number($this->server->cpu) . ' %' : ' / ∞';

if ($status->isOffline()) {
return ContainerStatus::Offline->getLabel();
}
return new HtmlString('<span id="server-stat-cpu">' . self::UNKNOWN . '</span>' . e($limit));
}

$latestMemoryUsed = collect(cache()->get("servers.{$this->server->id}.memory_bytes"))->last(default: 0);
public function memoryUsage(): HtmlString
{
$totalMemory = $this->server->memory * (config('panel.use_binary_prefix') ? 1024 * 1024 : 1000 * 1000);
$limit = $this->server->memory > 0 ? ' / ' . convert_bytes_to_readable($totalMemory) : ' / ∞';

$used = convert_bytes_to_readable($latestMemoryUsed);
$total = convert_bytes_to_readable($totalMemory);

return $used . ($this->server->memory > 0 ? ' / ' . $total : ' / ∞');
return new HtmlString('<span id="server-stat-memory">' . self::UNKNOWN . '</span>' . e($limit));
}

public function diskUsage(): string
public function diskUsage(): HtmlString
{
$disk = collect(cache()->get("servers.{$this->server->id}.disk_bytes"))->last(default: 0);

if ($disk === 0) {
return 'Unavailable';
}

$totalBytes = $this->server->disk * (config('panel.use_binary_prefix') ? 1024 * 1024 : 1000 * 1000);
$limit = $this->server->disk > 0 ? ' / ' . convert_bytes_to_readable($totalBytes) : ' / ∞';

$used = convert_bytes_to_readable($disk);
$total = convert_bytes_to_readable($totalBytes);

return $used . ($this->server->disk > 0 ? ' / ' . $total : ' / ∞');
return new HtmlString('<span id="server-stat-disk">' . self::UNKNOWN . '</span>' . e($limit));
}
}
Loading
Loading