Skip to content

Commit b34778f

Browse files
Refactor Node Stats (#1145)
Co-authored-by: Boy132 <[email protected]>
1 parent 84c351d commit b34778f

File tree

4 files changed

+55
-67
lines changed

4 files changed

+55
-67
lines changed

app/Console/Kernel.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use App\Console\Commands\Maintenance\PruneImagesCommand;
88
use App\Console\Commands\Maintenance\PruneOrphanedBackupsCommand;
99
use App\Console\Commands\Schedule\ProcessRunnableCommand;
10-
use App\Jobs\NodeStatistics;
1110
use App\Models\ActivityLog;
1211
use App\Models\Webhook;
1312
use Illuminate\Console\Scheduling\Schedule;
@@ -44,8 +43,6 @@ protected function schedule(Schedule $schedule): void
4443
$schedule->command(PruneImagesCommand::class)->daily();
4544
$schedule->command(CheckEggUpdatesCommand::class)->hourly();
4645

47-
$schedule->job(new NodeStatistics())->everyFiveSeconds()->withoutOverlapping();
48-
4946
if (config('backups.prune_age')) {
5047
// Every 30 minutes, run the backup pruning command so that any abandoned backups can be deleted.
5148
$schedule->command(PruneOrphanedBackupsCommand::class)->everyThirtyMinutes();

app/Filament/Admin/Resources/NodeResource/Widgets/NodeCpuChart.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace App\Filament\Admin\Resources\NodeResource\Widgets;
44

55
use App\Models\Node;
6-
use Carbon\Carbon;
76
use Filament\Support\RawJs;
87
use Filament\Widgets\ChartWidget;
98
use Illuminate\Support\Number;
@@ -16,30 +15,42 @@ class NodeCpuChart extends ChartWidget
1615

1716
public Node $node;
1817

18+
/**
19+
* @var array<int, array{cpu: string, timestamp: string}>
20+
*/
21+
protected array $cpuHistory = [];
22+
23+
protected int $threads = 0;
24+
1925
protected function getData(): array
2026
{
21-
$threads = $this->node->systemInformation()['cpu_count'] ?? 0;
27+
$sessionKey = "node_stats.{$this->node->id}";
28+
29+
$data = $this->node->statistics();
30+
31+
$this->threads = session("{$sessionKey}.threads", $this->node->systemInformation()['cpu_count'] ?? 0);
32+
33+
$this->cpuHistory = session("{$sessionKey}.cpu_history", []);
34+
$this->cpuHistory[] = [
35+
'cpu' => round($data['cpu_percent'] * $this->threads, 2),
36+
'timestamp' => now(auth()->user()->timezone ?? 'UTC')->format('H:i:s'),
37+
];
2238

23-
$cpu = collect(cache()->get("nodes.{$this->node->id}.cpu_percent"))
24-
->slice(-10)
25-
->map(fn ($value, $key) => [
26-
'cpu' => round($value * $threads, 2),
27-
'timestamp' => Carbon::createFromTimestamp($key, auth()->user()->timezone ?? 'UTC')->format('H:i:s'),
28-
])
29-
->all();
39+
$this->cpuHistory = array_slice($this->cpuHistory, -60);
40+
session()->put("{$sessionKey}.cpu_history", $this->cpuHistory);
3041

3142
return [
3243
'datasets' => [
3344
[
34-
'data' => array_column($cpu, 'cpu'),
45+
'data' => array_column($this->cpuHistory, 'cpu'),
3546
'backgroundColor' => [
3647
'rgba(96, 165, 250, 0.3)',
3748
],
3849
'tension' => '0.3',
3950
'fill' => true,
4051
],
4152
],
42-
'labels' => array_column($cpu, 'timestamp'),
53+
'labels' => array_column($this->cpuHistory, 'timestamp'),
4354
'locale' => auth()->user()->language ?? 'en',
4455
];
4556
}
@@ -69,10 +80,10 @@ protected function getOptions(): RawJs
6980

7081
public function getHeading(): string
7182
{
72-
$threads = $this->node->systemInformation()['cpu_count'] ?? 0;
83+
$data = array_slice(end($this->cpuHistory), -60);
7384

74-
$cpu = Number::format(collect(cache()->get("nodes.{$this->node->id}.cpu_percent"))->last() * $threads, maxPrecision: 2, locale: auth()->user()->language);
75-
$max = Number::format($threads * 100, locale: auth()->user()->language);
85+
$cpu = Number::format($data['cpu'], maxPrecision: 2, locale: auth()->user()->language);
86+
$max = Number::format($this->threads * 100, locale: auth()->user()->language);
7687

7788
return trans('admin/node.cpu_chart', ['cpu' => $cpu, 'max' => $max]);
7889
}

app/Filament/Admin/Resources/NodeResource/Widgets/NodeMemoryChart.php

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace App\Filament\Admin\Resources\NodeResource\Widgets;
44

55
use App\Models\Node;
6-
use Carbon\Carbon;
76
use Filament\Support\RawJs;
87
use Filament\Widgets\ChartWidget;
98
use Illuminate\Support\Number;
@@ -16,27 +15,44 @@ class NodeMemoryChart extends ChartWidget
1615

1716
public Node $node;
1817

18+
/**
19+
* @var array<int, array{memory: string, timestamp: string}>
20+
*/
21+
protected array $memoryHistory = [];
22+
23+
protected int $totalMemory = 0;
24+
1925
protected function getData(): array
2026
{
21-
$memUsed = collect(cache()->get("nodes.{$this->node->id}.memory_used"))->slice(-10)
22-
->map(fn ($value, $key) => [
23-
'memory' => round(config('panel.use_binary_prefix') ? $value / 1024 / 1024 / 1024 : $value / 1000 / 1000 / 1000, 2),
24-
'timestamp' => Carbon::createFromTimestamp($key, auth()->user()->timezone ?? 'UTC')->format('H:i:s'),
25-
])
26-
->all();
27+
$sessionKey = "node_stats.{$this->node->id}";
28+
29+
$data = $this->node->statistics();
30+
31+
$this->totalMemory = session("{$sessionKey}.total_memory", $data['memory_total']);
32+
33+
$this->memoryHistory = session("{$sessionKey}.memory_history", []);
34+
$this->memoryHistory[] = [
35+
'memory' => round(config('panel.use_binary_prefix')
36+
? $data['memory_used'] / 1024 / 1024 / 1024
37+
: $data['memory_used'] / 1000 / 1000 / 1000, 2),
38+
'timestamp' => now(auth()->user()->timezone ?? 'UTC')->format('H:i:s'),
39+
];
40+
41+
$this->memoryHistory = array_slice($this->memoryHistory, -60);
42+
session()->put("{$sessionKey}.memory_history", $this->memoryHistory);
2743

2844
return [
2945
'datasets' => [
3046
[
31-
'data' => array_column($memUsed, 'memory'),
47+
'data' => array_column($this->memoryHistory, 'memory'),
3248
'backgroundColor' => [
3349
'rgba(96, 165, 250, 0.3)',
3450
],
3551
'tension' => '0.3',
3652
'fill' => true,
3753
],
3854
],
39-
'labels' => array_column($memUsed, 'timestamp'),
55+
'labels' => array_column($this->memoryHistory, 'timestamp'),
4056
'locale' => auth()->user()->language ?? 'en',
4157
];
4258
}
@@ -66,16 +82,15 @@ protected function getOptions(): RawJs
6682

6783
public function getHeading(): string
6884
{
69-
$latestMemoryUsed = collect(cache()->get("nodes.{$this->node->id}.memory_used"))->last();
70-
$totalMemory = collect(cache()->get("nodes.{$this->node->id}.memory_total"))->last();
85+
$latestMemoryUsed = array_slice(end($this->memoryHistory), -60);
7186

7287
$used = config('panel.use_binary_prefix')
73-
? Number::format($latestMemoryUsed / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB'
74-
: Number::format($latestMemoryUsed / 1000 / 1000 / 1000, maxPrecision: 2, locale: auth()->user()->language) . ' GB';
88+
? Number::format($latestMemoryUsed['memory'], maxPrecision: 2, locale: auth()->user()->language) .' GiB'
89+
: Number::format($latestMemoryUsed['memory'], maxPrecision: 2, locale: auth()->user()->language) . ' GB';
7590

7691
$total = config('panel.use_binary_prefix')
77-
? Number::format($totalMemory / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB'
78-
: Number::format($totalMemory / 1000 / 1000 / 1000, maxPrecision: 2, locale: auth()->user()->language) . ' GB';
92+
? Number::format($this->totalMemory / 1024 / 1024 / 1024, maxPrecision: 2, locale: auth()->user()->language) .' GiB'
93+
: Number::format($this->totalMemory / 1000 / 1000 / 1000, maxPrecision: 2, locale: auth()->user()->language) . ' GB';
7994

8095
return trans('admin/node.memory_chart', ['used' => $used, 'total' => $total]);
8196
}

app/Jobs/NodeStatistics.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)