From 779d3e0bf6f3a2be21fc47e32242eef4b5b85f50 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Tue, 28 Jan 2025 15:27:40 +0200 Subject: [PATCH] Updates latest data page functionality --- app/classes/agent.php | 68 +++++++++++++++ app/pages/data.php | 97 +++++++++++++--------- app/templates/latest-data.php | 150 +++++++++++++++++++++++----------- 3 files changed, 231 insertions(+), 84 deletions(-) diff --git a/app/classes/agent.php b/app/classes/agent.php index 5bc9f73..f7668e2 100644 --- a/app/classes/agent.php +++ b/app/classes/agent.php @@ -576,6 +576,74 @@ class Agent { return $data; } + + /** + * Gets the previous record for a specific metric + * + * @param int $host_id The host ID + * @param string $agent_type The type of agent (e.g., 'jvb', 'jicofo') + * @param string $metric_type The type of metric to retrieve + * @param string $current_timestamp Current record's timestamp to get data before this + * @return array|null Previous record data or null if not found + */ + public function getPreviousRecord($host_id, $agent_type, $metric_type, $current_timestamp) { + $sql = 'SELECT + jac.timestamp, + jac.response_content + FROM + jilo_agent_checks jac + JOIN + jilo_agents ja ON jac.agent_id = ja.id + JOIN + jilo_agent_types jat ON ja.agent_type_id = jat.id + JOIN + hosts h ON ja.host_id = h.id + WHERE + h.id = :host_id + AND jat.description = :agent_type + AND jac.status_code = 200 + AND jac.timestamp < :current_timestamp + ORDER BY + jac.timestamp DESC + LIMIT 1'; + + $query = $this->db->prepare($sql); + $query->execute([ + ':host_id' => $host_id, + ':agent_type' => $agent_type, + ':current_timestamp' => $current_timestamp + ]); + + $result = $query->fetch(PDO::FETCH_ASSOC); + + if ($result) { + $json_data = json_decode($result['response_content'], true); + if (json_last_error() === JSON_ERROR_NONE) { + $api_data = []; + if ($agent_type === 'jvb') { + $api_data = $json_data['jvb_api_data'] ?? []; + } elseif ($agent_type === 'jicofo') { + $api_data = $json_data['jicofo_api_data'] ?? []; + } elseif ($agent_type === 'jigasi') { + $api_data = $json_data['jigasi_api_data'] ?? []; + } elseif ($agent_type === 'prosody') { + $api_data = $json_data['prosody_api_data'] ?? []; + } elseif ($agent_type === 'nginx') { + $api_data = $json_data['nginx_api_data'] ?? []; + } + + $value = $this->getNestedValue($api_data, $metric_type); + if ($value !== null) { + return [ + 'value' => $value, + 'timestamp' => $result['timestamp'] + ]; + } + } + } + + return null; + } } ?> diff --git a/app/pages/data.php b/app/pages/data.php index bd157c4..656c129 100644 --- a/app/pages/data.php +++ b/app/pages/data.php @@ -10,9 +10,11 @@ $agent = $_REQUEST['agent'] ?? ''; require '../app/classes/settings.php'; require '../app/classes/agent.php'; require '../app/classes/conference.php'; +require '../app/classes/host.php'; $settingsObject = new Settings(); $agentObject = new Agent($dbWeb); +$hostObject = new Host($dbWeb); // connect to Jilo database $response = connectDB($config, 'jilo', $platformDetails[0]['jilo_database'], $platform_id); @@ -125,7 +127,6 @@ if ($response['db'] === null) { 'jibri_detector.available' => ['label' => 'Jibri idle'], 'jibri.live_streaming_active' => ['label' => 'Jibri active streaming'], 'jibri.recording_active' => ['label' => 'Jibri active recording'], - ], 'System stats' => [ 'threads' => ['label' => 'Threads'], @@ -134,54 +135,74 @@ if ($response['db'] === null) { ] ]; - // Get latest data for all the agents - $agents = ['jvb', 'jicofo', 'jibri', 'prosody', 'nginx']; - $widget['records'] = []; + // Get all hosts for this platform + $hosts = $hostObject->getHostDetails($platform_id); + $hostsData = []; - // Initialize records for each agent - foreach ($agents as $agent) { - $record = [ - 'table_headers' => strtoupper($agent), - 'metrics' => [], - 'timestamp' => null + // For each host, get its agents and their metrics + foreach ($hosts as $host) { + $hostData = [ + 'id' => $host['id'], + 'name' => $host['name'] ?: $host['address'], + 'address' => $host['address'], + 'agents' => [] ]; - // Fetch all metrics for this agent - foreach ($metrics as $section => $section_metrics) { - foreach ($section_metrics as $metric => $metricConfig) { - $data = $agentObject->getLatestData($platform_id, $agent, $metric); - if ($data !== null) { - $record['metrics'][$section][$metric] = [ - 'value' => $data['value'], - 'label' => $metricConfig['label'], - 'link' => isset($metricConfig['link']) ? $metricConfig['link'] : null - ]; - // Use the most recent timestamp - if ($record['timestamp'] === null || strtotime($data['timestamp']) > strtotime($record['timestamp'])) { - $record['timestamp'] = $data['timestamp']; + // Get agents for this host + $hostAgents = $agentObject->getAgentDetails($host['id']); + foreach ($hostAgents as $agent) { + $agentData = [ + 'id' => $agent['id'], + 'type' => $agent['agent_description'], + 'name' => strtoupper($agent['agent_description']), + 'metrics' => [], + 'timestamp' => null + ]; + + // Fetch all metrics for this agent + foreach ($metrics as $section => $section_metrics) { + foreach ($section_metrics as $metric => $metricConfig) { + // Get latest data + $latestData = $agentObject->getLatestData($host['id'], $agent['agent_description'], $metric); + + if ($latestData !== null) { + // Get the previous record + $previousData = $agentObject->getPreviousRecord( + $host['id'], + $agent['agent_description'], + $metric, + $latestData['timestamp'] + ); + + $agentData['metrics'][$section][$metric] = [ + 'latest' => [ + 'value' => $latestData['value'], + 'timestamp' => $latestData['timestamp'] + ], + 'previous' => $previousData, + 'label' => $metricConfig['label'], + 'link' => isset($metricConfig['link']) ? $metricConfig['link'] : null + ]; + + // Use the most recent timestamp for the agent + if ($agentData['timestamp'] === null || strtotime($latestData['timestamp']) > strtotime($agentData['timestamp'])) { + $agentData['timestamp'] = $latestData['timestamp']; + } } } } + + if (!empty($agentData['metrics'])) { + $hostData['agents'][] = $agentData; + } } - if (!empty($record['metrics'])) { - $widget['records'][] = $record; + if (!empty($hostData['agents'])) { + $hostsData[] = $hostData; } } - // prepare the widget - $widget['full'] = false; - $widget['name'] = 'LatestData'; - $widget['title'] = 'Latest data from Jilo Agents'; - $widget['collapsible'] = false; - $widget['collapsed'] = false; - $widget['filter'] = false; - $widget['metrics'] = $metrics; // Pass metrics configuration to template - if (!empty($widget['records'])) { - $widget['full'] = true; - } - $widget['pagination'] = false; - + // Load the template include '../app/templates/latest-data.php'; break; diff --git a/app/templates/latest-data.php b/app/templates/latest-data.php index 853f9a7..1667003 100644 --- a/app/templates/latest-data.php +++ b/app/templates/latest-data.php @@ -1,53 +1,111 @@
-
+
Latest data from Jilo Agents
-
+
- - - - - - - - - - - - $section_metrics) { ?> - - - - $metricConfig) { ?> - - - - - - - - - -
Metric - - -
- as of - -
- - - - - - No data - -
+ + +
+
+
+ + + () +
+
+
+ +
+
+ + agent +
+ + + + + + + + + + $section_metrics) { ?> + $metricConfig) { + if (isset($agent['metrics'][$section][$metric])) { + $hasData = true; + break; + } + } + if (!$hasData) continue; + ?> + + + + $metricConfig) { ?> + + + + + + + + + + +
Metric + Latest value +
+ + + +
+ Previous value + $m_metrics) { + foreach ($m_metrics as $m_metric => $m_config) { + if (isset($agent['metrics'][$m_section][$m_metric]['previous'])) { + $prevTimestamp = $agent['metrics'][$m_section][$m_metric]['previous']['timestamp']; + break 2; + } + } + } + if ($prevTimestamp) { ?> +
+ + + + +
+ + + + + + + + + + + + + + No previous data + +
+
+ +
+
+
- +