Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions packages/status/day-bars.node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { expect, test } from 'vitest'
import {
dayBarClassName,
dayBarKind,
dayBarTitle,
incidentMinutesForDay,
type IncidentSpan,
} from './day-bars.ts'
import { type ComponentDayStat } from './status-types.ts'

function day(overrides: Partial<ComponentDayStat> = {}): ComponentDayStat {
return {
day: '2026-08-19',
total: 1440,
failed: 0,
incidentMinutes: 0,
...overrides,
}
}

test('day bars follow opened incidents and keep isolated probe failures green', () => {
expect(dayBarKind(day({ total: 0 }))).toBe('empty')
expect(dayBarClassName('empty')).toBe('bar empty')
expect(dayBarTitle(day({ total: 0 }))).toBe('2026-08-19: no data')

expect(dayBarKind(day())).toBe('ok')
expect(dayBarTitle(day())).toBe('2026-08-19: 100% up')

const isolated = day({ failed: 2 })
expect(dayBarKind(isolated)).toBe('ok')
expect(dayBarClassName('ok')).toBe('bar')
expect(dayBarTitle(isolated)).toBe(
'2026-08-19: 2 isolated probe failures, no incident',
)
expect(dayBarTitle(day({ failed: 1 }))).toBe(
'2026-08-19: 1 isolated probe failure, no incident',
)

const shortIncident = day({ failed: 6, incidentMinutes: 6 })
expect(dayBarKind(shortIncident)).toBe('partial')
expect(dayBarClassName('partial')).toBe('bar partial')
expect(dayBarTitle(shortIncident)).toBe(
'2026-08-19: 6 min incident · 99.58% up',
)

const longIncident = day({ failed: 90, incidentMinutes: 90 })
expect(dayBarKind(longIncident)).toBe('bad')
expect(dayBarClassName('bad')).toBe('bar bad')
expect(dayBarTitle(longIncident)).toBe(
'2026-08-19: 90 min incident · 93.75% up',
)

const highFailedRatio = day({ failed: 80, incidentMinutes: 20 })
expect(dayBarKind(highFailedRatio)).toBe('bad')

const midnight = Date.parse('2026-08-12T00:00:00.000Z')
const now = Date.parse('2026-08-12T12:00:00.000Z')
const crossing: IncidentSpan = {
startedAt: Date.parse('2026-08-11T23:58:00.000Z'),
resolvedAt: Date.parse('2026-08-12T00:03:00.000Z'),
}
expect(incidentMinutesForDay('2026-08-11', [crossing], now)).toBe(2)
expect(incidentMinutesForDay('2026-08-12', [crossing], now)).toBe(3)
expect(incidentMinutesForDay('2026-08-13', [crossing], now)).toBe(0)
expect(
incidentMinutesForDay(
'2026-08-12',
[{ startedAt: midnight, resolvedAt: null }],
now,
),
).toBe(720)
expect(incidentMinutesForDay('not-a-day', [crossing], now)).toBe(0)

const underHour = incidentMinutesForDay(
'2026-08-12',
[
{
startedAt: midnight,
resolvedAt: midnight + 59 * 60_000 + 30_000,
},
],
now,
)
expect(underHour).toBe(59)
expect(dayBarKind(day({ failed: 59, incidentMinutes: underHour }))).toBe(
'partial',
)
})
94 changes: 94 additions & 0 deletions packages/status/day-bars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* 90-day status bars follow opened incidents, not raw probe failures.
* Isolated ticks below the consecutive-failure threshold stay green.
*/

import { type ComponentDayStat } from './status-types.ts'

export type IncidentSpan = {
startedAt: number
resolvedAt: number | null
}

export type DayBarKind = 'empty' | 'ok' | 'partial' | 'bad'

const dayMs = 24 * 60 * 60 * 1000
const minuteMs = 60_000
const majorIncidentMinutes = 60
const majorFailedRatio = 0.05

export function utcDayStartMs(day: string): number {
return Date.parse(`${day}T00:00:00.000Z`)
}

