mirror of
https://github.com/myronblair/jarvis
synced 2026-07-27 16:22:55 -05:00
Adopt live production state as git baseline
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
// Agent metrics history — returns CPU/RAM samples for sparklines
|
||||
require_once __DIR__ . '/../config.php';
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$agentId = $_GET['agent_id'] ?? '';
|
||||
$hours = min((int)($_GET['hours'] ?? 2), 24);
|
||||
|
||||
if (!$agentId) {
|
||||
// Return all online agents' last 2h in one shot
|
||||
$agents = JarvisDB::query(
|
||||
"SELECT DISTINCT agent_id FROM agent_metrics WHERE recorded_at > DATE_SUB(NOW(), INTERVAL ? HOUR)",
|
||||
[$hours]
|
||||
) ?? [];
|
||||
|
||||
$out = [];
|
||||
foreach ($agents as $a) {
|
||||
$rows = JarvisDB::query(
|
||||
"SELECT CAST(JSON_EXTRACT(metric_data, '$.cpu_percent') AS DECIMAL(5,1)) as cpu,
|
||||
CAST(JSON_EXTRACT(metric_data, '$.memory.percent') AS DECIMAL(5,1)) as mem,
|
||||
recorded_at
|
||||
FROM agent_metrics
|
||||
WHERE agent_id = ? AND recorded_at > DATE_SUB(NOW(), INTERVAL ? HOUR)
|
||||
ORDER BY recorded_at ASC",
|
||||
[$a['agent_id'], $hours]
|
||||
) ?? [];
|
||||
if ($rows) {
|
||||
$out[$a['agent_id']] = array_map(fn($r) => [
|
||||
'cpu' => (float)($r['cpu'] ?? 0),
|
||||
'mem' => (float)($r['mem'] ?? 0),
|
||||
], $rows);
|
||||
}
|
||||
}
|
||||
echo json_encode($out);
|
||||
} else {
|
||||
$rows = JarvisDB::query(
|
||||
"SELECT CAST(JSON_EXTRACT(metric_data, '$.cpu_percent') AS DECIMAL(5,1)) as cpu,
|
||||
CAST(JSON_EXTRACT(metric_data, '$.memory.percent') AS DECIMAL(5,1)) as mem,
|
||||
recorded_at
|
||||
FROM agent_metrics
|
||||
WHERE agent_id = ? AND recorded_at > DATE_SUB(NOW(), INTERVAL ? HOUR)
|
||||
ORDER BY recorded_at ASC",
|
||||
[$agentId, $hours]
|
||||
) ?? [];
|
||||
echo json_encode(array_map(fn($r) => [
|
||||
'cpu' => (float)($r['cpu'] ?? 0),
|
||||
'mem' => (float)($r['mem'] ?? 0),
|
||||
], $rows));
|
||||
}
|
||||
Reference in New Issue
Block a user