Updates latest data page functionality

main
Yasen Pramatarov 2025-01-28 15:27:40 +02:00
parent a288d311c0
commit 779d3e0bf6
3 changed files with 231 additions and 84 deletions

View File

@ -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;
}
}
?>

View File

@ -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,14 +135,26 @@ 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),
// 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' => []
];
// 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
];
@ -149,39 +162,47 @@ if ($response['db'] === null) {
// 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'],
// 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
if ($record['timestamp'] === null || strtotime($data['timestamp']) > strtotime($record['timestamp'])) {
$record['timestamp'] = $data['timestamp'];
// Use the most recent timestamp for the agent
if ($agentData['timestamp'] === null || strtotime($latestData['timestamp']) > strtotime($agentData['timestamp'])) {
$agentData['timestamp'] = $latestData['timestamp'];
}
}
}
}
if (!empty($record['metrics'])) {
$widget['records'][] = $record;
if (!empty($agentData['metrics'])) {
$hostData['agents'][] = $agentData;
}
}
// 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;
if (!empty($hostData['agents'])) {
$hostsData[] = $hostData;
}
}
$widget['pagination'] = false;
// Load the template
include '../app/templates/latest-data.php';
break;

View File

@ -1,53 +1,111 @@
<div class="row">
<div class="card w-auto bg-light border-light card-body" style="flex-direction: row;"><?= $widget['title'] ?></div>
<div class="card w-auto bg-light border-light card-body" style="flex-direction: row;">Latest data from Jilo Agents</div>
</div>
<div class="collapse show" id="collapse<?= htmlspecialchars($widget['name']) ?>">
<div class="collapse show">
<div class="mb-5">
<?php if ($widget['full'] === true) { ?>
<?php if (!empty($hostsData)) { ?>
<?php foreach ($hostsData as $host) { ?>
<div class="card mb-4">
<div class="card-header">
<h5 class="mb-0">
<i class="fas fa-network-wired me-2"></i>
<?= htmlspecialchars($host['name']) ?>
<small class="text-muted">(<?= htmlspecialchars($host['address']) ?>)</small>
</h5>
</div>
<div class="card-body">
<?php foreach ($host['agents'] as $agent) { ?>
<div class="mb-4">
<h6 class="border-bottom pb-2">
<i class="fas fa-robot me-2"></i>
<?= htmlspecialchars($agent['name']) ?> agent
</h6>
<table class="table table-results table-striped table-hover table-bordered">
<thead class="thead-dark">
<thead class="align-top">
<tr>
<th scope="col">Metric</th>
<?php foreach ($widget['records'] as $record) { ?>
<th scope="col">
<?= htmlspecialchars($record['table_headers']) ?>
<?php if ($record['timestamp']) { ?>
<th>Metric</th>
<th>
Latest value
<br>
<small class="text-muted">as of <?= date('Y-m-d H:i:s', strtotime($record['timestamp'])) ?></small>
<small class="text-muted">
<?= date('d M Y H:i:s', strtotime($agent['timestamp'])) ?>
</small>
</th>
<th>
Previous value
<?php
// Find first metric with previous data to get timestamp
$prevTimestamp = null;
foreach ($metrics as $m_section => $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) { ?>
<br>
<small class="text-muted">
<?= date('d M Y H:i:s', strtotime($prevTimestamp)) ?>
</small>
<?php } ?>
</th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach ($widget['metrics'] as $section => $section_metrics) { ?>
<?php foreach ($metrics as $section => $section_metrics) { ?>
<?php
// Check if this section has any data for this agent
$hasData = false;
foreach ($section_metrics as $metric => $metricConfig) {
if (isset($agent['metrics'][$section][$metric])) {
$hasData = true;
break;
}
}
if (!$hasData) continue;
?>
<tr class="table-secondary">
<th colspan="<?= count($widget['records']) + 1 ?>"><?= htmlspecialchars($section) ?></th>
<th colspan="3"><?= htmlspecialchars($section) ?></th>
</tr>
<?php foreach ($section_metrics as $metric => $metricConfig) { ?>
<?php if (isset($agent['metrics'][$section][$metric])) {
$metric_data = $agent['metrics'][$section][$metric];
?>
<tr>
<td><?= htmlspecialchars($metricConfig['label']) ?></td>
<?php foreach ($widget['records'] as $record) { ?>
<td>
<?php if (isset($record['metrics'][$section][$metric])) {
$metric_data = $record['metrics'][$section][$metric];
if ($metric_data['link']) { ?>
<a href="<?= htmlspecialchars($app_root) ?>?platform=<?= htmlspecialchars($platform_id) ?>&page=<?= htmlspecialchars($metric_data['link']) ?>&from_time=<?= htmlspecialchars($record['timestamp']) ?>&until_time=<?= htmlspecialchars($record['timestamp']) ?>"><?= htmlspecialchars($metric_data['value']) ?></a>
<?php if ($metric_data['link']) { ?>
<a href="<?= htmlspecialchars($app_root) ?>?platform=<?= htmlspecialchars($platform_id) ?>&page=<?= htmlspecialchars($metric_data['link']) ?>&from_time=<?= htmlspecialchars($metric_data['latest']['timestamp']) ?>&until_time=<?= htmlspecialchars($metric_data['latest']['timestamp']) ?>"><?= htmlspecialchars($metric_data['latest']['value']) ?></a>
<?php } else { ?>
<?= htmlspecialchars($metric_data['value']) ?>
<?php }
} else { ?>
<span class="text-muted">No data</span>
<?= htmlspecialchars($metric_data['latest']['value']) ?>
<?php } ?>
</td>
<td>
<?php if ($metric_data['previous']) { ?>
<?php if ($metric_data['link']) { ?>
<a href="<?= htmlspecialchars($app_root) ?>?platform=<?= htmlspecialchars($platform_id) ?>&page=<?= htmlspecialchars($metric_data['link']) ?>&from_time=<?= htmlspecialchars($metric_data['previous']['timestamp']) ?>&until_time=<?= htmlspecialchars($metric_data['previous']['timestamp']) ?>"><?= htmlspecialchars($metric_data['previous']['value']) ?></a>
<?php } else { ?>
<?= htmlspecialchars($metric_data['previous']['value']) ?>
<?php } ?>
<?php } else { ?>
<span class="text-muted">No previous data</span>
<?php } ?>
</td>
</tr>
<?php } ?>
<?php } ?>
<?php } ?>
</tbody>
</table>
</div>
<?php } ?>
</div>
</div>
<?php } ?>
<?php } else { ?>
<div class="alert alert-info m-3" role="alert">
No data available from any agents. Please check agent configuration and connectivity.
@ -55,4 +113,4 @@
<?php } ?>
</div>
</div>
<!-- /widget "<?= htmlspecialchars($widget['name']) ?>" -->
<!-- /latest data -->