diff --git a/app/classes/agent.php b/app/classes/agent.php index fa5aba5..1589fa3 100644 --- a/app/classes/agent.php +++ b/app/classes/agent.php @@ -375,6 +375,28 @@ class Agent { } + /** + * Gets a value from a nested array using dot notation + * e.g. "bridge_selector.bridge_count" will get $array['bridge_selector']['bridge_count'] + * + * @param array $array The array to search in + * @param string $path The path in dot notation + * @return mixed|null The value if found, null otherwise + */ + private function getNestedValue($array, $path) { + $keys = explode('.', $path); + $value = $array; + + foreach ($keys as $key) { + if (!isset($value[$key])) { + return null; + } + $value = $value[$key]; + } + + return $value; + } + /** * Retrieves the latest stored data for a specific platform, agent type, and metric type. * @@ -385,22 +407,22 @@ class Agent { * @return mixed The latest stored data. */ public function getLatestData($platform_id, $agent_type, $metric_type) { - $sql = 'SELECT + $sql = 'SELECT jac.timestamp, jac.response_content, jac.agent_id, jat.description - FROM + FROM jilo_agent_checks jac - JOIN + JOIN jilo_agents ja ON jac.agent_id = ja.id - JOIN + JOIN jilo_agent_types jat ON ja.agent_type_id = jat.id - WHERE + WHERE ja.platform_id = :platform_id AND jat.description = :agent_type AND jac.status_code = 200 - ORDER BY + ORDER BY jac.timestamp DESC LIMIT 1'; @@ -411,32 +433,64 @@ class Agent { ]); $result = $query->fetch(PDO::FETCH_ASSOC); - + if ($result) { // Parse the JSON response content $data = json_decode($result['response_content'], true); if (json_last_error() !== JSON_ERROR_NONE) { return null; } - + // Extract the specific metric value from the response based on agent type if ($agent_type === 'jvb') { - if (isset($data['jvb_api_data'][$metric_type])) { + $value = $this->getNestedValue($data['jvb_api_data'], $metric_type); + if ($value !== null) { return [ - 'value' => $data['jvb_api_data'][$metric_type], + 'value' => $value, 'timestamp' => $result['timestamp'] ]; } + } elseif ($agent_type === 'jicofo') { - if (isset($data['jicofo_api_data'][$metric_type])) { + $value = $this->getNestedValue($data['jicofo_api_data'], $metric_type); + if ($value !== null) { return [ - 'value' => $data['jicofo_api_data'][$metric_type], + 'value' => $value, + 'timestamp' => $result['timestamp'] + ]; + } + + } elseif ($agent_type === 'jigasi') { + $value = $this->getNestedValue($data['jigasi_api_data'], $metric_type); + if ($value !== null) { + return [ + 'value' => $value, + 'timestamp' => $result['timestamp'] + ]; + } + + } elseif ($agent_type === 'prosody') { + $value = $this->getNestedValue($data['prosody_api_data'], $metric_type); + if ($value !== null) { + return [ + 'value' => $value, + 'timestamp' => $result['timestamp'] + ]; + } + + } elseif ($agent_type === 'nginx') { + $value = $this->getNestedValue($data['nginx_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 d3b1cb8..a4b5178 100644 --- a/app/pages/data.php +++ b/app/pages/data.php @@ -64,34 +64,59 @@ switch ($item) { break; case 'latest': - // Get latest data for both JVB and Jicofo agents - $latestJvbConferences = $agentObject->getLatestData($platform_id, 'jvb', 'conferences'); - $latestJvbParticipants = $agentObject->getLatestData($platform_id, 'jvb', 'participants'); - $latestJicofoConferences = $agentObject->getLatestData($platform_id, 'jicofo', 'conferences'); - $latestJicofoParticipants = $agentObject->getLatestData($platform_id, 'jicofo', 'participants'); + // Define metrics to display + $metrics = [ + 'Basic stats' => [ + 'conferences' => ['label' => 'Current conferences', 'link' => 'conferences'], + 'participants' => ['label' => 'Current participants', 'link' => 'participants'], + 'total_conferences_created' => ['label' => 'Total conferences created'], + 'total_participants' => ['label' => 'Total participants'] + ], + 'Bridge stats' => [ + 'bridge_selector.bridge_count' => ['label' => 'Bridge count'], + 'bridge_selector.operational_bridge_count' => ['label' => 'Operational bridges'], + 'bridge_selector.in_shutdown_bridge_count' => ['label' => 'Bridges in shutdown'] + ], + 'System stats' => [ + 'threads' => ['label' => 'Threads'], + 'jibri_detector.count' => ['label' => 'Jibri count'], + 'stress_level' => ['label' => 'Stress level'] + ] + ]; - $widget['records'] = array(); + // Get latest data for all the agents + $agents = ['jvb', 'jicofo', 'jibri', 'prosody', 'nginx']; + $widget['records'] = []; - // Format data for JVB metrics - if ($latestJvbConferences !== null || $latestJvbParticipants !== null) { - $widget['records'][] = [ - 'table_headers' => 'JVB', - 'conferences' => $latestJvbConferences ? $latestJvbConferences['value'] : null, - 'participants' => $latestJvbParticipants ? $latestJvbParticipants['value'] : null, - 'from_time' => $latestJvbConferences ? $latestJvbConferences['timestamp'] : ($latestJvbParticipants ? $latestJvbParticipants['timestamp'] : null), - 'until_time' => $latestJvbConferences ? $latestJvbConferences['timestamp'] : ($latestJvbParticipants ? $latestJvbParticipants['timestamp'] : null) + // Initialize records for each agent + foreach ($agents as $agent) { + $record = [ + 'table_headers' => strtoupper($agent), + 'metrics' => [], + 'timestamp' => null ]; - } - // Format data for Jicofo metrics - if ($latestJicofoConferences !== null || $latestJicofoParticipants !== null) { - $widget['records'][] = [ - 'table_headers' => 'Jicofo', - 'conferences' => $latestJicofoConferences ? $latestJicofoConferences['value'] : null, - 'participants' => $latestJicofoParticipants ? $latestJicofoParticipants['value'] : null, - 'from_time' => $latestJicofoConferences ? $latestJicofoConferences['timestamp'] : ($latestJicofoParticipants ? $latestJicofoParticipants['timestamp'] : null), - 'until_time' => $latestJicofoConferences ? $latestJicofoConferences['timestamp'] : ($latestJicofoParticipants ? $latestJicofoParticipants['timestamp'] : null) - ]; + // Fetch all metrics for this agent + foreach ($metrics as $section => $section_metrics) { + foreach ($section_metrics as $metric => $config) { + $data = $agentObject->getLatestData($platform_id, $agent, $metric); + if ($data !== null) { + $record['metrics'][$section][$metric] = [ + 'value' => $data['value'], + 'label' => $config['label'], + 'link' => isset($config['link']) ? $config['link'] : null + ]; + // Use the most recent timestamp + if ($record['timestamp'] === null || strtotime($data['timestamp']) > strtotime($record['timestamp'])) { + $record['timestamp'] = $data['timestamp']; + } + } + } + } + + if (!empty($record['metrics'])) { + $widget['records'][] = $record; + } } // prepare the widget @@ -101,6 +126,7 @@ switch ($item) { $widget['collapsible'] = false; $widget['collapsed'] = false; $widget['filter'] = false; + $widget['metrics'] = $metrics; // Pass metrics configuration to template if (!empty($widget['records'])) { $widget['full'] = true; } diff --git a/app/templates/latest-data.php b/app/templates/latest-data.php index 02e7fae..3666184 100644 --- a/app/templates/latest-data.php +++ b/app/templates/latest-data.php @@ -11,47 +11,41 @@ Metric - + + + +
+ as of + + + $section_metrics) { ?> + + + + $config) { ?> - Conferences - + + - - - -
- + + - 0 - - + + No data - + - - Participants - - - - - -
- - - 0 - - - No data - - + -