mirror of
https://github.com/tiennm99/coolify.git
synced 2026-04-17 17:21:04 +00:00
fix: prevent metric charts from freezing when navigating with wire:navigate
Wraps inline chart initialization scripts in IIFEs to create local scope for variables. This prevents "Identifier has already been declared" errors when Livewire's SPA navigation re-executes scripts, allowing smooth navigation between metrics pages without page refresh. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
74
app/Traits/HasMetrics.php
Normal file
74
app/Traits/HasMetrics.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
trait HasMetrics
|
||||
{
|
||||
public function getCpuMetrics(int $mins = 5): ?array
|
||||
{
|
||||
return $this->getMetrics('cpu', $mins, 'percent');
|
||||
}
|
||||
|
||||
public function getMemoryMetrics(int $mins = 5): ?array
|
||||
{
|
||||
$field = $this->isServerMetrics() ? 'usedPercent' : 'used';
|
||||
|
||||
return $this->getMetrics('memory', $mins, $field);
|
||||
}
|
||||
|
||||
private function getMetrics(string $type, int $mins, string $valueField): ?array
|
||||
{
|
||||
$server = $this->getMetricsServer();
|
||||
if (! $server->isMetricsEnabled()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$from = now()->subMinutes($mins)->toIso8601ZuluString();
|
||||
$endpoint = $this->getMetricsEndpoint($type, $from);
|
||||
|
||||
$response = instant_remote_process(
|
||||
["docker exec coolify-sentinel sh -c 'curl -H \"Authorization: Bearer {$server->settings->sentinel_token}\" {$endpoint}'"],
|
||||
$server,
|
||||
false
|
||||
);
|
||||
|
||||
if (str($response)->contains('error')) {
|
||||
$error = json_decode($response, true);
|
||||
$error = data_get($error, 'error', 'Something is not okay, are you okay?');
|
||||
if ($error === 'Unauthorized') {
|
||||
$error = 'Unauthorized, please check your metrics token or restart Sentinel to set a new token.';
|
||||
}
|
||||
throw new \Exception($error);
|
||||
}
|
||||
|
||||
$metrics = collect(json_decode($response, true))->map(function ($metric) use ($valueField) {
|
||||
return [(int) $metric['time'], (float) ($metric[$valueField] ?? 0.0)];
|
||||
})->toArray();
|
||||
|
||||
if ($mins > 60 && count($metrics) > 1000) {
|
||||
$metrics = downsampleLTTB($metrics, 1000);
|
||||
}
|
||||
|
||||
return $metrics;
|
||||
}
|
||||
|
||||
private function isServerMetrics(): bool
|
||||
{
|
||||
return $this instanceof \App\Models\Server;
|
||||
}
|
||||
|
||||
private function getMetricsServer(): \App\Models\Server
|
||||
{
|
||||
return $this->isServerMetrics() ? $this : $this->destination->server;
|
||||
}
|
||||
|
||||
private function getMetricsEndpoint(string $type, string $from): string
|
||||
{
|
||||
$base = 'http://localhost:8888/api';
|
||||
if ($this->isServerMetrics()) {
|
||||
return "{$base}/{$type}/history?from={$from}";
|
||||
}
|
||||
|
||||
return "{$base}/container/{$this->uuid}/{$type}/history?from={$from}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user