Leave the overnight gap out of the Timing panel

This commit is contained in:
Alexander Heldt committed 2026-09-29 20:34:36 +00:00
1 parent 15211e32b0
commit 50947d077f
3 files changed
+23 -10

No files matched your search

+21 -9
View File
@@ -1041,13 +1041,18 @@
// "longest" figure far worse than the sparse day this feature exists to
// take out of the numbers.
if (spansExcluded(times[i - 1], times[i])) continue;
// The gap from a day's last event to the next day's first is the night,
// not how often he needs to go: it measures bedtime and a lie-in, and at
// eight-plus hours a night it made "longest" a sleep figure and pulled
// the typical gap up with it. Only gaps within one day are counted.
if (ymd(new Date(times[i - 1])) !== ymd(new Date(times[i]))) continue;
gaps.push(times[i] - times[i - 1]);
}
return gaps.sort((a, b) => a - b);
}
// Median is used for the "typical" gap because it ignores the single long
// overnight gap each day, so it reflects real daytime frequency.
// Median is used for the "typical" gap so that a single unusually long
// stretch — a long nap, a day out — doesn't drag it the way a mean would.
function median(sorted) {
if (sorted.length === 0) return null;
const mid = Math.floor(sorted.length / 2);
@@ -1072,11 +1077,11 @@
if (!svg || !note) continue;
const gaps = gapsBetween(events, row.type);
const last = lastEventOfType(events, row.type);
drawTimingChart(svg, note, row, gaps, last ? Math.max(0, Date.now() - last.at) : null);
drawTimingChart(svg, note, row, gaps, last ? Math.max(0, Date.now() - last.at) : null, events);
}
}
function drawTimingChart(svg, note, row, gaps, since) {
function drawTimingChart(svg, note, row, gaps, since, events) {
const typical = median(gaps);
const shortest = gaps[0] ?? null;
const longest = gaps[gaps.length - 1] ?? null;
@@ -1087,9 +1092,16 @@
svg.innerHTML = "";
svg.style.display = "none";
note.hidden = false;
// Several logged but none sharing a day (one meal a day, say) is a
// different case from one logged: every gap there was overnight.
const cutoff = startOfDay(new Date());
cutoff.setDate(cutoff.getDate() - (chartDays() - 1));
const inWindow = events.filter(e => e.type === row.type && e.at >= cutoff.getTime()).length;
note.textContent = since == null
? `No ${row.noun}s logged in the last ${chartDays()} days.`
: `One ${row.noun} logged, ${formatDuration(since)} ago — log another to see the typical gap.`;
: inWindow >= 2
? `No two ${row.noun}s on the same day in the last ${chartDays()} days — the gap overnight isn't counted, so there is nothing to measure yet.`
: `One ${row.noun} logged, ${formatDuration(since)} ago — log another to see the typical gap.`;
return;
}
svg.style.display = "";
@@ -1099,10 +1111,10 @@
const innerW = W - ML - MR;
const trackY = 20, trackH = 8;
// Gaps are heavily skewed: one long overnight gap a night sits alongside
// daytime gaps a tenth its length, and on a plain linear axis it squashes
// the everyday range — the part you actually read — into the first tenth of
// the track. So the axis is stretched, the way the sleep trend's is above
// Gaps are skewed: the odd long stretch (a long nap, an afternoon out) sits
// alongside everyday gaps a fraction its length, and on a plain linear axis
// it squashes the everyday range — the part you actually read — into the
// first stretch of the track. So the axis is stretched, the way the sleep trend's is above
// 10h: the typical gap is pinned to the middle, the shortest-to-typical
// stretch gets the left half and typical-to-longest the right. Reading it
// is then the same in every row — left of centre is sooner than usual,