export function incidentMinutesForDay(
day: string,
spans: ReadonlyArray<IncidentSpan>,
now: number,
): number {
const dayStart = utcDayStartMs(day)
if (!Number.isFinite(dayStart)) return 0
const dayEnd = dayStart + dayMs
let overlapMs = 0
for (const span of spans) {
const end = span.resolvedAt ?? now
overlapMs += Math.max(
0,
Math.min(end, dayEnd) - Math.max(span.startedAt, dayStart),
)
}
if (overlapMs <= 0) return 0
return Math.max(1, Math.floor(overlapMs / minuteMs))
}

export function dayBarKind(day: ComponentDayStat): DayBarKind {
if (day.total === 0) return 'empty'
if (day.incidentMinutes === 0) return 'ok'
const failedRatio = day.failed / day.total
if (
failedRatio >= majorFailedRatio ||
day.incidentMinutes >= majorIncidentMinutes
) {
return 'bad'
}
return 'partial'
}

export function dayBarClassName(kind: DayBarKind): string {
switch (kind) {
case 'ok':
return 'bar'
case 'empty':
return 'bar empty'
case 'partial':
return 'bar partial'
case 'bad':
return 'bar bad'
default: {
kind satisfies never
throw new Error(`Unknown day bar kind: ${String(kind)}`)
}
}
}

