import { MAPLE_SERVICES_NAMESPACE } from "@maple/domain/uptime" import { bucketTimeScale } from "@maple/ui/components/plot/plot-scales" import { countLabel, formatBucketLabel, formatNumber } from "@maple/ui/lib/format" import { TONE_COLOR, TONE_TEXT } from "@maple/ui/lib/tone" import { cn } from "@maple/ui/lib/utils" import { formatAlertDateTime, formatAlertTime } from "@maple/web-data/alerts/form-utils" import { Option, Array as Arr, Record as Rec, String as Str, Order } from "effect" import { AsyncData, Submodel } from "foldkit" import { createLazy, type Html, type HtmlBuilder } from "foldkit/html" import { refreshingClass } from "@maple/ui/lib/refreshing" import * as Chart from "@maple/views/charts/chart" import { ChartSpec } from "@maple/views/charts/chart-spec" import * as FixedMetricChart from "@maple/views/charts/fixed-metric-chart" import { fixedMetricLegend } from "@maple/views/charts/fixed-metric-legend" import { LATENCY_SERIES } from "@maple/views/charts/fixed-metric-series" import { bucketTooltipBody } from "@maple/views/charts/bucket-tooltip" import { plotFrame } from "@maple/views/charts/plot-frame" import { stackedBreakdown } from "@maple/views/charts/stacked-breakdown-chart" import { resolveColor, type ThemeColors } from "@maple/views/charts/theme-colors" import { chartSkeleton } from "@maple/views/ui/chart-skeleton" import { emptyMessage } from "@maple/views/ui/empty" import { errorState } from "@maple/views/ui/error-state" import { segmentedSelect } from "@maple/views/ui/segmented-select" import { relativeTime } from "../../kit/relative-time" import { statRail, statRailItem } from "../../kit/stat-rail" import { isoMs } from "../shared/time" import * as CopyButton from "@maple/views/ui/copy-button" import { copyableField } from "@maple/views/ui/copyable-field" import { eyebrow } from "@maple/views/ui/eyebrow" import { panel, panelHeader } from "@maple/views/ui/panel" import { traceHref, uptimeTracesHref, UPTIME_SERVICE } from "../shared/uptime-commands" import { displayStatus, formatEvery, formatMs, formatShare, formatSpan, checkHealth, latencyPercentiles, medianOf, STATUS_TONE, type UptimeMonitor, uptimeShare, } from "../shared/uptime-derive" import { Message } from "./message" import type { CheckPoint, CheckSample, CheckStats, Model, PhasePoint, UptimeWindow } from "./model" import { CHECK_HEALTH_FILL, uptimePanelBody } from "../shared/uptime-view" import { typography, typographyClass } from "@maple/views/ui/typography" import { hstack, vstack, vstackClass } from "@maple/views/ui/stack" import { chartFrameClass } from "@maple/views/ui/chart-frame" import { dividedList } from "@maple/views/ui/rule" import { textLink } from "@maple/views/ui/text-link" import { selectableRow } from "@maple/views/ui/selectable-row" // The monitor page's panels: response time, the checks list and the side panels. type H = HtmlBuilder const CHECK_ROWS = 15 /** A key/value row in a side panel. */ const fact = (h: H, label: string, value: Html | string, valueClass?: string): Html => hstack(h, { gap: "3", align: "baseline", justify: "between", className: "px-4 py-2.5 text-sm" }, [ typography(h, { tone: "muted", className: "shrink-0 whitespace-nowrap" }, [label]), h.span([h.Class(cn("min-w-0 break-words text-right tabular-nums", valueClass))], [value]), ]) /** An HTTP check's phases in the order they happen, which is also the stack order from the axis up. */ const PHASES = [ { key: "dnsMs", label: "DNS", token: "--chart-1" }, { key: "connectMs", label: "Connect", token: "--chart-2" }, { key: "tlsMs", label: "TLS", token: "--chart-3" }, { key: "ttfbMs", label: "TTFB", token: "--chart-4" }, { key: "transferMs", label: "Transfer", token: "--chart-5" }, ] as const satisfies ReadonlyArray<{ key: keyof PhasePoint; label: string; token: string }> export const CHART_TOKENS = [ ...Arr.map(LATENCY_SERIES, (entry) => entry.token), ...Arr.map(PHASES, (phase) => phase.token), "--border", "--background", ] const WINDOWS: ReadonlyArray<{ value: UptimeWindow; label: string }> = [ { value: "1h", label: "1h" }, { value: "24h", label: "24h" }, { value: "7d", label: "7d" }, ] const WINDOW_LABEL = { "1h": "last hour", "24h": "last 24 hours", "7d": "last 7 days" } as const const STATUS_WORD = { up: "Up", down: "Down", degraded: "Degraded", pending: "Pending", paused: "Paused", } as const export type ChartContext = Readonly<{ theme: ThemeColors; timeZone: string; nowMs: number }> const statsOf = (model: Model): Option.Option => AsyncData.getData(model.stats.data) /** A switched window shows the previous figures until its own land; they are dimmed meanwhile. */ const isStatsRefreshing = (model: Model): boolean => AsyncData.isRefreshing(model.stats.data) /** Time of day within an hour, the date as well over longer windows. */ const timeLabel = (window: UptimeWindow, iso: string, timeZone: string): string => window === "1h" ? formatAlertTime(iso, timeZone) : formatAlertDateTime(iso, timeZone) /** `3m ago`, the absolute time on hover. */ const ago = (h: H, nowMs: number, timeZone: string, iso: string): Html => relativeTime(h, { value: isoMs(iso), nowMs, timeZone }) /** The figures over the page: status, the last check, uptime and the window's percentiles. */ export const kpiRail = ( h: H, model: Model, monitor: UptimeMonitor, nowMs: number, timeZone: string, ): Html => { const status = displayStatus(monitor) const changed = monitor.state.status_changed_at const last = monitor.state.last_check const isHeartbeat = monitor.kind === "heartbeat" const statusItem = statRailItem(h, { eyebrow: "Status", value: STATUS_WORD[status], tone: STATUS_TONE[status], compact: true, subline: changed === null || status === "paused" || status === "pending" ? `Every ${formatEvery(monitor.interval_seconds)}` : `for ${formatSpan(nowMs - isoMs(changed))}`, }) const lastItem = isHeartbeat ? statRailItem(h, { eyebrow: "Last ping", value: monitor.state.last_ping_at === null ? "None yet" : ago(h, nowMs, timeZone, monitor.state.last_ping_at), compact: true, subline: monitor.state.last_ping_at === null ? "Waiting for the first ping" : formatAlertTime(monitor.state.last_ping_at, timeZone), }) : statRailItem(h, { eyebrow: "Last checked", value: last === null ? "Not yet" : ago(h, nowMs, timeZone, last.at), tone: last !== null && !last.ok ? "crit" : "neutral", compact: true, subline: last === null ? "The first check runs shortly" : Arr.join( [ last.status_code === null ? last.ok ? "Passed" : "Failed" : String(last.status_code), last.duration_ms === null ? "" : ` in ${formatMs(last.duration_ms)}`, ], "", ), }) if (isHeartbeat) { const share = uptimeShare(monitor.state.recent) return statRail(h, { columns: 3 }, [ statusItem, lastItem, statRailItem(h, { eyebrow: "Uptime", value: formatShare(share), tone: share !== null && share < 1 ? "warn" : "neutral", compact: true, subline: `Over the last ${countLabel(monitor.state.recent.length, "check")}`, }), ]) } // The window's figures from the check spans; the recent ring until they arrive. const stats = statsOf(model) const recent = latencyPercentiles(monitor.state.recent) const figures = Option.match(stats, { onNone: () => ({ share: uptimeShare(monitor.state.recent), p50: recent.p50, p95: recent.p95, p99: recent.p99, scope: `last ${countLabel(monitor.state.recent.length, "check")}`, subline: `last ${countLabel(monitor.state.recent.length, "check")}`, }), onSome: (data) => ({ share: data.checks === 0 ? null : (data.checks - data.failures) / data.checks, p50: data.p50Ms, p95: data.p95Ms, p99: data.p99Ms, scope: WINDOW_LABEL[data.window], subline: data.checks === 0 ? `No checks in the ${WINDOW_LABEL[data.window]}` : `${formatNumber(data.checks)} checks, ${formatNumber(data.failures)} failed`, }), }) const percentile = (label: string, value: number | null) => statRailItem(h, { eyebrow: label, value: formatMs(value), compact: true, subline: figures.scope, }) return statRail( h, { columns: 3, isBusy: isStatsRefreshing(model), className: cn("lg:grid-cols-6 lg:divide-y-0", refreshingClass(isStatsRefreshing(model))), }, [ statusItem, lastItem, statRailItem(h, { eyebrow: "Uptime", value: formatShare(figures.share), tone: figures.share !== null && figures.share < 1 ? "warn" : "neutral", compact: true, subline: figures.subline, }), percentile("P50", figures.p50), percentile("P95", figures.p95), percentile("P99", figures.p99), ], ) } /** * The latency rows: buckets that ran a check, with a null row where checks stopped for longer than * two check intervals, so the line breaks there instead of drawing latency nobody measured. A shorter * empty bucket (a check every 5 minutes on 1-minute buckets) stays joined: a null there would leave * each check a lone point the line never draws. */ /** The monitor's check interval, 0 until it loads (the bucket width then sets the gap). */ const checkIntervalMs = (model: Model): number => (Option.getOrNull(AsyncData.getData(model.monitor.data))?.interval_seconds ?? 0) * 1000 /** Where the response time line breaks: the buckets the breakdown leaves a gap at too. */ const breakBuckets = (stats: CheckStats, intervalMs: number): ReadonlyArray => Arr.flatMap(chartRows(stats, intervalMs), (row) => row.p50LatencyMs === null && typeof row.bucket === "string" ? [row.bucket] : [], ) export const chartRows = (stats: CheckStats, intervalMs: number) => { const stepMs = bucketStepMs(stats.points, stats.windowEndMs) const gapAfterMs = 2 * Math.max(intervalMs, stepMs) return Arr.reduce< CheckPoint, { rows: ReadonlyArray> emptySinceMs: number | null } >(stats.points, { rows: [], emptySinceMs: null }, (acc, point) => { const atMs = isoMs(point.bucket) if (point.totalCount === 0) return { rows: acc.rows, emptySinceMs: acc.emptySinceMs ?? atMs } const broken = acc.rows.length > 0 && acc.emptySinceMs !== null && atMs - acc.emptySinceMs > gapAfterMs const gap = { bucket: new Date(acc.emptySinceMs ?? atMs).toISOString(), p50LatencyMs: null, p95LatencyMs: null, p99LatencyMs: null, partial: false, } const row = { bucket: point.bucket, p50LatencyMs: point.p50LatencyMs, p95LatencyMs: point.p95LatencyMs, p99LatencyMs: point.p99LatencyMs, partial: point.partial, } return { rows: [...acc.rows, ...(broken ? [gap] : []), row], emptySinceMs: null } }).rows } /** The plot's insets in px, shared by every chart on the page so one instant sits at one x (`pl-16`, `pr-3`). */ const PLOT_LEFT = 64 const PLOT_RIGHT = 12 /** Every chart spans the picked window, not just the buckets that hold data. */ const windowDomain = (stats: CheckStats): readonly [number, number] => [ stats.windowStartMs, stats.windowEndMs, ] /** The median count of checks in a bucket that holds any. */ const checksPerBucket = (points: ReadonlyArray): number => { const counts = Arr.sort( Arr.filter( Arr.map(points, (point) => point.totalCount), (count) => count > 0, ), Order.Number, ) return counts[Math.floor(counts.length / 2)] ?? 0 } /** * The percentiles a bucket holds enough checks to tell apart: one check is just its duration, and * P99 of a few dozen checks is their slowest one, the same line as P95. */ export const percentileSeries = (points: ReadonlyArray) => { const perBucket = checksPerBucket(points) const shown = Arr.filter(LATENCY_SERIES, (entry) => perBucket <= 1 ? entry.key === "p50LatencyMs" : perBucket < 50 ? entry.key !== "p99LatencyMs" : true, ) return Arr.map(shown, (entry) => ({ ...entry, label: perBucket <= 1 ? "Response time" : entry.label })) } const latencyChart = (h: H, model: Model, stats: CheckStats, context: ChartContext): Html => { const series = percentileSeries(stats.points) const intervalMs = checkIntervalMs(model) return h.submodel({ slotId: "uptime-latency-chart", model: model.chart, view: FixedMetricChart.view, viewInputs: { chartId: "uptime-latency", ariaLabel: "Response time percentiles", spec: ChartSpec.FixedMetric({ chart: "Latency", marginLeft: PLOT_LEFT, marginRight: PLOT_RIGHT, xDomainMs: [...windowDomain(stats)], latencySeries: Arr.map(series, ({ key, label }) => ({ key, label })), rows: chartRows(stats, intervalMs), colors: { border: resolveColor(context.theme, "--border"), background: resolveColor(context.theme, "--background"), ...Rec.fromEntries( Arr.map(LATENCY_SERIES, (entry) => [ entry.key, resolveColor(context.theme, entry.token), ]), ), }, perSecond: false, timeZone: context.timeZone, nowMs: context.nowMs, gradientPrefix: "uptime-latency", showTooltip: true, }), className: chartFrameClass("fill"), legend: fixedMetricLegend( h, Arr.map(series, (entry) => ({ key: entry.key, label: entry.label, color: resolveColor(context.theme, entry.token), })), ), }, toParentMessage: (message: Chart.Message) => Message.GotChartMessage({ message }), }) } /** The bucket width: the smallest gap between buckets, else the span from the only one to the window's end. */ const bucketStepMs = (points: ReadonlyArray, endMs: number): number => { const starts = Arr.map(points, (point) => isoMs(point.bucket)) const gaps = Arr.flatMap(starts.slice(1), (start, index) => { const gap = start - (starts[index] ?? start) return gap > 0 ? [gap] : [] }) return gaps.length > 0 ? Math.min(...gaps) : Math.max(1, endMs - (starts[0] ?? endMs)) } /** * One segment per bucket under the chart, placed by its time on the same window as the chart: the * warehouse trims empty leading buckets, so counting segments would stretch them across the window. * Filled by `checkHealth` against the window's median p95. */ const availabilityStrip = (h: H, stats: CheckStats, window: UptimeWindow, timeZone: string): Html => { const domain: Domain = { startMs: stats.windowStartMs, endMs: stats.windowEndMs } const stepMs = bucketStepMs(stats.points, domain.endMs) const medianMs = medianOf( Arr.flatMap(stats.points, (point) => (point.totalCount === 0 ? [] : [point.p95LatencyMs])), ) return h.div( [h.Class("h-2 pr-3 pl-16"), h.AriaHidden(true)], [ h.div( [h.Class("relative h-full")], Arr.map(stats.points, (point) => { const startMs = isoMs(point.bucket) const left = Math.max(0, xShare(startMs, domain)) const right = Math.min(1, xShare(startMs + stepMs, domain)) const failed = Math.round(point.errorRate * point.totalCount) const health = checkHealth( { total: point.totalCount, failed, latencyMs: point.p95LatencyMs }, medianMs, ) const label = point.totalCount === 0 ? `${timeLabel(window, point.bucket, timeZone)}: no checks` : `${timeLabel(window, point.bucket, timeZone)}: ${point.totalCount} checks, ${failed} failed${health === "slow" ? ", slow" : ""}` return h.span( [ h.Key(point.bucket), h.Title(label), h.Class(cn("absolute inset-y-0 rounded-2xs", CHECK_HEALTH_FILL[health])), h.Style({ left: `${left * 100}%`, width: `max(1px, calc(${((right - left) * 100).toFixed(4)}% - 1px))`, }), ], [], ) }), ), ], ) } /** P50, P95 and P99 over the picked window, from the monitor's check spans. */ export const responseTimePanel = (h: H, model: Model, context: ChartContext | null): Html => { const data = model.stats.data const body = AsyncData.isFailure(data) ? errorState(h, { failure: data.error, title: "Couldn't load response times", className: "py-8" }) : Option.match(statsOf(model), { onNone: () => h.div([h.Class("h-56")], [chartSkeleton(h, "line")]), onSome: (stats) => stats.checks === 0 ? h.p( [h.Class("py-16 text-center text-sm text-muted-foreground")], [ `No checks in the ${WINDOW_LABEL[stats.window]}. The next one runs shortly.`, ], ) : model.chartRuntime._tag === "Failed" ? h.p( [h.Class("py-16 text-center text-sm text-muted-foreground")], ["The chart couldn't load. Reload the page to try again."], ) : context === null ? h.div([h.Class("h-56")], [chartSkeleton(h, "line")]) : h.div( [ ...(isStatsRefreshing(model) ? [h.AriaBusy(true)] : []), h.Class( cn( vstackClass({ gap: "2" }), refreshingClass(isStatsRefreshing(model)), ), ), ], [ h.div( [h.Class("h-56")], [latencyChart(h, model, stats, context)], ), availabilityStrip(h, stats, stats.window, context.timeZone), ], ), }) return panel(h, {}, [ panelHeader(h, { title: "Response time", action: segmentedSelect(h, { ariaLabel: "Window", size: "xs", value: model.window, onChange: (window) => Message.PickedWindow({ window }), options: WINDOWS, }), }), uptimePanelBody(h, [body]), ]) } /** 1, 2 or 5 times a power of ten, at or above `value`. */ const niceCeil = (value: number): number => { const power = 10 ** Math.floor(Math.log10(Math.max(1, value))) const step = Option.getOrElse( Arr.findFirst([1, 2, 5, 10], (candidate) => candidate * power >= value), () => 10, ) return step * power } /** The y range: the checks' p99 with headroom, so one timeout does not flatten the rest; slower checks sit on the top edge. */ const scatterMax = (samples: ReadonlyArray): number => { const sorted = Arr.map(samples, (sample) => sample.durationMs).sort((a, b) => a - b) const p99 = sorted[Math.min(sorted.length - 1, Math.ceil(sorted.length * 0.99) - 1)] ?? 1 return niceCeil(p99 * 1.15) } type Domain = Readonly<{ startMs: number; endMs: number }> /** Where a time or a duration sits on the plot, as a 0 to 1 share of its width or height. */ const xShare = (ms: number, domain: Domain): number => (ms - domain.startMs) / Math.max(1, domain.endMs - domain.startMs) const yShare = (ms: number, yMax: number): number => Math.min(1, ms / yMax) /** Round clock ticks in the viewer's zone, short labels; the end ones kept inside the plot. */ const scatterTimeTicks = (domain: Domain, window: UptimeWindow, timeZone: string) => { const context = { rangeMs: domain.endMs - domain.startMs, bucketSeconds: undefined, timeZone } return Arr.filter( Arr.map( bucketTimeScale([new Date(domain.startMs), new Date(domain.endMs)], timeZone).ticks( window === "7d" ? 7 : 4, ), (date) => ({ share: xShare(date.getTime(), domain), label: formatBucketLabel(date.toISOString(), context, "tick"), }), ), (tick) => tick.share >= 0 && tick.share <= 1, ) } const tickAnchor = (share: number): string => share < 0.06 ? "translate-x-0" : share > 0.94 ? "-translate-x-full" : "-translate-x-1/2" /** A check's bar segments bottom up: its phases when it recorded them, else one bar of its total. */ const barSegments = (sample: CheckSample): ReadonlyArray<{ key: string; ms: number; color: string }> => sample.phases === null ? [{ key: "total", ms: sample.durationMs, color: "var(--muted-foreground)" }] : Arr.filter( Arr.map(PHASES, (phase) => ({ key: phase.key, ms: sample.phases?.[phase.key] ?? 0, color: `var(${phase.token})`, })), (segment) => segment.ms > 0, ) /** One column per check, its full height a link to its trace; failures red, a check past the axis capped with a caret. */ const checkBars = ( h: H, samples: ReadonlyArray, startMs: number, endMs: number, yMax: number, ): Html => { const domain: Domain = { startMs, endMs } // Bars thin out as checks pile up, never under a pixel or over 6; the width keeps a 30% gap, so no inset. const width = `max(1px, min(6px, ${(70 / Math.max(1, samples.length)).toFixed(3)}%))` const bar = (sample: CheckSample, index: number) => { const clipped = sample.durationMs > yMax return selectableRow( h, { href: traceHref(sample.traceId), isSelected: false, className: "group absolute inset-y-0 -translate-x-1/2 hover:bg-surface-sunken active:bg-surface-sunken", attributes: [ h.Key(`${sample.traceId}-${index}`), h.Attribute("tabindex", "-1"), h.OnMouseEnter(Message.HoveredCheck({ index })), h.Style({ left: `${xShare(isoMs(sample.at), domain) * 100}%`, width }), ], }, [ h.span( [ h.Class( cn( "absolute inset-x-0 bottom-0 flex flex-col-reverse overflow-hidden rounded-t-2xs group-hover:brightness-125", !sample.ok && CHECK_HEALTH_FILL.mostlyFailed, ), ), h.Style({ height: `${yShare(sample.durationMs, yMax) * 100}%` }), ], sample.ok ? Arr.map(barSegments(sample), (segment) => h.span( [ h.Key(segment.key), h.Class("block w-full"), h.Style({ flexGrow: String(segment.ms), background: segment.color }), ], [], ), ) : [], ), ...(clipped ? [ h.span( [ h.Class( cn( "absolute -top-1.5 left-1/2 size-0 -translate-x-1/2 border-x-[3px] border-t-[5px] border-x-transparent", sample.ok ? "border-t-foreground/60" : "border-t-severity-error", ), ), ], [], ), ] : []), ], ) } return h.div( [ h.AriaHidden(true), h.Class("absolute inset-0"), h.OnMouseLeave(Message.HoveredCheck({ index: null })), ], Arr.map(samples, bar), ) } const barsSlot = createLazy() const cardRow = (h: H, key: string, color: string, label: string, value: string): Html => hstack(h, { gap: "4", justify: "between", as: "span", attributes: [h.Key(key)] }, [ hstack(h, { gap: "1.5", as: "span", className: typographyClass({ tone: "muted" }) }, [ h.span([h.Class("inline-block size-2 rounded-xs"), h.Style({ background: color })], []), label, ]), typography(h, { mono: true, numeric: true }, [value]), ]) /** The hovered check's card: when, what it returned, and where its time went. */ const hoverCard = (h: H, sample: CheckSample, domain: Domain, timeZone: string): Html => { const left = xShare(isoMs(sample.at), domain) * 100 const result = sample.statusCode === null ? (sample.ok ? "Passed" : "Failed") : String(sample.statusCode) return h.div( [ h.Class( cn( "pointer-events-none absolute top-1 z-30 flex w-48 flex-col gap-1 rounded-md border bg-popover px-2.5 py-2 text-xs shadow-popup", left > 60 ? "-translate-x-full -ml-2" : "ml-2", ), ), h.Style({ left: `${left}%` }), ], [ typography(h, { tone: "muted" }, [formatAlertDateTime(sample.at, timeZone)]), hstack( h, { gap: "4", align: "center", justify: "between", as: "span", className: "font-medium" }, [ h.span([h.Class(sample.ok ? TONE_TEXT.info : TONE_TEXT.crit)], [result]), typography(h, { mono: true, numeric: true }, [formatMs(sample.durationMs)]), ], ), ...(sample.phases === null ? [] : Arr.map(PHASES, (phase) => cardRow( h, phase.key, `var(${phase.token})`, phase.label, formatPhaseMs(sample.phases?.[phase.key] ?? 0), ), )), h.span([h.Class("pt-0.5 text-primary")], ["Click to open the trace"]), ], ) } /** Phases are often under a millisecond, which whole-ms rounding would show as 0. */ const formatPhaseMs = (ms: number): string => (ms < 10 ? `${ms.toFixed(1)} ms` : formatMs(ms)) const legendItem = (h: H, key: string, swatch: Html, label: string): Html => hstack(h, { gap: "1.5", as: "span", attributes: [h.Key(key)] }, [swatch, label]) const swatch = (h: H, color: string): Html => h.span([h.Class("inline-block size-2 rounded-xs"), h.Style({ background: color })], []) const barsLegend = (h: H, samples: ReadonlyArray, yMax: number): Html => { const timed = Arr.some(samples, (sample) => sample.ok && sample.phases !== null) const untimed = Arr.some(samples, (sample) => sample.ok && sample.phases === null) const failed = Arr.some(samples, (sample) => !sample.ok) const clipped = Arr.some(samples, (sample) => sample.durationMs > yMax) return hstack( h, { align: "center", wrap: true, className: "gap-x-4 gap-y-1 pr-3 pl-16 text-xs text-muted-foreground", }, [ ...(timed ? Arr.map(PHASES, (phase) => legendItem(h, phase.key, swatch(h, `var(${phase.token})`), phase.label), ) : []), ...(untimed ? [ legendItem( h, "untimed", swatch(h, "var(--muted-foreground)"), timed ? "No phases recorded" : "Passed", ), ] : []), ...(failed ? [legendItem(h, "failed", swatch(h, TONE_COLOR.crit), "Failed")] : []), ...(clipped ? [ legendItem( h, "clipped", h.span( [ h.Class( "inline-block size-0 border-x-[4px] border-t-[6px] border-x-transparent border-t-foreground/60", ), ], [], ), `Slower than ${formatMs(yMax)}`, ), ] : []), h.span([h.Key("hint"), h.Class("ml-auto")], ["Click a bar to open its trace"]), ], ) } /** Every check in the window as a bar, its height its duration and its stack its phases. */ export const checksScatterPanel = (h: H, model: Model, timeZone: string): Html | null => Option.match(statsOf(model), { onNone: () => null, onSome: (stats) => { if (stats.samplesFailed) return panel(h, {}, [ panelHeader(h, { title: "Every check" }), uptimePanelBody(h, [ h.p( [h.Class("py-10 text-center text-sm text-muted-foreground")], ["Couldn't load the checks. The other figures are unaffected."], ), ]), ]) if (stats.samples.length === 0) return null const yMax = scatterMax(stats.samples) const failed = Arr.filter(stats.samples, (sample) => !sample.ok).length // Past the list's limit the axis starts at the oldest check shown, not the window's start. const oldest = stats.samples[0] const startMs = stats.samplesTruncated && oldest !== undefined ? isoMs(oldest.at) : stats.windowStartMs const domain: Domain = { startMs, endMs: stats.windowEndMs } const hovered = model.hoveredCheck === null ? undefined : stats.samples[model.hoveredCheck] const yTicks = [0, 0.5, 1] return panel(h, {}, [ panelHeader(h, { title: "Every check", action: typography(h, { size: "xs", tone: "muted", numeric: true }, [ stats.samplesTruncated ? `Newest ${formatNumber(stats.samples.length)} checks` : `${formatNumber(stats.samples.length)} checks`, failed === 0 ? "" : `, ${formatNumber(failed)} failed`, ]), }), uptimePanelBody(h, [ h.div( [ ...(isStatsRefreshing(model) ? [h.AriaBusy(true)] : []), h.Class(cn(vstackClass({ gap: "2" }), refreshingClass(isStatsRefreshing(model)))), ], [ hstack(h, { align: "stretch", className: "h-56 pt-2 pr-3" }, [ h.div( [ h.AriaHidden(true), h.Class( "relative w-16 shrink-0 font-mono text-2xs text-muted-foreground tabular-nums", ), ], Arr.map(yTicks, (share) => h.span( [ h.Key(String(share)), h.Class("absolute right-2 translate-y-1/2"), h.Style({ bottom: `${share * 100}%` }), ], [share === 0 ? "0" : formatMs(yMax * share)], ), ), ), h.div( [ h.Role("img"), h.AriaLabel( `${stats.samples.length} checks over the ${WINDOW_LABEL[stats.window]}, ${failed} failed`, ), h.Class("relative min-w-0 flex-1"), ], [ ...Arr.map(yTicks, (share) => h.div( [ h.Key(`grid-${share}`), h.AriaHidden(true), h.Class( cn( "pointer-events-none absolute inset-x-0 border-t", share === 0 ? "border-border" : "border-dashed border-border-subtle", ), ), h.Style({ bottom: `${share * 100}%` }), ], [], ), ), // Memoized: hover and other page messages do not rebuild thousands of bars. barsSlot(checkBars, [ h, stats.samples, startMs, stats.windowEndMs, yMax, ]), ...(hovered === undefined ? [] : [hoverCard(h, hovered, domain, timeZone)]), ], ), ]), h.div( [ h.AriaHidden(true), h.Class( "relative mr-3 ml-16 h-4 font-mono text-2xs text-muted-foreground", ), ], Arr.map(scatterTimeTicks(domain, stats.window, timeZone), (tick) => h.span( [ h.Key(tick.label), h.Class( cn( "absolute top-0 whitespace-nowrap", tickAnchor(tick.share), ), ), h.Style({ left: `${tick.share * 100}%` }), ], [tick.label], ), ), ), barsLegend(h, stats.samples, yMax), ], ), ]), ]) }, }) /** Buckets whose checks got no response to time (all failed early, or predate phases): zeros, not phases. */ const untimed = (point: PhasePoint): boolean => point.timedChecks === 0 /** * The breakdown's long rows (one per bucket and phase, labelled for the tooltip and legend) and its * breaks: where checks stopped, and where checks ran but none was timed. */ export const phaseSeries = (stats: CheckStats, intervalMs: number) => ({ rows: Arr.flatMap( Arr.filter(stats.phases, (point: PhasePoint) => !untimed(point)), (point: PhasePoint) => Arr.map(PHASES, (phase) => ({ bucket: point.bucket, attributeValue: phase.label, value: point[phase.key], })), ), breaks: [ ...breakBuckets(stats, intervalMs), ...Arr.map(Arr.filter(stats.phases, untimed), (point) => point.bucket), ], }) const phaseChart = (h: H, model: Model, stats: CheckStats, context: ChartContext): Html => { const series = phaseSeries(stats, checkIntervalMs(model)) const built = stackedBreakdown({ rows: series.rows, colors: Rec.fromEntries(Arr.map(PHASES, (phase) => [phase.label, `var(${phase.token})`])), order: Arr.map(PHASES, (phase) => phase.label), theme: context.theme, timeZone: context.timeZone, gradientPrefix: "uptime-phases", unit: "duration_ms", showTotal: true, xDomainMs: windowDomain(stats), marginLeft: PLOT_LEFT, breaks: series.breaks, }) return h.div( [h.Class("h-56")], [ h.submodel({ slotId: "uptime-phase-chart", model: model.phaseChart, view: PhaseChartView, viewInputs: { spec: built.spec, legend: fixedMetricLegend( h, Arr.map(PHASES, (phase) => ({ key: phase.key, label: phase.label, color: resolveColor(context.theme, phase.token), })), ), }, toParentMessage: (message: Chart.Message) => Message.GotPhaseChartMessage({ message }), }), ], ) } const PhaseChartView = Submodel.defineView< Chart.Model, Chart.Message, Readonly<{ spec: ChartSpec; legend: Html }> >((model, inputs, h) => plotFrame(h, { chartId: "uptime-phases", spec: inputs.spec, ariaLabel: "Response time breakdown by phase", className: chartFrameClass("fill"), legend: inputs.legend, tooltipBody: (portal) => Option.flatMap(model.tooltip, (tooltip) => tooltip._tag === "Bucket" ? Option.some(bucketTooltipBody(h, tooltip, portal)) : Option.none(), ), }), ) /** HTTP monitors: where each check's time went, averaged per bucket over the picked window. */ export const phaseBreakdownPanel = (h: H, model: Model, context: ChartContext | null): Html => { const data = model.stats.data const body = AsyncData.isFailure(data) ? errorState(h, { failure: data.error, title: "Couldn't load the breakdown", className: "py-8" }) : Option.match(statsOf(model), { onNone: () => h.div([h.Class("h-56")], [chartSkeleton(h, "area")]), onSome: (stats) => stats.phasesFailed ? h.p( [h.Class("py-16 text-center text-sm text-muted-foreground")], ["Couldn't load the breakdown. The other figures are unaffected."], ) : Arr.every(stats.phases, untimed) ? h.p( [h.Class("py-16 text-center text-sm text-muted-foreground")], [ `No timed checks in the ${WINDOW_LABEL[stats.window]}. Newer checks record each phase.`, ], ) : model.chartRuntime._tag === "Failed" ? h.p( [h.Class("py-16 text-center text-sm text-muted-foreground")], ["The chart couldn't load. Reload the page to try again."], ) : context === null ? h.div([h.Class("h-56")], [chartSkeleton(h, "area")]) : phaseChart(h, model, stats, context), }) return panel(h, { attributes: [h.AriaLabel("Response time breakdown")] }, [ panelHeader(h, { title: "Response time breakdown" }), uptimePanelBody(h, [body]), ]) } type CheckRow = Readonly<{ at: string ok: boolean statusCode: number | null durationMs: number | null result: string traceId: string | null }> /** Newest first: the last check carries the status, error and trace; the rest only pass or fail. */ const checkRows = (monitor: UptimeMonitor): ReadonlyArray => { const last = monitor.state.last_check const recent = [...monitor.state.recent].reverse() return Arr.map(recent.slice(0, CHECK_ROWS), (check): CheckRow => last !== null && last.at === check.at ? { at: last.at, ok: last.ok, statusCode: last.status_code, durationMs: last.duration_ms, result: last.ok ? "" : (last.error ?? ""), traceId: last.trace_id, } : { at: check.at, ok: check.ok, statusCode: null, durationMs: check.duration_ms, result: "", traceId: null, }, ) } const CHECK_GRID = "grid grid-cols-[90px_64px_90px_minmax(0,1fr)_80px] items-center gap-3" export const checksPanel = (h: H, monitor: UptimeMonitor, timeZone: string): Html => { const rows = checkRows(monitor) return panel(h, {}, [ panelHeader(h, { title: "Checks", action: h.a( [ h.Href(uptimeTracesHref()), h.Title( `Each check is a span in the ${UPTIME_SERVICE} service, namespace ${MAPLE_SERVICES_NAMESPACE}.`, ), h.Class( typographyClass({ size: "xs", tone: "primary", className: "hover:underline active:underline", }), ), ], ["View all checks in Traces"], ), }), h.div( [h.Class(cn(CHECK_GRID, "border-y px-4 py-2"))], [ eyebrow(h, {}, ["Time"]), eyebrow(h, {}, ["Status"]), eyebrow(h, {}, ["Duration"]), eyebrow(h, {}, ["Error"]), eyebrow(h, { className: "text-right" }, ["Trace"]), ], ), rows.length === 0 ? emptyMessage(h, {}, ["No checks yet"]) : dividedList( h, { as: "ul", bordered: false }, Arr.map(rows, (row, index) => h.li( [h.Key(`${row.at}-${index}`), h.Class(cn(CHECK_GRID, "px-4 py-2 text-sm"))], [ typography(h, { size: "xs", tone: "muted", mono: true }, [ formatAlertTime(row.at, timeZone), ]), h.span( [ h.Class( cn( typographyClass({ size: "xs", mono: true }), row.ok ? TONE_TEXT.info : TONE_TEXT.crit, ), ), ], [ row.statusCode === null ? row.ok ? "Pass" : "Fail" : String(row.statusCode), ], ), typography(h, { size: "xs", mono: true, numeric: true }, [ formatMs(row.durationMs), ]), h.span( [ h.Class( cn( typographyClass({ size: "xs", truncate: true }), TONE_TEXT.crit, ), ), h.Title(row.result), ], [row.result], ), row.traceId === null ? typography(h, { size: "xs", tone: "muted", className: "text-right" }, [ "–", ]) : textLink( h, { href: traceHref(row.traceId), isMono: true, className: "text-right text-xs font-normal", }, [`${Str.slice(0, 4)(row.traceId)}…${Str.slice(-4)(row.traceId)}`], ), ], ), ), ), ]) } /** "every 1d, 30m grace", or the cron line and its time zone. */ const heartbeatExpectation = (config: Extract): string => { const schedule = config.schedule.type === "interval" ? `every ${formatEvery(config.schedule.seconds)}` : `cron ${config.schedule.expression} (${config.schedule.timezone})` return `${schedule}, ${formatEvery(config.grace_seconds)} grace` } export const lastCheckPanel = (h: H, monitor: UptimeMonitor): Html => { const last = monitor.state.last_check const result = last === null ? "No check yet" : last.status_code !== null ? String(last.status_code) : last.ok ? "Passed" : "Failed" const config = monitor.config const isHeartbeat = config.type === "heartbeat" return panel(h, { className: "divide-y" }, [ panelHeader(h, { title: "Last check" }), fact(h, "Result", result, last !== null && !last.ok ? TONE_TEXT.crit : undefined), ...(isHeartbeat ? [] : [fact(h, "Duration", formatMs(last?.duration_ms ?? null))]), fact(h, "Probe", isHeartbeat ? "Your job" : "Ashburn, US"), config.type === "heartbeat" ? fact(h, "Expected", heartbeatExpectation(config)) : fact( h, "Down after", monitor.failures_to_down === 1 ? "1 failure" : `${monitor.failures_to_down} failures in a row`, ), ...(last === null || last.ok || last.error === null ? [] : [h.p([h.Class(cn("px-4 py-2.5 text-sm", TONE_TEXT.crit))], [last.error])]), ]) } export const setupPanel = (h: H, model: Model, monitor: UptimeMonitor, serviceHref: string | null): Html => { const count = monitor.destination_ids.length // `null` until destinations sync: the count, rather than calling every one unavailable. const destinations = model.destinations const notifies = count === 0 ? "Nobody yet" : destinations === null ? countLabel(count, "destination") : Arr.join( Arr.map(monitor.destination_ids, (id) => Arr.findFirst(destinations, (row) => row.id === id).pipe( Option.flatMapNullishOr((found) => found.name), Option.getOrElse(() => "Unavailable destination"), ), ), ", ", ) return panel(h, { className: "divide-y" }, [ panelHeader(h, { title: "Setup" }), fact(h, "Notifies", notifies, count === 0 ? "text-muted-foreground" : undefined), serviceHref === null || monitor.service_name === null ? fact(h, "Service", "None", "text-muted-foreground") : fact( h, "Service", h.a( [ h.Href(serviceHref), h.Class( typographyClass({ tone: "primary", className: "hover:underline active:underline", }), ), ], [monitor.service_name], ), ), ]) } const snippet = (h: H, model: Model, id: string, label: string, value: string): Html => copyableField(h, { id: `uptime-snippet-${id}`, label, value, copy: model.copies[id] ?? CopyButton.init(), toCopyMessage: (message) => Message.GotCopyMessage({ id, message }), }) /** The ping URL and the lines a job adds: success, failure, and a crontab entry. */ export const heartbeatPanel = (h: H, model: Model, pingUrl: string): Html => panel(h, {}, [ panelHeader(h, { title: "Ping URL" }), uptimePanelBody(h, [ h.p( [h.Class("mb-3 text-sm text-muted-foreground")], ["Send a GET or POST here when the job succeeds. Add /fail to report a failure right away."], ), vstack(h, { gap: "3" }, [ snippet(h, model, "url", "Ping URL", pingUrl), snippet( h, model, "success", "After the job succeeds", `curl -fsS -m 10 --retry 3 ${pingUrl}`, ), snippet(h, model, "fail", "When the job fails", `curl -fsS -m 10 --retry 3 ${pingUrl}/fail`), snippet( h, model, "crontab", "Crontab", `0 2 * * * /path/to/job.sh && curl -fsS -m 10 ${pingUrl} || curl -fsS -m 10 ${pingUrl}/fail`, ), ]), ]), ])