diff --git a/src/cache/Cache.ts b/src/cache/Cache.ts index dcb275e..525d14f 100644 --- a/src/cache/Cache.ts +++ b/src/cache/Cache.ts @@ -25,6 +25,43 @@ async function fetchSingleRange( significant_changes_only: boolean, minimal_response: boolean ): Promise { + // We fetch slightly more than requested (i.e the range visible in the screen). The reason is the following: + // When fetching data in a range `[startT,endT]`, Home Assistant adds a fictitious datapoint at + // the start of the fetched period containing a copy of the first datapoint that occurred before + // `startT`, except if there is actually one at `startT`. + // We fetch slightly more than requested/visible (`[startT-1,endT]`) and we mark the datapoint at + // `startT-1` to be deleted (`fake_boundary_datapoint`). When merging the fetched data into the + // cache, we keep the fictitious datapoint only if it's placed at the start (see `add` function), otherwise it's + // discarded. + // In general, we don't really know whether the datapoint is fictitious or it's a real datapoint + // that happened to be exactly at `startT-1`, therefore we purposely fetch it outside the requested range + // (which is `[startT,endT]`) and we leave it out of the "known cached ranges". + // If it happens to be a a real datapoint, it will be fetched properly when the user scrolls/zooms bring it into + // the visible part of the screen. + // + // Examples: + // + // * = fictitious + // + = real + // _ = fetched range + // + // _________ 1st fetch + // * + + + // ^ + // '-- point kept because it's at the start-edge of the trace and it's outside the visible range + // + // _______ 2nd fetch + // * + * + + + // ^ ^ + // | '--- discarded as it was fictitious and not at the start-edge + // '--- point at the edge, kept + // + // ________ 3rd fetch + // * + + +* + + + // ^ ^ + // | '--- discarded as it is fictitious + // '--- point at the edge, kept + const start = new Date(startT - 1); endT = Math.min(endT, Date.now()); const end = new Date(endT); @@ -41,13 +78,6 @@ async function fetchSingleRange( ); } - /* - home assistant will "invent" a datapoiont at startT with the previous - known value, except if there is actually one at startT. - To avoid these duplicates, the "fetched range" starts at startT-1, - but the first point is marked to be deleted (fake_boundary_datapoint). - Delettion occurs when merging the fetched range inside the cached history. - */ let range: [number, number] = [startT, endT]; if (history.length) { history[0].fake_boundary_datapoint = true;