feat: improve error management when data are missing
This commit is contained in:
parent
319821ce59
commit
5f15002462
4 changed files with 95 additions and 43 deletions
|
|
@ -1,17 +1,8 @@
|
|||
let chart = null;
|
||||
window.onload = init;
|
||||
document.addEventListener("DOMContentLoaded", init);
|
||||
|
||||
async function init() {
|
||||
let start = new Date();
|
||||
start.setDate(start.getDate() - 7);
|
||||
const end = new Date();
|
||||
function showChartError() {
|
||||
|
||||
document.getElementById("date-start").valueAsDate = start;
|
||||
document.getElementById("date-end").valueAsDate = end;
|
||||
document.getElementById("btn-refresh").addEventListener("click", loadData);
|
||||
|
||||
await loadDevices();
|
||||
await loadData();
|
||||
}
|
||||
|
||||
async function loadData() {
|
||||
|
|
@ -20,18 +11,26 @@ async function loadData() {
|
|||
const granularity = document.getElementById("granularity").value;
|
||||
const device_eui = document.getElementById("device").value;
|
||||
|
||||
const points = await fetchConsumption({
|
||||
device_eui: device_eui,
|
||||
start: start,
|
||||
end: end,
|
||||
granularity: granularity,
|
||||
});
|
||||
if (!device_eui || !start || !end) return;
|
||||
|
||||
if (chart) {
|
||||
chart.destroy();
|
||||
try {
|
||||
const points = await fetchConsumption({
|
||||
device_eui: device_eui,
|
||||
start: start,
|
||||
end: end,
|
||||
granularity: granularity,
|
||||
});
|
||||
|
||||
if (chart) { chart.destroy(); }
|
||||
|
||||
chart = renderChart(points, granularity);
|
||||
updateKPIs(points);
|
||||
|
||||
} catch (err) {
|
||||
if (chart) { chart.destroy(); }
|
||||
showMessage("Impossible de charger les données : " + err.message);
|
||||
updateKPIs(null);
|
||||
}
|
||||
chart = renderChart(points, granularity);
|
||||
updateKPIs(points);
|
||||
}
|
||||
|
||||
function updateKPIs(points) {
|
||||
|
|
@ -40,9 +39,9 @@ function updateKPIs(points) {
|
|||
const countDisplay = document.getElementById("kpi-count");
|
||||
|
||||
if (!points || points.length === 0) {
|
||||
total.textContent = "-";
|
||||
avg.textContent = "-";
|
||||
count.textContent = "-";
|
||||
totalDisplay.textContent = "-";
|
||||
avgDisplay.textContent = "-";
|
||||
countDisplay.textContent = "-";
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -57,11 +56,38 @@ function updateKPIs(points) {
|
|||
async function loadDevices() {
|
||||
const selectDeviceElmt = document.getElementById("device");
|
||||
|
||||
const devices = await fetchDevices();
|
||||
try {
|
||||
const devices = await fetchDevices();
|
||||
if (!devices.length) {
|
||||
selectDeviceElmt.disabled = true;
|
||||
selectDeviceElmt.add(new Option("Aucun appareil disponible", ""));
|
||||
return;
|
||||
}
|
||||
|
||||
devices.forEach((device) => {
|
||||
selectDeviceElmt.add(new Option(device.device_eui))
|
||||
});
|
||||
devices.forEach((device) => {
|
||||
selectDeviceElmt.add(new Option(device.device_eui));
|
||||
});
|
||||
|
||||
selectDeviceElmt.value = devices[0]?.device_eui ?? "";
|
||||
selectDeviceElmt.value = devices[0].device_eui;
|
||||
|
||||
} catch(err) {
|
||||
selectDeviceElmt.disabled = true;
|
||||
selectDeviceElmt.add(new Option("Erreur de chargement", ""));
|
||||
console.error("loadDevices :", err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function init() {
|
||||
let start = new Date();
|
||||
start.setDate(start.getDate() - 7);
|
||||
const end = new Date();
|
||||
|
||||
document.getElementById("date-start").valueAsDate = start;
|
||||
document.getElementById("date-end").valueAsDate = end;
|
||||
document.getElementById("btn-refresh").addEventListener("click", loadData);
|
||||
|
||||
await loadDevices();
|
||||
|
||||
const device_eui = document.getElementById("device").value;
|
||||
if (device_eui) await loadData();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue