Make batched status history work

This commit is contained in:
Fred KISSIE
2026-07-21 20:29:16 +01:00
parent 23181f4019
commit 70bddba55b
+9 -18
View File
@@ -37,9 +37,9 @@ export async function getCachedStatusHistory(
tzOffsetMinutes tzOffsetMinutes
); );
const cached = await cache.get<StatusHistoryResponse>(cacheKey); const cached = await cache.get<StatusHistoryResponse>(cacheKey);
// if (cached !== undefined) { if (cached !== undefined) {
// return cached; return cached;
// } }
console.time(`[getCachedStatusHistory/${entityType}=${entityId}]`); console.time(`[getCachedStatusHistory/${entityType}=${entityId}]`);
// Anchor to local midnight (UTC when tzOffsetMinutes is 0) so the query // Anchor to local midnight (UTC when tzOffsetMinutes is 0) so the query
@@ -344,10 +344,6 @@ export async function getBatchedStatusHistory(
) )
.orderBy(asc(statusHistory.timestamp)); .orderBy(asc(statusHistory.timestamp));
console.log({
events
});
// Fetch the last known state before the window so that entities that // Fetch the last known state before the window so that entities that
// haven't changed status recently still show the correct status rather // haven't changed status recently still show the correct status rather
// than appearing as "no_data". // than appearing as "no_data".
@@ -371,17 +367,12 @@ export async function getBatchedStatusHistory(
} }
> = {}; > = {};
for (const event of events) { for (const entityId of entityIds) {
if (!eventStatusMap[event.entityId]) { eventStatusMap[entityId] = {
eventStatusMap[event.entityId] = { events: events.filter((ev) => ev.entityId === entityId),
events: [], lastKnownEvent:
lastKnownEvent: lastKnownEvents.find((ev) => ev.entityId === entityId) ?? null
lastKnownEvents.find( };
(ev) => ev.entityId === event.entityId
) ?? null
};
}
eventStatusMap[event.entityId].events.push(event);
} }
const result: BatchedStatusHistoryResponse = {}; const result: BatchedStatusHistoryResponse = {};