Add small trend lines to the rows of the server, endpoint and integration lists
Server cards show load and memory over the last 24 hours, endpoint checks show their answer times with a tick where a check failed, and integrations that keep a figure history show their headline figure over the last week. Each list asks for all its lines in one request, drawn from the history already kept, and each line has a sentence for screen readers. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5750350565
commit
639408c0d0
15 files changed
+707
No files matched your search
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* Short series for the small lines in the rows of a list — a server's load and memory over the last day, how long an endpoint took to answer (and when it did not), the figure an integration is
|
||||
* known for over the last week — so a list says not only how things are but which way they are going. Every one is worked out for all rows at once, in the database, so a list asks once.
|
||||
*
|
||||
* A series is a fixed number of equal slices from `from` to now, each the average of what was read in it, or null where nothing was (a gap is a gap, not a line through it). Nothing here is new data:
|
||||
* it is the history the app already keeps (services/serverMetrics.ts, services/endpointChecks.ts, services/integrationFigures.ts).
|
||||
*/
|
||||
import { sql } from "drizzle-orm";
|
||||
import { db } from "../db/client.js";
|
||||
|
||||
export const SLICES = 24;
|
||||
const HOUR_MS = 3600_000;
|
||||
|
||||
const round1 = (n: number) => Math.round(n * 10) / 10;
|
||||
|
||||
/** A column holding an ISO time, as the number of the slice it falls in (0 at `fromMs`): the work done in SQL so a list is one query. */
|
||||
const sliceOf = (column: string, fromMs: number, sliceMs: number) => sql.raw(`cast((strftime('%s', ${column}) * 1000 - ${Math.round(fromMs)}) / ${Math.round(sliceMs)} as integer)`);
|
||||
|
||||
/** Slices of (slice number → value) into a full list of `n` values, null where there is none. Pure. */
|
||||
export function fillSlices(found: Map<number, number>, n: number = SLICES): (number | null)[] {
|
||||
return Array.from({ length: n }, (_, i) => (found.has(i) ? round1(found.get(i)!) : null));
|
||||
}
|
||||
|
||||
export interface Spark {
|
||||
/** `SLICES` values, oldest first; null for a slice with nothing in it. */
|
||||
values: (number | null)[];
|
||||
/** The last value there is. */
|
||||
latest: number | null;
|
||||
low: number | null;
|
||||
high: number | null;
|
||||
}
|
||||
|
||||
/** A series with its last, lowest and highest value worked out; null when there is not one value in it. */
|
||||
export function sparkOf(values: (number | null)[]): Spark | null {
|
||||
const real = values.filter((v): v is number => v !== null);
|
||||
if (real.length === 0) return null;
|
||||
return { values, latest: real[real.length - 1], low: Math.min(...real), high: Math.max(...real) };
|
||||
}
|
||||
|
||||
// ─── Servers ──────────────────────────────────────────────────────────────
|
||||
|
||||
export type ServerSparklines = Record<number, { cpu?: Spark; mem?: Spark }>;
|
||||
|
||||
/** Load and memory of every server that reports them, over the last `hours` (an agent-reported figure, as a percentage). */
|
||||
export async function serverSparklines(now: Date = new Date(), hours = 24): Promise<ServerSparklines> {
|
||||
const to = now.getTime();
|
||||
const from = to - hours * HOUR_MS;
|
||||
const sliceMs = (hours * HOUR_MS) / SLICES;
|
||||
const rows = await db.all<{ server_id: number; metric: string; slice: number; v: number }>(sql`
|
||||
SELECT server_id, metric, ${sliceOf("recorded_at", from, sliceMs)} AS slice, avg(value) AS v
|
||||
FROM server_metrics
|
||||
WHERE recorded_at >= ${new Date(from).toISOString()} AND metric IN ('cpu', 'mem')
|
||||
GROUP BY server_id, metric, slice`);
|
||||
const grouped = new Map<string, Map<number, number>>();
|
||||
for (const r of rows) {
|
||||
if (r.slice < 0 || r.slice >= SLICES) continue;
|
||||
const key = `${r.server_id}:${r.metric}`;
|
||||
if (!grouped.has(key)) grouped.set(key, new Map());
|
||||
grouped.get(key)!.set(r.slice, r.v);
|
||||
}
|
||||
const out: ServerSparklines = {};
|
||||
for (const [key, found] of grouped) {
|
||||
const [id, metric] = key.split(":");
|
||||
const spark = sparkOf(fillSlices(found));
|
||||
if (!spark) continue;
|
||||
(out[Number(id)] ??= {})[metric as "cpu" | "mem"] = spark;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ─── Endpoints ────────────────────────────────────────────────────────────
|
||||
|
||||
export interface EndpointSpark extends Spark {
|
||||
/** For each slice: at least one check in it failed. */
|
||||
failed: boolean[];
|
||||
}
|
||||
export type EndpointSparklines = Record<number, EndpointSpark>;
|
||||
|
||||
/** How long each endpoint check took to answer over the last `hours` (milliseconds), and in which slices a check failed. */
|
||||
export async function endpointSparklines(now: Date = new Date(), hours = 24): Promise<EndpointSparklines> {
|
||||
const to = now.getTime();
|
||||
const from = to - hours * HOUR_MS;
|
||||
const sliceMs = (hours * HOUR_MS) / SLICES;
|
||||
const rows = await db.all<{ check_id: number; slice: number; ms: number | null; bad: number }>(sql`
|
||||
SELECT check_id, ${sliceOf("at", from, sliceMs)} AS slice, avg(CASE WHEN ok = 1 THEN ms END) AS ms, sum(CASE WHEN ok = 0 THEN 1 ELSE 0 END) AS bad
|
||||
FROM endpoint_results
|
||||
WHERE at >= ${new Date(from).toISOString()}
|
||||
GROUP BY check_id, slice`);
|
||||
const ms = new Map<number, Map<number, number>>();
|
||||
const bad = new Map<number, Set<number>>();
|
||||
for (const r of rows) {
|
||||
if (r.slice < 0 || r.slice >= SLICES) continue;
|
||||
if (!ms.has(r.check_id)) ms.set(r.check_id, new Map());
|
||||
if (r.ms !== null) ms.get(r.check_id)!.set(r.slice, r.ms);
|
||||
if (r.bad > 0) (bad.get(r.check_id) ?? bad.set(r.check_id, new Set()).get(r.check_id)!).add(r.slice);
|
||||
}
|
||||
const out: EndpointSparklines = {};
|
||||
for (const id of new Set([...ms.keys(), ...bad.keys()])) {
|
||||
const values = fillSlices(ms.get(id) ?? new Map());
|
||||
const failed = Array.from({ length: SLICES }, (_, i) => bad.get(id)?.has(i) ?? false);
|
||||
const spark = sparkOf(values) ?? { values, latest: null, low: null, high: null };
|
||||
out[id] = { ...spark, failed };
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// ─── Integrations ─────────────────────────────────────────────────────────
|
||||
|
||||
/** Which figure stands for an integration in a list: the first of these it has, else the first it was given. */
|
||||
export const HEADLINE_ORDER = ["cost", "queries", "queue", "speedDown", "photos", "documents", "users", "devices", "failed24h", "targetsDown", "firing", "failedLogins24h", "movies", "active"];
|
||||
|
||||
export interface IntegrationSpark extends Spark {
|
||||
key: string;
|
||||
label: string;
|
||||
unit: string;
|
||||
}
|
||||
export type IntegrationSparklines = Record<number, IntegrationSpark>;
|
||||
|
||||
/** Pure: the figure that stands for an integration, from the ones it has (in the order they were first kept). */
|
||||
export function headlineOf<T extends { key: string }>(figures: T[]): T | null {
|
||||
for (const key of HEADLINE_ORDER) {
|
||||
const f = figures.find((x) => x.key === key);
|
||||
if (f) return f;
|
||||
}
|
||||
return figures[0] ?? null;
|
||||
}
|
||||
|
||||
/** The headline figure of every integration that keeps history, over the last `days`. */
|
||||
export async function integrationSparklines(now: Date = new Date(), days = 7): Promise<IntegrationSparklines> {
|
||||
const to = now.getTime();
|
||||
const from = to - days * 24 * HOUR_MS;
|
||||
const sliceMs = (days * 24 * HOUR_MS) / SLICES;
|
||||
const figures = await db.all<{ id: number; integration_id: number; key: string; label: string; unit: string }>(sql`SELECT id, integration_id, key, label, unit FROM integration_figures ORDER BY id`);
|
||||
const byIntegration = new Map<number, typeof figures>();
|
||||
for (const f of figures) (byIntegration.get(f.integration_id) ?? byIntegration.set(f.integration_id, []).get(f.integration_id)!).push(f);
|
||||
const chosen = new Map<number, (typeof figures)[number]>();
|
||||
for (const [integrationId, list] of byIntegration) {
|
||||
const pick = headlineOf(list);
|
||||
if (pick) chosen.set(pick.id, pick);
|
||||
void integrationId;
|
||||
}
|
||||
if (chosen.size === 0) return {};
|
||||
const rows = await db.all<{ figure_id: number; slice: number; v: number }>(sql`
|
||||
SELECT figure_id, ${sliceOf("at", from, sliceMs)} AS slice, avg(value) AS v
|
||||
FROM integration_figure_samples
|
||||
WHERE at >= ${new Date(from).toISOString()}
|
||||
GROUP BY figure_id, slice`);
|
||||
const found = new Map<number, Map<number, number>>();
|
||||
for (const r of rows) {
|
||||
if (!chosen.has(r.figure_id) || r.slice < 0 || r.slice >= SLICES) continue;
|
||||
if (!found.has(r.figure_id)) found.set(r.figure_id, new Map());
|
||||
found.get(r.figure_id)!.set(r.slice, r.v);
|
||||
}
|
||||
const out: IntegrationSparklines = {};
|
||||
for (const [figureId, f] of chosen) {
|
||||
const spark = sparkOf(fillSlices(found.get(figureId) ?? new Map()));
|
||||
if (spark) out[f.integration_id] = { ...spark, key: f.key, label: f.label, unit: f.unit };
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Reference in new issue
Block a user