Dashboard stats now scoped to user's accessible servers; super_admin sees all

This commit is contained in:
2026-06-07 07:43:37 -04:00
parent 5b13518672
commit b105b27124
2 changed files with 51 additions and 15 deletions

View File

@@ -173,20 +173,41 @@ class MonitoringService
);
}
public function getDashboardStats(): array
public function getDashboardStats(?array $serverIds = null): array
{
$totalServers = $this->db->fetch("SELECT COUNT(*) as total FROM servers");
$onlineServers = $this->db->fetch("SELECT COUNT(*) as total FROM servers WHERE current_status = 'online' AND is_active = 1");
$offlineServers = $this->db->fetch("SELECT COUNT(*) as total FROM servers WHERE current_status = 'offline' AND is_active = 1");
$where = '';
$params = [];
if ($serverIds !== null) {
if (empty($serverIds)) {
$serverIds = [-1];
}
$placeholders = implode(',', array_fill(0, count($serverIds), '?'));
$where = " AND s.id IN ({$placeholders})";
$params = $serverIds;
}
$totalServers = $this->db->fetch("SELECT COUNT(*) as total FROM servers s WHERE 1=1{$where}", $params);
$onlineServers = $this->db->fetch(
"SELECT COUNT(*) as total FROM servers s WHERE s.current_status = 'online' AND s.is_active = 1{$where}",
$params
);
$offlineServers = $this->db->fetch(
"SELECT COUNT(*) as total FROM servers s WHERE s.current_status = 'offline' AND s.is_active = 1{$where}",
$params
);
$avgCpu = $this->db->fetch(
"SELECT AVG(cpu_usage) as avg FROM servers WHERE is_active = 1 AND current_status = 'online'"
"SELECT AVG(s.cpu_usage) as avg FROM servers s WHERE s.is_active = 1 AND s.current_status = 'online'{$where}",
$params
);
$avgRam = $this->db->fetch(
"SELECT AVG(ram_usage) as avg FROM servers WHERE is_active = 1 AND current_status = 'online'"
"SELECT AVG(s.ram_usage) as avg FROM servers s WHERE s.is_active = 1 AND s.current_status = 'online'{$where}",
$params
);
$avgDisk = $this->db->fetch(
"SELECT AVG(disk_usage) as avg FROM servers WHERE is_active = 1 AND current_status = 'online'"
"SELECT AVG(s.disk_usage) as avg FROM servers s WHERE s.is_active = 1 AND s.current_status = 'online'{$where}",
$params
);
return [