feat: add aggregated resource usage chart to dashboard
This commit is contained in:
@@ -34,12 +34,15 @@ class DashboardController
|
||||
return in_array((int) $s['id'], $accessibleIds, true);
|
||||
})) : [];
|
||||
|
||||
$aggregatedMetrics = $this->monitoringService->getAggregatedHistoricalMetrics($accessibleIds ?? []);
|
||||
|
||||
$recentActivity = $this->auditService->getRecentActivity(10, $userId);
|
||||
|
||||
$this->view->display('dashboard.index', [
|
||||
'title' => 'Dashboard - ServerManager',
|
||||
'stats' => $stats,
|
||||
'servers' => $servers,
|
||||
'aggregatedMetrics' => $aggregatedMetrics,
|
||||
'recentActivity' => $recentActivity,
|
||||
]);
|
||||
}
|
||||
@@ -65,4 +68,16 @@ class DashboardController
|
||||
'message' => 'Metrics refreshed',
|
||||
]);
|
||||
}
|
||||
|
||||
public function chartData(): void
|
||||
{
|
||||
$serverModel = new Server();
|
||||
$accessibleIds = $serverModel->getAccessibleServerIds((int) Session::get('user_id'));
|
||||
$data = $this->monitoringService->getAggregatedHistoricalMetrics($accessibleIds ?? []);
|
||||
|
||||
$this->view->json([
|
||||
'success' => true,
|
||||
'data' => $data,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,6 +173,29 @@ class MonitoringService
|
||||
);
|
||||
}
|
||||
|
||||
public function getAggregatedHistoricalMetrics(array $serverIds, int $hours = 24): array
|
||||
{
|
||||
if (empty($serverIds)) {
|
||||
return [];
|
||||
}
|
||||
$placeholders = implode(',', array_fill(0, count($serverIds), '?'));
|
||||
$params = $serverIds;
|
||||
$params[] = $hours;
|
||||
return $this->db->fetchAll(
|
||||
"SELECT
|
||||
DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:00') as time_bucket,
|
||||
AVG(cpu_usage) as avg_cpu,
|
||||
AVG(ram_usage) as avg_ram,
|
||||
AVG(disk_usage) as avg_disk
|
||||
FROM monitoring_history
|
||||
WHERE server_id IN ({$placeholders})
|
||||
AND created_at >= DATE_SUB(NOW(), INTERVAL ? HOUR)
|
||||
GROUP BY time_bucket
|
||||
ORDER BY time_bucket ASC",
|
||||
$params
|
||||
);
|
||||
}
|
||||
|
||||
public function getDashboardStats(?array $serverIds = null): array
|
||||
{
|
||||
$where = '';
|
||||
|
||||
Reference in New Issue
Block a user