add web ui

This commit is contained in:
2026-01-27 16:51:58 +11:00
parent 7526a7af93
commit 57a3a29914
12 changed files with 1158 additions and 23 deletions

352
cmd/ingestd/web/app.js Normal file
View File

@@ -0,0 +1,352 @@
const state = {
range: "24h",
bucket: "5m",
charts: {},
timer: null,
};
const colors = {
obs: "#7bdff2",
forecast: "#f4b942",
gust: "#ff7d6b",
humidity: "#7ee081",
uvi: "#f4d35e",
light: "#b8f2e6",
precip: "#4ea8de",
};
function formatNumber(value, digits) {
if (value === null || value === undefined) {
return "--";
}
return Number(value).toFixed(digits);
}
function formatTime(iso) {
if (!iso) return "--";
const dt = new Date(iso);
if (Number.isNaN(dt.getTime())) return "--";
return dt.toLocaleString();
}
function series(points, key) {
return points.map((p) => ({
x: p.ts,
y: p[key] === undefined ? null : p[key],
}));
}
function minMax(values) {
let min = null;
let max = null;
for (const v of values) {
if (v === null || v === undefined) continue;
if (min === null || v < min) min = v;
if (max === null || v > max) max = v;
}
return { min, max };
}
function sum(values) {
let total = 0;
let seen = false;
for (const v of values) {
if (v === null || v === undefined) continue;
total += v;
seen = true;
}
return seen ? total : null;
}
function updateText(id, text) {
const el = document.getElementById(id);
if (el) el.textContent = text;
}
function upsertChart(id, config) {
const ctx = document.getElementById(id);
if (!ctx) return;
if (state.charts[id]) {
state.charts[id].data = config.data;
state.charts[id].options = config.options;
state.charts[id].update();
return;
}
state.charts[id] = new Chart(ctx, config);
}
function baseOptions() {
return {
responsive: true,
maintainAspectRatio: false,
interaction: { mode: "index", intersect: false },
plugins: {
legend: { labels: { color: "#d6f0f0" } },
tooltip: { mode: "index", intersect: false },
},
scales: {
x: {
type: "time",
time: { unit: "hour" },
ticks: { color: "#a4c4c4", maxTicksLimit: 6 },
grid: { color: "rgba(123, 223, 242, 0.08)" },
},
y: {
ticks: { color: "#a4c4c4" },
grid: { color: "rgba(123, 223, 242, 0.08)" },
},
},
elements: {
line: { tension: 0.2, borderWidth: 2 },
point: { radius: 0, hitRadius: 8 },
},
spanGaps: true,
};
}
function renderDashboard(data) {
const latest = data.latest;
updateText("site-meta", `${data.site} | model ${data.model}`);
updateText("last-updated", `updated ${formatTime(data.generated_at)}`);
const forecastMeta = data.forecast && data.forecast.points && data.forecast.points.length
? `forecast retrieved ${formatTime(data.forecast.retrieved_at)}`
: "forecast not available";
updateText("forecast-meta", forecastMeta);
if (latest) {
updateText("live-temp", `${formatNumber(latest.temp_c, 2)} C`);
updateText("live-rh", `${formatNumber(latest.rh, 0)} %`);
updateText("live-wind", `${formatNumber(latest.wind_m_s, 2)}`);
updateText("live-gust", `${formatNumber(latest.wind_gust_m_s, 2)}`);
updateText("live-wdir", `${formatNumber(latest.wind_dir_deg, 0)}`);
updateText("live-uvi", `${formatNumber(latest.uvi, 2)}`);
updateText("live-lux", `${formatNumber(latest.light_lux, 0)}`);
} else {
updateText("live-temp", "--");
updateText("live-rh", "--");
updateText("live-wind", "--");
updateText("live-gust", "--");
updateText("live-wdir", "--");
updateText("live-uvi", "--");
updateText("live-lux", "--");
}
const forecastUrl = document.getElementById("forecast-url");
if (forecastUrl) {
if (data.open_meteo_url) {
forecastUrl.href = data.open_meteo_url;
forecastUrl.textContent = data.open_meteo_url;
} else {
forecastUrl.textContent = "--";
}
}
const obs = data.observations || [];
const forecast = (data.forecast && data.forecast.points) || [];
const obsTemps = obs.map((p) => p.temp_c);
const obsWinds = obs.map((p) => p.wind_m_s);
const obsGusts = obs.map((p) => p.wind_gust_m_s);
const obsRH = obs.map((p) => p.rh);
const obsUvi = obs.map((p) => p.uvi);
const obsLux = obs.map((p) => p.light_lux);
const fcTemps = forecast.map((p) => p.temp_c);
const fcWinds = forecast.map((p) => p.wind_m_s);
const fcGusts = forecast.map((p) => p.wind_gust_m_s);
const fcRH = forecast.map((p) => p.rh);
const fcPrecip = forecast.map((p) => p.precip_mm);
const obsTempSummary = minMax(obsTemps);
const obsWindSummary = minMax(obsWinds);
const obsUviSummary = minMax(obsUvi);
const obsLuxSummary = minMax(obsLux);
const forecastTempSummary = minMax(fcTemps);
const forecastPrecipTotal = sum(fcPrecip);
const obsParts = [];
if (obsTempSummary.min !== null) {
obsParts.push(`temp_c ${obsTempSummary.min.toFixed(1)} to ${obsTempSummary.max.toFixed(1)}`);
}
if (obsWindSummary.max !== null) {
obsParts.push(`wind_max ${obsWindSummary.max.toFixed(1)} m/s`);
}
if (obsUviSummary.max !== null) {
obsParts.push(`uvi_max ${obsUviSummary.max.toFixed(1)}`);
}
if (obsLuxSummary.max !== null) {
obsParts.push(`lux_max ${obsLuxSummary.max.toFixed(0)}`);
}
updateText("obs-summary", obsParts.length ? obsParts.join(" | ") : "--");
const forecastParts = [];
if (forecastTempSummary.min !== null) {
forecastParts.push(`temp_c ${forecastTempSummary.min.toFixed(1)} to ${forecastTempSummary.max.toFixed(1)}`);
}
if (forecastPrecipTotal !== null) {
forecastParts.push(`precip_total ${forecastPrecipTotal.toFixed(1)} mm`);
}
updateText("forecast-summary", forecastParts.length ? forecastParts.join(" | ") : "--");
updateText("commentary", buildCommentary(latest, forecast));
const tempChart = {
type: "line",
data: {
datasets: [
{ label: "obs temp C", data: series(obs, "temp_c"), borderColor: colors.obs },
{ label: "forecast temp C", data: series(forecast, "temp_c"), borderColor: colors.forecast },
],
},
options: baseOptions(),
};
upsertChart("chart-temp", tempChart);
const windChart = {
type: "line",
data: {
datasets: [
{ label: "obs wind m/s", data: series(obs, "wind_m_s"), borderColor: colors.obs },
{ label: "obs gust m/s", data: series(obs, "wind_gust_m_s"), borderColor: colors.gust },
{ label: "forecast wind m/s", data: series(forecast, "wind_m_s"), borderColor: colors.forecast },
{ label: "forecast gust m/s", data: series(forecast, "wind_gust_m_s"), borderColor: "#f7d79f" },
],
},
options: baseOptions(),
};
upsertChart("chart-wind", windChart);
const rhChart = {
type: "line",
data: {
datasets: [
{ label: "obs humidity %", data: series(obs, "rh"), borderColor: colors.humidity },
{ label: "forecast rh %", data: series(forecast, "rh"), borderColor: colors.forecast },
],
},
options: baseOptions(),
};
upsertChart("chart-rh", rhChart);
const lightOptions = baseOptions();
lightOptions.scales.y1 = {
position: "right",
ticks: { color: "#a4c4c4" },
grid: { drawOnChartArea: false },
};
const lightChart = {
type: "line",
data: {
datasets: [
{ label: "uvi", data: series(obs, "uvi"), borderColor: colors.uvi, yAxisID: "y" },
{ label: "light lux", data: series(obs, "light_lux"), borderColor: colors.light, yAxisID: "y1" },
],
},
options: lightOptions,
};
upsertChart("chart-light", lightChart);
const precipChart = {
type: "bar",
data: {
datasets: [
{ label: "forecast precip mm", data: series(forecast, "precip_mm"), backgroundColor: colors.precip },
],
},
options: baseOptions(),
};
upsertChart("chart-precip", precipChart);
}
function buildCommentary(latest, forecast) {
if (!latest || !forecast || !forecast.length) {
return "Waiting for forecast data...";
}
const obsTs = new Date(latest.ts);
if (Number.isNaN(obsTs.getTime())) {
return "No valid observation timestamp yet.";
}
let nearest = null;
let bestDiff = null;
for (const p of forecast) {
if (!p.ts) continue;
const fcTs = new Date(p.ts);
if (Number.isNaN(fcTs.getTime())) continue;
const diff = Math.abs(obsTs - fcTs);
if (bestDiff === null || diff < bestDiff) {
bestDiff = diff;
nearest = p;
}
}
if (!nearest || bestDiff > 2 * 60 * 60 * 1000) {
return "No nearby forecast point to compare yet.";
}
const parts = [];
if (latest.temp_c !== null && nearest.temp_c !== null) {
const delta = latest.temp_c - nearest.temp_c;
parts.push(`temp ${delta >= 0 ? "+" : ""}${delta.toFixed(1)} C`);
}
if (latest.wind_m_s !== null && nearest.wind_m_s !== null) {
const delta = latest.wind_m_s - nearest.wind_m_s;
parts.push(`wind ${delta >= 0 ? "+" : ""}${delta.toFixed(1)} m/s`);
}
if (latest.wind_gust_m_s !== null && nearest.wind_gust_m_s !== null) {
const delta = latest.wind_gust_m_s - nearest.wind_gust_m_s;
parts.push(`gust ${delta >= 0 ? "+" : ""}${delta.toFixed(1)} m/s`);
}
if (latest.rh !== null && nearest.rh !== null) {
const delta = latest.rh - nearest.rh;
parts.push(`rh ${delta >= 0 ? "+" : ""}${delta.toFixed(0)} %`);
}
if (!parts.length) {
return "Not enough data to compute deviation yet.";
}
return `Now vs forecast: ${parts.join(", ")}`;
}
async function loadAndRender() {
const params = new URLSearchParams({
range: state.range,
bucket: state.bucket,
});
try {
const resp = await fetch(`/api/dashboard?${params.toString()}`, { cache: "no-store" });
if (!resp.ok) {
updateText("commentary", `Dashboard error ${resp.status}`);
return;
}
const data = await resp.json();
renderDashboard(data);
} catch (err) {
updateText("commentary", "Dashboard fetch failed.");
}
}
function setupControls() {
const buttons = document.querySelectorAll(".btn[data-range]");
buttons.forEach((btn) => {
btn.addEventListener("click", () => {
buttons.forEach((b) => b.classList.remove("active"));
btn.classList.add("active");
state.range = btn.dataset.range;
state.bucket = state.range === "6h" ? "1m" : "5m";
loadAndRender();
});
});
}
document.addEventListener("DOMContentLoaded", () => {
setupControls();
loadAndRender();
if (state.timer) clearInterval(state.timer);
state.timer = setInterval(loadAndRender, 60 * 1000);
});