Update public/scripts/trends.js
This commit is contained in:
parent
c9e16ea0b0
commit
bf8728cfd8
@ -10,16 +10,18 @@ const tfConfig = [
|
||||
];
|
||||
|
||||
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
||||
function formatEST(epoch) {
|
||||
return new Date(epoch).toLocaleString('en-US', {
|
||||
timeZone: 'America/New_York',
|
||||
month: 'numeric',
|
||||
day: 'numeric',
|
||||
year: '2-digit',
|
||||
hour12: false,
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
}).replace(',', ' @');
|
||||
function formatESTTimestamp(ts) {
|
||||
// ts is "YYYY-MM-DD HH:mm:ss" in Eastern time
|
||||
const [date, time] = ts.split(' ');
|
||||
const [Y, M, D] = date.split('-').map(Number);
|
||||
const [h, m] = time.split(':');
|
||||
return `${M}/${D}/${String(Y).slice(-2)} @${h.padStart(2,'0')}:${m}`;
|
||||
}
|
||||
function parseLocalDate(ts) {
|
||||
const [date, time] = ts.split(' ');
|
||||
const [Y, M, D] = date.split('-').map(Number);
|
||||
const [h, m, s] = time.split(':').map(Number);
|
||||
return new Date(Y, M-1, D, h, m, s);
|
||||
}
|
||||
function subtract(date, count, unit) {
|
||||
const d = new Date(date);
|
||||
@ -37,16 +39,23 @@ function subtract(date, count, unit) {
|
||||
let readings = [];
|
||||
let chart;
|
||||
|
||||
// ─── On Load ─────────────────────────────────────────────────────────────────
|
||||
// ─── Init ─────────────────────────────────────────────────────────────────────
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
await loadReadings();
|
||||
setupUI();
|
||||
initChart();
|
||||
updateView();
|
||||
});
|
||||
|
||||
// ─── Load readings ────────────────────────────────────────────────────────────
|
||||
async function loadReadings() {
|
||||
const data = await fetch('/api/readings').then(r=>r.json());
|
||||
readings = data.map(r => ({
|
||||
...r,
|
||||
ts: formatEST(r.epoch_ms),
|
||||
date: new Date(r.epoch_ms)
|
||||
formattedTs: formatESTTimestamp(r.timestamp),
|
||||
dateObj: parseLocalDate(r.timestamp)
|
||||
}));
|
||||
setupUI(); initChart(); updateView();
|
||||
});
|
||||
}
|
||||
|
||||
// ─── UI Wiring ────────────────────────────────────────────────────────────────
|
||||
function setupUI() {
|
||||
@ -58,7 +67,9 @@ function setupUI(){
|
||||
});
|
||||
label.textContent = tfConfig[slider.value].label;
|
||||
|
||||
document.getElementsByName('metric').forEach(cb=>cb.addEventListener('change',updateView));
|
||||
document.getElementsByName('metric').forEach(cb=>{
|
||||
cb.addEventListener('change', updateView);
|
||||
});
|
||||
|
||||
document.querySelectorAll('#trends-table thead th.sortable').forEach(th=>{
|
||||
th.addEventListener('click', ()=>{
|
||||
@ -77,7 +88,7 @@ function setupUI(){
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Chart.js Setup ──────────────────────────────────────────────────────────
|
||||
// ─── Chart Setup ──────────────────────────────────────────────────────────────
|
||||
function initChart() {
|
||||
const ctx = document.getElementById('trend-chart').getContext('2d');
|
||||
chart = new Chart(ctx, {
|
||||
@ -99,31 +110,40 @@ function initChart(){
|
||||
function updateView() {
|
||||
const idx = +document.getElementById('timeframe-slider').value;
|
||||
const { unit, count } = tfConfig[idx];
|
||||
const cutoff = subtract(Date.now(),count,unit);
|
||||
const cutoff = subtract(new Date(), count, unit);
|
||||
|
||||
const filtered = readings.filter(r=>r.dateObj >= cutoff);
|
||||
|
||||
const filtered = readings.filter(r=>r.date>=cutoff);
|
||||
const selected = Array.from(document.getElementsByName('metric'))
|
||||
.filter(cb=>cb.checked).map(cb=>cb.value);
|
||||
|
||||
const labels = filtered.map(r=>r.ts);
|
||||
const labels = filtered.map(r=>r.formattedTs);
|
||||
const datasets = [];
|
||||
if (selected.includes('temperature'))
|
||||
if (selected.includes('temperature')) {
|
||||
datasets.push({ label:'Temperature (°F)', data:filtered.map(r=>r.temperature), tension:0.3 });
|
||||
if (selected.includes('humidity'))
|
||||
}
|
||||
if (selected.includes('humidity')) {
|
||||
datasets.push({ label:'Humidity (%)', data:filtered.map(r=>r.humidity), tension:0.3 });
|
||||
if (selected.includes('heatIndex'))
|
||||
}
|
||||
if (selected.includes('heatIndex')) {
|
||||
datasets.push({ label:'Heat Index (°F)', data:filtered.map(r=>r.heatIndex), tension:0.3 });
|
||||
}
|
||||
|
||||
chart.data.labels = labels;
|
||||
chart.data.datasets = datasets;
|
||||
chart.update();
|
||||
|
||||
populateTable(filtered);
|
||||
}
|
||||
|
||||
// ─── Table ───────────────────────────────────────────────────────────────────
|
||||
function populateTable(data) {
|
||||
const tbody = document.getElementById('trends-table-body');
|
||||
tbody.innerHTML = '';
|
||||
filtered.forEach(r=>{
|
||||
data.forEach(r=>{
|
||||
const tr = document.createElement('tr');
|
||||
tr.innerHTML = `
|
||||
<td>${r.ts}</td>
|
||||
<td>${r.formattedTs}</td>
|
||||
<td>${r.temperature.toFixed(1)}</td>
|
||||
<td>${r.humidity.toFixed(1)}</td>
|
||||
<td>${r.heatIndex.toFixed(2)}</td>
|
||||
|
Loading…
x
Reference in New Issue
Block a user