export function dayBarTitle(day: ComponentDayStat): string {
const kind = dayBarKind(day)
switch (kind) {
case 'empty':
return `${day.day}: no data`
case 'ok': {
if (day.failed === 0) return `${day.day}: 100% up`
const noun = day.failed === 1 ? 'failure' : 'failures'
return `${day.day}: ${day.failed} isolated probe ${noun}, no incident`
}
case 'partial':
case 'bad': {
const pct = (((day.total - day.failed) / day.total) * 100).toFixed(2)
return `${day.day}: ${day.incidentMinutes} min incident · ${pct}% up`
}
default: {
kind satisfies never
throw new Error(`Unknown day bar kind: ${String(kind)}`)
}
}
}
5 changes: 5 additions & 0 deletions packages/status/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ returns Cloudflare 1016. Component probes never use the status hostname, so a
h), daily uptime rollups (90 days), incidents, and sent notifications.
- A component opens an incident after two consecutive probe failures and
resolves it after two consecutive successes.
- Day bars follow those opened incidents. Isolated probe failures below the
threshold stay green (the hover names them as isolated) and still count
against the 90-day probe uptime percentage. A day turns amber when an incident
covered it for under an hour and under 5% of that day's probes failed; red
when the incident lasted an hour or more, or at least 5% of probes failed.
- Operator alert emails go to `ALERT_EMAIL_TO` through the Cloudflare Email REST
API: one email when an outage starts, at most one reminder per day while it
lasts, one all-clear when everything has recovered, all under a daily cap
Expand Down
15 changes: 13 additions & 2 deletions packages/status/status-page.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ function componentSnapshot(
latencyMs: 42,
uptimePct: 99.98,
days: [
{ day: '2026-08-03', total: 1440, failed: 0 },
{ day: '2026-08-04', total: 720, failed: 3 },
{ day: '2026-08-03', total: 1440, failed: 0, incidentMinutes: 0 },
{ day: '2026-08-04', total: 720, failed: 3, incidentMinutes: 0 },
{ day: '2026-08-05', total: 1440, failed: 6, incidentMinutes: 6 },
{ day: '2026-08-06', total: 1440, failed: 90, incidentMinutes: 90 },
],
...overrides,
}
Expand Down Expand Up @@ -46,6 +48,15 @@ test('status page renders components, incidents, unknown state, and escapes deta
expect(healthy).toContain(component.name.replaceAll('&', '&amp;'))
}
expect(healthy).toContain('99.98% uptime')
expect(healthy).toContain(
'class="bar" title="2026-08-04: 3 isolated probe failures, no incident"',
)
expect(healthy).toContain(
'class="bar partial" title="2026-08-05: 6 min incident · 99.58% up"',
)
expect(healthy).toContain(
'class="bar bad" title="2026-08-06: 90 min incident · 93.75% up"',
)
expect(healthy).toContain(
'https://github.com/kentcdodds/kody/commit/abc123def4567890abcdef1234567890abcdef12',
)
Expand Down
11 changes: 3 additions & 8 deletions packages/status/status-page.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dayBarClassName, dayBarKind, dayBarTitle } from './day-bars.ts'
import {
cloudflareStatusPageUrl,
sanitizeProviderIncidentShortlink,
Expand Down Expand Up @@ -156,14 +157,8 @@ function bannerFor(snapshot: StatusSnapshot): { label: string; kind: string } {
function renderDayBars(component: ComponentSnapshot): string {
const bars = component.days
.map((day) => {
if (day.total === 0) {
return `<div class="bar empty" title="${escapeHtml(day.day)}: no data"></div>`
}
const failedRatio = day.failed / day.total
const kind =
failedRatio === 0 ? '' : failedRatio < 0.05 ? ' partial' : ' bad'
const pct = (((day.total - day.failed) / day.total) * 100).toFixed(2)
return `<div class="bar${kind}" title="${escapeHtml(day.day)}: ${pct}% up"></div>`
const kind = dayBarKind(day)
return `<div class="${dayBarClassName(kind)}" title="${escapeHtml(dayBarTitle(day))}"></div>`
})
.join('')
return `<div class="bars">${bars}</div>`
Expand Down
43 changes: 42 additions & 1 deletion packages/status/status-store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DurableObject } from 'cloudflare:workers'
import { sendAlertEmail } from './alert-email.ts'
import { incidentMinutesForDay, type IncidentSpan } from './day-bars.ts'
import {
composeStatusEmail,
decideStatusEmail,
Expand Down Expand Up @@ -509,8 +510,17 @@ export class StatusStore extends DurableObject<StatusWorkerEnv> {

async getSnapshot(): Promise<StatusSnapshot> {
const now = Date.now()
const windowStartMs = Date.parse(
`${toDay(now - (uptimeWindowDays - 1) * 24 * 60 * 60 * 1000)}T00:00:00.000Z`,
)
const incidentSpans = this.loadIncidentSpans(windowStartMs, now)
const components = statusComponents.map((component) =>
this.buildComponentSnapshot(component.id, component.name, now),
this.buildComponentSnapshot(
component.id,
component.name,
now,
incidentSpans.get(component.id) ?? [],
),
)
const overallStatus: ComponentStatus = components.some(
(component) => component.status === 'down',
Expand All @@ -532,10 +542,40 @@ export class StatusStore extends DurableObject<StatusWorkerEnv> {
}
}

private loadIncidentSpans(
windowStartMs: number,
now: number,
): Map<StatusComponentId, Array<IncidentSpan>> {
const rows = this.ctx.storage.sql
.exec<{
component: string
started_at: number
resolved_at: number | null
}>(
`SELECT component, started_at, resolved_at FROM incidents
WHERE started_at < ? AND (resolved_at IS NULL OR resolved_at >= ?)`,
now,
windowStartMs,
)
.toArray()
const byComponent = new Map<StatusComponentId, Array<IncidentSpan>>()
for (const row of rows) {
if (!isStatusComponentId(row.component)) continue
const spans = byComponent.get(row.component) ?? []
spans.push({
startedAt: row.started_at,
resolvedAt: row.resolved_at,
})
byComponent.set(row.component, spans)
}
return byComponent
}

private buildComponentSnapshot(
id: StatusComponentId,
name: string,
now: number,
spans: ReadonlyArray<IncidentSpan>,
): ComponentSnapshot {
const stateRows = this.ctx.storage.sql
.exec<{ status: string }>(
Expand Down Expand Up @@ -573,6 +613,7 @@ export class StatusStore extends DurableObject<StatusWorkerEnv> {
day,
total: stat?.total ?? 0,
failed: stat?.failed ?? 0,
incidentMinutes: incidentMinutesForDay(day, spans, now),
})
}
const total = days.reduce((sum, day) => sum + day.total, 0)
Expand Down
3 changes: 3 additions & 0 deletions packages/status/status-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export type ComponentDayStat = {
day: string
total: number
failed: number
/** Minutes an opened incident overlapped this UTC day. Isolated probe
* failures below the consecutive-failure threshold stay 0. */
incidentMinutes: number
}

export type ComponentSnapshot = {
Expand Down
Loading