Repository Public
1// Prepares API series (`[ts_ms, avg, min, max]` points) for plotting: one shared time axis, nulls where a series has2// no sample, and an explicit null inserted after a hole so the line breaks instead of bridging missing data.34export type Point = readonly number[];5export interface SeriesInput {6 id: string;7 label: string;8 points: readonly Point[];9}10export interface Aligned {11 ts: number[];12 series: { id: string; label: string; avg: (number | null)[]; min: (number | null)[]; max: (number | null)[] }[];13}14export interface SeriesSummary {15 id: string;16 label: string;17 count: number;18 min?: number;19 max?: number;20 avg?: number;21 last?: number;22 lastTs?: number;23}2425export function medianStep(points: readonly Point[]): number | undefined {26 if (points.length < 2) return undefined;27 const steps: number[] = [];28 for (let i = 1; i < points.length; i++) steps.push((points[i]?.[0] ?? 0) - (points[i - 1]?.[0] ?? 0));29 steps.sort((a, b) => a - b);30 return steps[Math.floor(steps.length / 2)];31}3233/** A hole is a spacing larger than `factor` × the median spacing of the series. */34export function alignSeries(input: readonly SeriesInput[], gapFactor = 2.5): Aligned {35 const maps: Map<number, Point | null>[] = [];36 const all = new Set<number>();37 for (const s of input) {38 const pts = [...s.points].filter((p) => Number.isFinite(p[0])).sort((a, b) => (a[0] ?? 0) - (b[0] ?? 0));39 const step = medianStep(pts);40 const m = new Map<number, Point | null>();41 pts.forEach((p, i) => {42 const t = p[0] as number;43 const prev = pts[i - 1]?.[0];44 if (step !== undefined && step > 0 && prev !== undefined && t - prev > step * gapFactor) {45 const hole = prev + step;46 if (!m.has(hole)) m.set(hole, null);47 all.add(hole);48 }49 m.set(t, p);50 all.add(t);51 });52 maps.push(m);53 }54 const ts = [...all].sort((a, b) => a - b);55 const val = (p: Point | null | undefined, i: number): number | null => {56 const v = p?.[i];57 return typeof v === "number" && Number.isFinite(v) ? v : null;58 };59 return {60 ts,61 series: input.map((s, k) => {62 const m = maps[k] ?? new Map<number, Point | null>();63 return {64 id: s.id,65 label: s.label,66 avg: ts.map((t) => val(m.get(t), 1)),67 // older servers / raw resolution may send only [ts, value]: the band collapses onto the line68 min: ts.map((t) => val(m.get(t), 2) ?? val(m.get(t), 1)),69 max: ts.map((t) => val(m.get(t), 3) ?? val(m.get(t), 1)),70 };71 }),72 };73}7475export function summarize(input: readonly SeriesInput[]): SeriesSummary[] {76 return input.map((s) => {77 let min = Infinity, max = -Infinity, sum = 0, n = 0, last: number | undefined, lastTs: number | undefined;78 for (const p of s.points) {79 const avg = p[1];80 if (typeof avg !== "number" || !Number.isFinite(avg)) continue;81 const lo = typeof p[2] === "number" ? p[2] : avg;82 const hi = typeof p[3] === "number" ? p[3] : avg;83 min = Math.min(min, lo);84 max = Math.max(max, hi);85 sum += avg;86 n++;87 if (lastTs === undefined || (p[0] ?? 0) >= lastTs) {88 lastTs = p[0];89 last = avg;90 }91 }92 return n === 0 ? { id: s.id, label: s.label, count: 0 } : { id: s.id, label: s.label, count: n, min, max, avg: sum / n, ...(last !== undefined ? { last } : {}), ...(lastTs !== undefined ? { lastTs } : {}) };93 });94}9596/** `#RRGGBB` / `#RRGGBBAA` token value → rgba() with the given alpha (canvas fills for the min/max band). */97export function withAlpha(color: string, alpha: number): string {98 const c = color.trim();99 const m = /^#([0-9a-f]{6})([0-9a-f]{2})?$/i.exec(c);100 if (!m?.[1]) return c;101 const n = parseInt(m[1], 16);102 return `rgba(${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}, ${alpha})`;103}104