Makes latest data configurable

main
Yasen Pramatarov 2025-01-14 14:09:28 +02:00
parent 5986993e45
commit d3f65939cd
3 changed files with 140 additions and 66 deletions

View File

@ -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.
*
@ -421,20 +443,52 @@ class Agent {
// 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;

View File

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

View File

@ -11,47 +11,41 @@
<tr>
<th scope="col">Metric</th>
<?php foreach ($widget['records'] as $record) { ?>
<th scope="col"><?= htmlspecialchars($record['table_headers']) ?></th>
<th scope="col">
<?= htmlspecialchars($record['table_headers']) ?>
<?php if ($record['timestamp']) { ?>
<br>
<small class="text-muted">as of <?= date('Y-m-d H:i:s', strtotime($record['timestamp'])) ?></small>
<?php } ?>
</th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach ($widget['metrics'] as $section => $section_metrics) { ?>
<tr class="table-secondary">
<th colspan="<?= count($widget['records']) + 1 ?>"><?= htmlspecialchars($section) ?></th>
</tr>
<?php foreach ($section_metrics as $metric => $config) { ?>
<tr>
<td>Conferences</td>
<?php foreach ($widget['records'] as $record) { ?>
<td><?= htmlspecialchars($config['label']) ?></td>
<?php foreach ($widget['records'] as $record) { ?>
<td>
<?php if (isset($record['conferences'])) { ?>
<?php if ($record['conferences'] !== null) { ?>
<a href="<?= htmlspecialchars($app_root) ?>?platform=<?= htmlspecialchars($platform_id) ?>&page=conferences&from_time=<?= htmlspecialchars($record['from_time']) ?>&until_time=<?= htmlspecialchars($record['until_time']) ?>"><?= htmlspecialchars($record['conferences']) ?></a>
<br>
<small class="text-muted"><?= date('Y-m-d H:i:s', strtotime($record['from_time'])) ?></small>
<?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 } else { ?>
<span class="text-muted">0</span>
<?php } ?>
<?php } else { ?>
<?= htmlspecialchars($metric_data['value']) ?>
<?php }
} else { ?>
<span class="text-muted">No data</span>
<?php } ?>
</td>
<?php } ?>
<?php } ?>
</tr>
<tr>
<td>Participants</td>
<?php foreach ($widget['records'] as $record) { ?>
<td>
<?php if (isset($record['participants'])) { ?>
<?php if ($record['participants'] !== null) { ?>
<a href="<?= htmlspecialchars($app_root) ?>?platform=<?= htmlspecialchars($platform_id) ?>&page=participants&from_time=<?= htmlspecialchars($record['from_time']) ?>&until_time=<?= htmlspecialchars($record['until_time']) ?>"><?= htmlspecialchars($record['participants']) ?></a>
<br>
<small class="text-muted"><?= date('Y-m-d H:i:s', strtotime($record['from_time'])) ?></small>
<?php } else { ?>
<span class="text-muted">0</span>
<?php } ?>
<?php } else { ?>
<span class="text-muted">No data</span>
<?php } ?>
</td>
<?php } ?>
<?php } ?>
</tr>
</tbody>
</table>
<?php } else { ?>