Skip to content

Commit f746899

Browse files
gskjoldclaude
andauthored
Refresh prices every price point, not once a day (#1257)
* Refresh prices every price point, not once a day The price plot labels every bar from the live browser clock, refreshed every 15 minutes, but reads the array starting at the cursor in the last importprice.json payload. Those two clocks drift apart, because the prices are only refetched when the current price changes, or once a day: priceFetchTimeout = setTimeout(getPrices, ((24-date.getHours())*3600000)+10) For an hour of staleness every bar shows the price of the hour before its label, so a modifier configured to start at 23 appears to start at 00. Users on a spot price never see it, since the hourly change of the current price refetches the payload for them. On a fixed price the current price only changes when a modifier does, so the plot drifts a bar per hour for as long as the page stays open, and keeps showing yesterday's array for up to 59 minutes past midnight. Rearm on the next price point instead, the way getDayPlot() already rearms on the next hour. Reported in #1255 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Advance the price cursor by the points passed since the fetch Refreshing on every price point keeps the cursor current, but only while the device answers. A failed or delayed fetch puts the plot back to labelling from the live clock while indexing from an old cursor, and it mislabels silently. Stamp the payload when it arrives and skip the points that have passed since, so a stale payload draws the bars it still has under the right labels and then runs out, rather than drawing the wrong ones. Price points start on the whole hour in the price zone, which is a whole number of hours from UTC, so counting them off epoch boundaries needs no timezone handling. Reported in #1255 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 4d5c80c commit f746899

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

ui/dist/index.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/src/lib/DataStores.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,22 @@ export async function getPrices() {
123123
{
124124
const response = await fetchWithTimeout("importprice.json");
125125
importPrices = (await response.json())
126+
importPrices.fetched = Date.now();
126127
importPricesStore.set(importPrices);
127128
}
128129

129130
if(importPrices?.importExportPriceDifferent) {
130131
const response = await fetchWithTimeout("exportprice.json");
131132
exportprices = (await response.json())
133+
exportprices.fetched = Date.now();
132134
exportPricesStore.set(exportprices);
133135
}
134136

137+
// The plot labels every bar from the live clock, but reads the array from
138+
// the cursor in this payload, so it must be refreshed every price point
135139
let date = new Date();
136-
priceFetchTimeout = setTimeout(getPrices, ((24-date.getHours())*3600000)+10)
140+
let resolution = importPrices?.resolution ? importPrices.resolution : 60;
141+
priceFetchTimeout = setTimeout(getPrices, ((resolution-(date.getMinutes()%resolution))*60000)+10)
137142
}
138143

139144
let dayPlot = {};

ui/src/lib/PricePlot.svelte

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
min = max = 0;
4343
addHours(cur, sysinfo.clock_offset - ((24 + cur.getHours() - cur.getUTCHours())%24));
4444
let i = json?.cursor ? json.cursor : 0;
45+
if(json?.fetched && json?.resolution) {
46+
// The cursor is from when the payload was fetched, the labels
47+
// are from now, so skip the points passed since. Price points
48+
// start on the whole hour in the price zone, which is a whole
49+
// number of hours from UTC, so epoch boundaries line up
50+
let pointMs = json.resolution * 60000;
51+
i += Math.floor(Date.now()/pointMs) - Math.floor(json.fetched/pointMs);
52+
}
4553
cur.setMinutes(Math.floor(cur.getMinutes()/json?.resolution)*json?.resolution,0,0);
4654
while(i < json?.prices?.length) {
4755
val = json.prices[i];

0 commit comments

Comments
 (0)