copy()->subSeconds(90); $rows = []; foreach (Server::query()->get(['id', 'cpu', 'mem', 'disk', 'last_seen_at']) as $server) { if ($server->last_seen_at === null || $server->last_seen_at->lt($fresh)) { continue; } // load is not persisted on the row; best-effort from the live cache when the store is shared. $cached = Cache::get("metrics:latest:{$server->id}"); $rows[] = [ 'server_id' => $server->id, // Percentages — clamp to 0..100 so a stray reading can never plot outside the chart. 'cpu' => min(100, max(0, (int) $server->cpu)), 'mem' => min(100, max(0, (int) $server->mem)), 'disk' => min(100, max(0, (int) $server->disk)), 'load' => is_array($cached) ? round((float) ($cached['load'] ?? 0), 2) : 0.0, 'sampled_at' => $now, ]; } if ($rows !== []) { MetricSample::insert($rows); } $retention = max(1, (int) $this->option('retention')); MetricSample::where('sampled_at', '<', now()->subDays($retention))->delete(); return self::SUCCESS; } }