'decimal:2', 'memory' => 'decimal:2', 'captured_at' => 'datetime', ]; public function scopeLatestBatch($query) { $latestBatchId = static::orderBy('captured_at', 'desc')->value('batch_id'); return $query->where('batch_id', $latestBatchId); } public static function captureProcesses(array $processes, int $total = 0): string { $batchId = (string) \Illuminate\Support\Str::uuid(); $now = now(); // Delete old snapshots (keep last 10 batches for history) $keepBatches = static::select('batch_id') ->distinct() ->orderBy('captured_at', 'desc') ->limit(10) ->pluck('batch_id'); static::whereNotIn('batch_id', $keepBatches)->delete(); // Insert new processes foreach ($processes as $index => $proc) { static::create([ 'batch_id' => $batchId, 'rank' => $index + 1, 'pid' => $proc['pid'] ?? 0, 'user' => $proc['user'] ?? 'unknown', 'command' => $proc['command'] ?? '', 'cpu' => $proc['cpu'] ?? 0, 'memory' => $proc['memory'] ?? 0, 'captured_at' => $now, ]); } return $batchId; } }