Files
jarvis/api/endpoints/metrics.php
myron 2f5e7b5a00 fix: remove missing includes/auth.php require from metrics and suggestions
Both endpoints tried to require a non-existent includes/auth.php and call
AuthMiddleware::requireAuth() — auth is already handled by api.php before
any endpoint file runs. This caused 500 errors on /api/metrics (which
blocked agent sparklines) and /api/suggestions.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-17 14:18:33 +00:00

50 lines
1.8 KiB
PHP

<?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));
}