66 lines
1.6 KiB
PHP
66 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class ServerProcess extends Model
|
|
{
|
|
protected $fillable = [
|
|
'batch_id',
|
|
'rank',
|
|
'pid',
|
|
'user',
|
|
'command',
|
|
'cpu',
|
|
'memory',
|
|
'captured_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'cpu' => '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;
|
|
}
|
|
}
|