This commit is contained in:
sebseb7
2025-12-23 06:59:23 +01:00
parent 1b56e2cc42
commit eec45ee379
5 changed files with 105 additions and 40 deletions

View File

@@ -1218,30 +1218,43 @@ app.get('/api/history', (req, res) => {
break;
}
// Calculate time modifiers using offset in seconds
let durationSec;
if (range === 'week') durationSec = 7 * 24 * 3600;
else if (range === 'month') durationSec = 30 * 24 * 3600;
else durationSec = 24 * 3600; // day
// Calculate absolute start/end times (in seconds)
const nowMs = Date.now();
let startTsSec, endTsSec;
const endOffsetSec = off * durationSec;
const startOffsetSec = (off + 1) * durationSec;
if (range === 'today') {
// align to midnight local time
const d = new Date(nowMs);
d.setHours(0, 0, 0, 0);
const midnightMs = d.getTime();
const endMod = `-${endOffsetSec} seconds`;
const startMod = `-${startOffsetSec} seconds`;
// "1 Tag" = 24h aligned to midnight
startTsSec = (midnightMs / 1000) - (off * 24 * 3600);
endTsSec = startTsSec + (24 * 3600);
} else {
// Rolling window
let durationSec;
if (range === 'week') durationSec = 7 * 24 * 3600;
else if (range === 'month') durationSec = 30 * 24 * 3600;
else durationSec = 24 * 3600; // day
const endMs = nowMs - (off * durationSec * 1000);
const startMs = endMs - (durationSec * 1000);
startTsSec = Math.floor(startMs / 1000);
endTsSec = Math.floor(endMs / 1000);
}
// Select raw data
// Select raw data
const stmt = db.prepare(`
SELECT strftime('%s', timestamp) as ts, temp_c, humidity, fan_speed
FROM readings
WHERE dev_name = ? AND port = ?
AND timestamp >= datetime('now', ?)
AND timestamp < datetime('now', ?)
AND timestamp >= datetime(?, 'unixepoch')
AND timestamp < datetime(?, 'unixepoch')
ORDER BY timestamp ASC
`);
const rows = stmt.all(devName, parseInt(port, 10), startMod, endMod);
const rows = stmt.all(devName, parseInt(port, 10), startTsSec, endTsSec);
if (rows.length === 0) return res.json({ start: 0, step: bucketSize, temps: [], hums: [], levels: [] });
@@ -1320,27 +1333,38 @@ app.get('/api/outputs/history', (req, res) => {
const { range, offset = 0 } = req.query;
const off = parseInt(offset, 10) || 0;
// Calculate duration in seconds
let durationSec;
if (range === 'week') durationSec = 7 * 24 * 3600;
else if (range === 'month') durationSec = 30 * 24 * 3600;
else durationSec = 24 * 3600; // day
// Calculate absolute start/end times (in seconds)
const nowMs = Date.now();
let startTsSec, endTsSec;
const endOffsetSec = off * durationSec;
const startOffsetSec = (off + 1) * durationSec;
if (range === 'today') {
const d = new Date(nowMs);
d.setHours(0, 0, 0, 0);
const midnightMs = d.getTime();
startTsSec = (midnightMs / 1000) - (off * 24 * 3600);
endTsSec = startTsSec + (24 * 3600);
} else {
let durationSec;
if (range === 'week') durationSec = 7 * 24 * 3600;
else if (range === 'month') durationSec = 30 * 24 * 3600;
else durationSec = 24 * 3600; // day
const endMod = `-${endOffsetSec} seconds`;
const startMod = `-${startOffsetSec} seconds`;
const endMs = nowMs - (off * durationSec * 1000);
const startMs = endMs - (durationSec * 1000);
startTsSec = Math.floor(startMs / 1000);
endTsSec = Math.floor(endMs / 1000);
}
const stmt = db.prepare(`
SELECT timestamp, dev_name, port, state, level
FROM output_log
WHERE timestamp >= datetime('now', ?)
AND timestamp < datetime('now', ?)
WHERE timestamp >= datetime(?, 'unixepoch')
AND timestamp < datetime(?, 'unixepoch')
ORDER BY timestamp ASC
`);
const rows = stmt.all(startMod, endMod);
const rows = stmt.all(startTsSec, endTsSec);
// Compress: Group by "Dev:Port" -> [[ts, state, level], ...]
const compressed = {};