diff --git a/status/status.js b/status/status.js index 3fbbb2b..058958f 100644 --- a/status/status.js +++ b/status/status.js @@ -24,7 +24,7 @@ const fromButton = (source) => source.endsWith('-button'); /** * @typedef {{ at: number, kind: string, source: string, reason?: string, code?: string, times?: number }} Event - * @typedef {{ name: string, up: boolean, at: number, since: number }} Check + * @typedef {{ name: string, up: boolean, at: number, since: number, outage?: number }} Check outage: when the latest outage we saw began * @typedef {{ conversations: number, turns: number, huh: number, interrupts: number, irPresses: number, looks: number }} Tally * @typedef {{ word: string, since?: number, why?: string }} State * @typedef {{ state: State, heartbeat?: number, checks: (typeof CHECKS[number] & Partial)[], today: Tally, total: Tally, recent: Event[] }} Summary @@ -82,7 +82,9 @@ export class Status { /** @param {string} name @param {boolean} up @param {number} at */ check(name, up, at) { const prev = this.#checks.get(name); - this.#checks.set(name, { name, up, at, since: prev && prev.up === up ? prev.since : at }); + const since = prev && prev.up === up ? prev.since : at; + const outage = up ? prev?.outage : since; + this.#checks.set(name, { name, up, at, since, ...(outage === undefined ? {} : { outage }) }); } size() { return this.#events.length; } @@ -93,7 +95,9 @@ export class Status { if (!up) return { word: 'not checked yet' }; if (!up.up) return { word: 'unreachable', since: up.since }; const ready = this.#events.findLast((e) => e.kind === 'ready'); - if (!ready || ready.at < up.since) return { word: 'booting', since: up.since }; + // Services restart together, so ready can come before the check sees the box up again: a ready + // counts unless it's from before the latest outage we saw. + if (!ready || ready.at < (up.outage ?? -Infinity)) return { word: 'booting', since: up.since }; const alive = Math.max(this.#heartbeat ?? 0, this.#events.findLast((e) => fromButton(e.source))?.at ?? 0); if (now - alive > 3 * HEARTBEAT_MS) return { word: 'not responding', since: alive, why: 'no heartbeat from its button service' }; const last = this.#events.findLast((e) => e.kind === 'conversation' || e.kind === 'end'); diff --git a/status/status.test.js b/status/status.test.js index 2193c8b..db629d1 100644 --- a/status/status.test.js +++ b/status/status.test.js @@ -41,6 +41,24 @@ test('a ready from before the box went away does not count: booting', () => { assert.equal(s.summary(4 * MIN).state.word, 'booting'); }); +test('a ready during the outage counts: services restart together, and the check sees the box up last', () => { + const s = new Status(dayOf); + s.check('box', true, 0); + s.record({ at: 1 * MIN, kind: 'ready', source: 'pdtv26-button' }); + s.check('box', false, 2 * MIN); + s.record({ at: 2.5 * MIN, kind: 'ready', source: 'pdtv26-button' }); + s.check('box', true, 3 * MIN); + assert.deepEqual(s.summary(4 * MIN).state, { word: 'ready for duty', since: 2.5 * MIN }); +}); + +test('a ready from before the server started counts, if the server saw no outage', () => { + const s = new Status(dayOf); + s.record({ at: 1 * MIN, kind: 'ready', source: 'pdtv26-button' }); + s.check('box', true, 10 * MIN); + s.beat(10.5 * MIN); + assert.deepEqual(s.summary(11 * MIN).state, { word: 'ready for duty', since: 1 * MIN }); +}); + test('checks keep when they last changed', () => { const s = new Status(dayOf); s.check('brain', true, 1 * MIN);