diff --git a/public/index.php b/public/index.php index 42fc4e6..2e304dd 100755 --- a/public/index.php +++ b/public/index.php @@ -141,6 +141,9 @@ $dashboardGroup = $app->group('', function ($group) use ($twig) { return $twig->render($response, 'dashboard.twig', $templateData); }); })->add($csrfMiddleware)->add(AuthMiddleware::class); +n// API для дашборда + = new DashboardController(); +->get('/api/dashboard/stats', [, 'getDashboardData']); // Create controllers BEFORE routes $groupController = new GroupController($twig); diff --git a/src/Controllers/DashboardController.php b/src/Controllers/DashboardController.php index 66b1621..14d1784 100755 --- a/src/Controllers/DashboardController.php +++ b/src/Controllers/DashboardController.php @@ -42,4 +42,43 @@ class DashboardController return $this->twig->render($response, 'dashboard.twig', $templateData); } + + public function getDashboardData(Request $request, Response $response, $args) + { + $servers = $this->serverModel->getServersWithStatus(); + + $result = []; + foreach ($servers as $server) { + $serverData = [ + 'id' => $server['id'], + 'status' => $server['status'], + 'updated_at' => $server['last_metrics_at'] ? date('d.m.Y H:i:s', strtotime($server['last_metrics_at'])) : 'Нет данных', + 'metrics' => [] + ]; + + if (isset($server['latest_metrics']['cpu_load'])) { + $serverData['metrics']['cpu_load'] = [ + 'value' => $server['latest_metrics']['cpu_load']['value'], + 'unit' => $server['latest_metrics']['cpu_load']['unit'] ?? '%' + ]; + } + if (isset($server['latest_metrics']['ram_used'])) { + $serverData['metrics']['ram_used'] = [ + 'value' => $server['latest_metrics']['ram_used']['value'], + 'unit' => $server['latest_metrics']['ram_used']['unit'] ?? '%' + ]; + } + $diskMetric = $server['latest_metrics']['disk_used_root'] ?? $server['latest_metrics']['disk_used'] ?? null; + if ($diskMetric) { + $serverData['metrics']['disk'] = [ + 'value' => $diskMetric['value'], + 'unit' => $diskMetric['unit'] ?? '%' + ]; + } + $result[] = $serverData; + } + + $response->getBody()->write(json_encode($result)); + return $response->withHeader('Content-Type', 'application/json'); + } } diff --git a/templates/dashboard.twig b/templates/dashboard.twig index 233acf3..5351c3c 100755 --- a/templates/dashboard.twig +++ b/templates/dashboard.twig @@ -107,7 +107,7 @@