get(['id']) as $server) { $m = Cache::get("metrics:latest:{$server->id}"); if (! is_array($m)) { continue; } $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) ($m['cpu'] ?? 0))), 'mem' => min(100, max(0, (int) ($m['mem'] ?? 0))), 'disk' => min(100, max(0, (int) ($m['disk'] ?? 0))), 'load' => round((float) ($m['load'] ?? 0), 2), '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; } }