diff --git a/src/app.js b/src/app.js
index 75254b6..2465616 100644
--- a/src/app.js
+++ b/src/app.js
@@ -1064,44 +1064,115 @@
// the last one. The marker is free to sit outside the band — just went (left
// of shortest) or overdue (right of typical) — which is the reading that
// decides whether to take the puppy out now.
+ //
+ // The last two rows measure a response rather than a rhythm: from waking to
+ // the pee after it, and from a meal to the poo after it. Those are what say
+ // when to take him out, where the plain gaps only say how often.
const TIMING_ROWS = [
- { type: "pee", label: "Pees", noun: "pee", cls: "tm-pee" },
- { type: "poo", label: "Poos", noun: "poo", cls: "tm-poo" },
- { type: "eat", label: "Meals", noun: "meal", cls: "tm-eat" },
+ { id: "pee", type: "pee", label: "Pees", noun: "pee", cls: "tm-pee" },
+ { id: "poo", type: "poo", label: "Poos", noun: "poo", cls: "tm-poo" },
+ { id: "eat", type: "eat", label: "Meals", noun: "meal", cls: "tm-eat" },
+ { id: "wake-pee", label: "Waking → pee", cls: "tm-pee",
+ from: "sleep-end", to: "pee", until: "sleep-start",
+ between: "from waking to a pee", sinceText: "since he woke",
+ nowLabel: (d) => `awake ${d}`,
+ empty: "No pee logged after a wake-up" },
+ { id: "eat-poo", label: "Meal → poo", cls: "tm-poo",
+ from: "eat", to: "poo", until: "eat",
+ between: "from a meal to a poo", sinceText: "since the last meal",
+ nowLabel: (d) => `ate ${d} ago`,
+ empty: "No poo logged after a meal" },
];
+ // Time from each `from` event to the first `to` after it, for the rows that
+ // measure a response. A `from` gets no gap if an `until` comes first — he
+ // went back to sleep without a pee, or ate again before the poo — since the
+ // `to` that follows can't be told apart from a response to the later one.
+ // Same-day only, like gapsBetween: a meal at nine and the next morning's poo
+ // are not cause and effect.
+ //
+ // `pending` is the wait in progress: the latest `from` today with nothing
+ // after it yet. That is when the marker is worth showing; once the pee has
+ // happened there is nothing to wait for, so the row shows the band alone.
+ function gapsAfter(events, { from, to, until }, days = chartDays()) {
+ const cutoff = startOfDay(new Date());
+ cutoff.setDate(cutoff.getDate() - (days - 1));
+ // A `to` logged in the same minute as its `from` (a quick-logged pee on
+ // waking) must sort after it to be counted as the response.
+ const rank = (t) => (t === from ? 0 : 1);
+ const evs = events
+ .filter(e => e.type === from || e.type === to || e.type === until)
+ .sort((a, b) => a.at - b.at || rank(a.type) - rank(b.type));
+ const gaps = [];
+ let open = null;
+ for (const e of evs) {
+ if (open != null && e.type === to) {
+ if (open >= cutoff.getTime() && !spansExcluded(open, e.at) &&
+ ymd(new Date(open)) === ymd(new Date(e.at))) {
+ gaps.push(e.at - open);
+ }
+ open = null;
+ } else if (e.type === from) {
+ open = e.at;
+ } else if (e.type === until) {
+ open = null;
+ }
+ }
+ const pending = open != null && ymd(new Date(open)) === ymd(new Date()) && !isExcluded(new Date(open))
+ ? Math.max(0, Date.now() - open) : null;
+ return { gaps: gaps.sort((a, b) => a - b), pending };
+ }
+
function renderTiming(events) {
for (const row of TIMING_ROWS) {
- const svg = document.getElementById(`timing-chart-${row.type}`);
- const note = document.getElementById(`timing-note-${row.type}`);
+ const svg = document.getElementById(`timing-chart-${row.id}`);
+ const note = document.getElementById(`timing-note-${row.id}`);
if (!svg || !note) continue;
+ if (row.from) {
+ const { gaps, pending } = gapsAfter(events, row);
+ const empty = `${row.empty} in the last ${chartDays()} days yet.` +
+ (pending == null ? "" : ` It has been ${formatDuration(pending)} ${row.sinceText}.`);
+ drawTimingChart(svg, note, row, gaps, pending, empty);
+ 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, events);
+ const since = last ? Math.max(0, Date.now() - last.at) : null;
+ drawTimingChart(svg, note, row, gaps, since, timingEmptyNote(events, row, since));
}
}
- function drawTimingChart(svg, note, row, gaps, since, events) {
+ // Why a plain-gap row has nothing to draw. Several logged but none sharing a
+ // day (one meal a day, say) is a different case from one logged: every gap
+ // there was overnight.
+ function timingEmptyNote(events, row, since) {
+ 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;
+ return since == null
+ ? `No ${row.noun}s logged in the last ${chartDays()} days.`
+ : 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.`;
+ }
+
+ function drawTimingChart(svg, note, row, gaps, since, emptyText) {
const typical = median(gaps);
const shortest = gaps[0] ?? null;
const longest = gaps[gaps.length - 1] ?? null;
+ // The words differ between a rhythm row ("between pees") and a response
+ // row ("from waking to a pee"); the chart itself doesn't.
+ const between = row.between ?? `between ${row.noun}s`;
+ const sinceText = row.sinceText ?? `since the last ${row.noun}`;
+ const nowLabel = row.nowLabel ?? ((d) => `${d} ago`);
- // Fewer than two events in the window means there is no gap to draw, so the
- // row falls back to a sentence rather than an axis with nothing on it.
+ // No gap in the window means nothing to draw, so the row falls back to a
+ // sentence rather than an axis with nothing on it.
if (typical == null) {
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.`
- : 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.`;
+ note.textContent = emptyText;
return;
}
svg.style.display = "";
@@ -1145,9 +1216,9 @@
const parts = [
`
The middle of every bar is that type's typical gap: left of it is sooner than usual, right of it is longer, and the faded stretch runs out to the longest gap in the window. The gap overnight, from each day's last to the next day's first, isn't counted.
+The middle of every bar is that type's typical gap: left of it is sooner than usual, right of it is longer, and the faded stretch runs out to the longest gap in the window. The gap overnight, from each day's last to the next day's first, isn't counted. Waking → pee and Meal → poo measure from each wake-up to the pee after it, and each meal to the poo after it, unless he naps or eats again first; their marker only shows while you're still waiting on one.