mirror of
https://github.com/Roslund/sthlm-mesh.git
synced 2026-08-12 11:53:12 +02:00
fetch nodes only once
This commit is contained in:
@@ -88,6 +88,7 @@ Graferna baseras på data från [map.sthlm-mesh.se](https://map.sthlm-mesh.se),
|
||||
{{% /blocks/section %}}
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||
<script src="/js/status/shared.js"></script>
|
||||
<script src="/js/status/messagesChart.js"></script>
|
||||
<script src="/js/status/most-active-nodes.js"></script>
|
||||
<script src="/js/status/hardwareChart.js"></script>
|
||||
|
||||
@@ -9,14 +9,12 @@ async function deviceRolesChart() {
|
||||
ctx.fillText('Loading data...', canvas.width / 2, canvas.height / 2);
|
||||
|
||||
try {
|
||||
const response = await fetch('https://map.sthlm-mesh.se/api/v1/nodes/');
|
||||
const data = await response.json();
|
||||
|
||||
await fetchNodes();
|
||||
const predefinedLabels = ['CLIENT', 'CLIENT_MUTE', 'CLIENT_HIDDEN', 'ROUTER_LATE', 'ROUTER', 'ROUTER_CLIENT'];
|
||||
|
||||
// Count role occurrences
|
||||
const roleCounts = {};
|
||||
data.nodes.forEach(node => {
|
||||
nodes.forEach(node => {
|
||||
const role = node.role_name || "UNKNOWN";
|
||||
roleCounts[role] = (roleCounts[role] || 0) + 1;
|
||||
});
|
||||
|
||||
@@ -1,21 +1,16 @@
|
||||
function countNodesSince(daysAgo) {
|
||||
const now = new Date();
|
||||
const threshold = new Date(now);
|
||||
threshold.setDate(threshold.getDate() - daysAgo);
|
||||
return nodes.filter(node => new Date(node.updated_at) >= threshold).length;
|
||||
}
|
||||
|
||||
async function nodesSeen() {
|
||||
const response = await fetch('https://map.sthlm-mesh.se/api/v1/nodes/');
|
||||
const data = await response.json();
|
||||
const nodes = data.nodes;
|
||||
const now = new Date();
|
||||
|
||||
|
||||
const countNodesSince = (daysAgo) => {
|
||||
const threshold = new Date(now);
|
||||
threshold.setDate(threshold.getDate() - daysAgo);
|
||||
return nodes.filter(node => new Date(node.updated_at) >= threshold).length;
|
||||
};
|
||||
await fetchNodes();
|
||||
|
||||
document.getElementById("count-30").textContent = countNodesSince(30);
|
||||
document.getElementById("count-7").textContent = countNodesSince(7);
|
||||
document.getElementById("count-1").textContent = countNodesSince(1);
|
||||
|
||||
}
|
||||
|
||||
nodesSeen();
|
||||
nodesSeen();
|
||||
|
||||
@@ -1,20 +1,14 @@
|
||||
let allNodes = [];
|
||||
let highlightedIndex = -1;
|
||||
let chartInstance;
|
||||
|
||||
|
||||
async function fetchNodes() {
|
||||
const res = await fetch('https://map.sthlm-mesh.se/api/v1/nodes/');
|
||||
const data = await res.json();
|
||||
|
||||
const threshold = new Date();
|
||||
threshold.setDate(threshold.getDate() - 1);
|
||||
allNodes = data.nodes.filter(node => new Date(node.updated_at) >= threshold);
|
||||
}
|
||||
|
||||
function filterSuggestions(query) {
|
||||
const lowerQuery = query.toLowerCase();
|
||||
return allNodes?.filter(node => node.long_name?.toLowerCase().includes(lowerQuery)) || [];
|
||||
const threshold = new Date();
|
||||
threshold.setDate(threshold.getDate() - 1);
|
||||
return nodes
|
||||
.filter(node => new Date(node.updated_at) >= threshold)
|
||||
.filter(node => node.long_name?.toLowerCase().includes(lowerQuery)) || [];
|
||||
}
|
||||
|
||||
function showSuggestions(matches) {
|
||||
@@ -178,4 +172,4 @@ async function portnumDistributionChart(nodeId = null) {
|
||||
}
|
||||
}
|
||||
|
||||
fetchNodes().then(() => portnumDistributionChart());
|
||||
portnumDistributionChart();
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
let nodes = [];
|
||||
let fetchPromise = null;
|
||||
|
||||
async function fetchNodes() {
|
||||
if (nodes.length > 0) return;
|
||||
|
||||
if (!fetchPromise) {
|
||||
fetchPromise = fetch('https://map.sthlm-mesh.se/api/v1/nodes/')
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
nodes = data.nodes;
|
||||
fetchPromise = null; // reset after successful fetch
|
||||
})
|
||||
.catch(err => {
|
||||
fetchPromise = null; // reset on failure
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
await fetchPromise;
|
||||
}
|
||||
Reference in New Issue
Block a user