Keep a history of integration figures and draw it on each integration's page

The numbers integrations report (queries and blocked share, queue and missing
items, speeds, photos, documents, failed runs, firing rules, targets down,
monthly cost and more) are kept about every quarter of an hour for five weeks,
then as hours and days for up to two years, taken from each integration's
stored reading. Every card on the 14 integration pages gets a Show the
history button with a chart per figure, how it changed and its lowest and
highest. A stretch when the integration could not be read is a gap.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-12 21:12:47 +01:00
1 parent e4f3a54189
commit e7f23ba13f
30 files changed
+10532 -1

No files matched your search

+51
View File
@@ -1991,6 +1991,57 @@ export const storageSamples = sqliteTable(
(t) => [index("storage_samples_series_idx").on(t.seriesId, t.at)],
);
// ─── The figures of an integration, over time ──────────────────────────────
// A number an integration reports (queries a day, items in a queue, photos, documents, a monthly cost), one row for each, so how it has moved can be drawn — see services/integrationFigures.ts.
// `unit` says how to write it (count, percent, ms, bytes, bytes_per_s, money:SEK …); `latest` is the last reading.
export const integrationFigures = sqliteTable(
"integration_figures",
{
id: integer("id").primaryKey({ autoIncrement: true }),
integrationId: integer("integration_id")
.notNull()
.references(() => integrations.id, { onDelete: "cascade" }),
key: text("key").notNull(), // "queries", "queue", "photos" …: stable within a kind of integration
label: text("label").notNull(),
unit: text("unit").notNull(),
latest: real("latest").notNull(),
updatedAt: text("updated_at").notNull(), // ISO; the last reading
},
(t) => [uniqueIndex("integration_figures_key_idx").on(t.integrationId, t.key)],
);
// The readings themselves, one about every quarter of an hour, kept five weeks.
export const integrationFigureSamples = sqliteTable(
"integration_figure_samples",
{
id: integer("id").primaryKey({ autoIncrement: true }),
figureId: integer("figure_id")
.notNull()
.references(() => integrationFigures.id, { onDelete: "cascade" }),
at: text("at").notNull(), // ISO
value: real("value").notNull(),
},
(t) => [index("integration_figure_samples_idx").on(t.figureId, t.at)],
);
// The same, coarser and kept longer: one row for each figure and hour (grain "h", four months) or day (grain "d", over two years), like the servers' (server_metrics_rollup).
export const integrationFigureRollup = sqliteTable(
"integration_figure_rollup",
{
figureId: integer("figure_id")
.notNull()
.references(() => integrationFigures.id, { onDelete: "cascade" }),
grain: text("grain").$type<"h" | "d">().notNull(),
at: text("at").notNull(),
sum: real("sum").notNull(),
n: integer("n").notNull(),
min: real("min").notNull(),
max: real("max").notNull(),
},
(t) => [primaryKey({ columns: [t.figureId, t.grain, t.at] })],
);
// "Remind me tomorrow at 09:00": an alert hidden until a time, and announced again then if it is still there — see services/alertSnoozes.ts. Keyed like an acknowledgement.
export const alertSnoozes = sqliteTable(
"alert_snoozes",
+2
View File
@@ -47,6 +47,7 @@ import { initTechnitiumSync } from "./services/technitiumSync.js";
import { initDownloadsSync } from "./services/downloadsSync.js";
import { initHetznerRobotSync } from "./services/hetznerRobotSync.js";
import { initCostHistory } from "./services/costHistory.js";
import { initIntegrationFigures } from "./services/integrationFigures.js";
import { initGrafanaSync } from "./services/grafanaSync.js";
import { initVersionCheck } from "./services/appVersions.js";
import { backfillMetricRollups } from "./services/serverMetrics.js";
@@ -117,6 +118,7 @@ initTechnitiumSync();
initDownloadsSync();
initHetznerRobotSync();
initCostHistory();
initIntegrationFigures();
initGrafanaSync();
initVersionCheck();
// The hourly and daily figures are built from what is already kept, once, the first time after they were added (in the background: nothing waits for it).
+12
View File
@@ -30,6 +30,7 @@ import { getSourceStatus } from "../services/integrationHealthMonitor.js";
import { dockerVulnSyncStatuses, findingsOf, imageAgeDays, imagesOf, isDockerVulnFresh } from "../services/dockerVulnSync.js";
import { invalidateAlertsCache } from "../services/alerts.js";
import { forgetReading, readAfterChange, readIntegrationNow } from "../services/integrationSyncs.js";
import { getIntegrationFigures, hasFigures } from "../services/integrationFigures.js";
import { getSettings, updateSettings } from "../services/settingsStore.js";
import { asyncHandler } from "../utils/asyncHandler.js";
import { ID_PATTERNS, isValidId } from "../utils/pathSegment.js";
@@ -293,6 +294,17 @@ integrationsRouter.post("/:id/test", requireRole("admin"), asyncHandler(async (r
// Reads one integration now, instead of waiting for its next turn: a kind with a stored reading is read the way its schedule reads it (and what it says is judged again at once); a kind that is read live
// is asked whether it answers. Operators and administrators — it only reads, but some systems limit how often they may be asked, so the same integration is not read again within 20 seconds.
// How the figures an integration reports (queries a day, items in a queue, photos, documents …) have moved: a day to a year, drawn from what is kept (services/integrationFigures.ts). Anyone signed in.
integrationsRouter.get("/:id/figures", asyncHandler(async (req, res) => {
const id = Number(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
const [row] = await db.select({ type: integrations.type }).from(integrations).where(eq(integrations.id, id)).limit(1);
if (!row) return res.status(404).json({ error: "not_found" });
const asked = Number(req.query.days);
const days = Number.isFinite(asked) ? asked : 7;
res.json({ kept: hasFigures(row.type), days: Math.min(365, Math.max(1, Math.round(days))), figures: hasFigures(row.type) ? await getIntegrationFigures(id, days) : [] });
}));
integrationsRouter.post("/:id/read-now", requireRole("operator"), asyncHandler(async (req, res) => {
const id = Number(req.params.id);
if (!Number.isInteger(id)) return res.status(400).json({ error: "invalid_id" });
+375
View File
@@ -0,0 +1,375 @@
/**
* A history of the numbers integrations report — queries a day and how many were blocked, items waiting in a download queue, photos, documents, failed runs, the monthly cost at Hetzner — so a page
* can show how a figure has moved instead of only what it is now.
*
* What is kept is what each integration's own reading already stores (its "state" row, read on a schedule by its sync): every five minutes this looks at each enabled integration that has one, takes
* its figures out of it (the `extract*` functions below, one for each kind), and keeps one reading of each about every quarter of an hour for five weeks — and, beyond that, its hours (four months) and
* days (over two years) as a sum, a count, a lowest and a highest, as the servers' figures are (services/serverMetrics.ts, whose way of drawing them this shares). A reading that is not recent — the
* integration could not be read for a while — is not recorded, so a gap is a gap and not a flat line.
*
* What it does not cover: the integrations that already keep their own history (UPS, Home Assistant, speedtest, who is streaming from Jellyfin, the storage of Proxmox and Synology) and those that
* are only read live when a page is opened (Proxmox, Synology, Tailscale, Gitea, Semaphore, Uptime Kuma …) — there is nothing stored to take figures from.
*/
import { and, eq, gte, lt, sql } from "drizzle-orm";
import { db } from "../db/client.js";
import { adguardState, arrState, authentikState, downloadState, grafanaState, hetznerState, immichState, integrationFigureRollup, integrationFigureSamples, integrationFigures, integrations, jellyfinState, n8nState, netboxState, nextcloudState, paperlessState, prometheusState, technitiumState, type IntegrationType } from "../db/schema.js";
import { ARR_KINDS } from "../integrations/arr/adapter.js";
import { DOWNLOAD_KINDS } from "../integrations/downloads/adapter.js";
import { bucketPoints, bucketRollups, DAILY_KEEP_DAYS, dayStart, HOURLY_KEEP_DAYS, hourStart, MAX_RANGE_DAYS, RETENTION_DAYS, sourceFor, type Point, type RollupRow } from "./serverMetrics.js";
const DAY_MS = 24 * 3600_000;
/** Readings closer together than this add no sample (a read twice, or two passes over the same read). */
export const MIN_GAP_MS = 14 * 60_000;
/** A reading older than this is not recorded: it says nothing about now. */
export const STALE_MS = 45 * 60_000;
export const PASS_INTERVAL_MS = 5 * 60_000;
/** How a figure is written: a plain count, a percentage, milliseconds, bytes, bytes a second, or money in a currency ("money:SEK"). */
export type FigureUnit = "count" | "percent" | "ms" | "bytes" | "bytes_per_s" | `money:${string}`;
export interface Figure {
key: string;
label: string;
unit: FigureUnit;
value: number;
}
// ─── What each kind of integration has ─────────────────────────────────────
const parse = <T>(s: string | null | undefined): T | null => {
if (!s) return null;
try {
return JSON.parse(s) as T;
} catch {
return null;
}
};
const isNum = (n: unknown): n is number => typeof n === "number" && Number.isFinite(n);
const round2 = (n: number) => Math.round(n * 100) / 100;
/** Keeps the figures that are numbers: a column that is null could not be read, which is not the same as zero. */
function figures(list: [key: string, label: string, unit: FigureUnit, value: unknown][]): Figure[] {
return list.filter((f): f is [string, string, FigureUnit, number] => isNum(f[3])).map(([key, label, unit, value]) => ({ key, label, unit, value }));
}
type Row<T extends { $inferSelect: unknown }> = T["$inferSelect"];
export const extractAdguard = (r: Pick<Row<typeof adguardState>, "stats" | "probe">): Figure[] => {
const s = parse<{ queries?: number; blocked?: number; blockedPercent?: number | null; avgMs?: number | null }>(r.stats);
const p = parse<{ ok?: boolean; ms?: number | null }>(r.probe);
return figures([
["queries", "DNS queries", "count", s?.queries],
["blocked", "Blocked", "count", s?.blocked],
["blockedPercent", "Blocked share", "percent", s?.blockedPercent],
["avgMs", "Average answer time", "ms", s?.avgMs],
["probeMs", "Test question answered in", "ms", p?.ok ? p.ms : null],
]);
};
export const extractTechnitium = (r: Pick<Row<typeof technitiumState>, "stats" | "probe">): Figure[] => {
const s = parse<{ queries?: number; blocked?: number; blockedPercent?: number | null; clients?: number; cachedEntries?: number | null }>(r.stats);
const p = parse<{ ok?: boolean; ms?: number | null }>(r.probe);
return figures([
["queries", "DNS queries", "count", s?.queries],
["blocked", "Blocked", "count", s?.blocked],
["blockedPercent", "Blocked share", "percent", s?.blockedPercent],
["clients", "Clients", "count", s?.clients],
["cachedEntries", "Cache entries", "count", s?.cachedEntries],
["probeMs", "Test question answered in", "ms", p?.ok ? p.ms : null],
]);
};
export const extractArr = (r: Pick<Row<typeof arrState>, "queue" | "missing">): Figure[] =>
figures([
["queue", "In the download queue", "count", parse<{ total?: number }>(r.queue)?.total],
["missing", "Wanted, missing", "count", r.missing],
]);
export const extractDownload = (r: Pick<Row<typeof downloadState>, "speedDown" | "speedUp" | "counts" | "total">): Figure[] => {
const c = parse<Record<string, number>>(r.counts);
const pending = c ? r.total - (c.seeding ?? 0) - (c.completed ?? 0) - (c.failed ?? 0) : null;
return figures([
["speedDown", "Download speed", "bytes_per_s", r.speedDown],
["speedUp", "Upload speed", "bytes_per_s", r.speedUp],
["downloading", "Downloading", "count", c?.downloading],
["pending", "Not finished", "count", pending],
["failed", "Failed", "count", c?.failed],
]);
};
export const extractPaperless = (r: Pick<Row<typeof paperlessState>, "documents" | "inbox" | "storageTotal" | "storageAvailable">): Figure[] =>
figures([
["documents", "Documents", "count", r.documents],
["inbox", "In the inbox", "count", r.inbox],
["storagePercent", "Storage used", "percent", isNum(r.storageTotal) && r.storageTotal > 0 && isNum(r.storageAvailable) ? round2(((r.storageTotal - r.storageAvailable) / r.storageTotal) * 100) : null],
]);
export const extractImmich = (r: Pick<Row<typeof immichState>, "photos" | "videos" | "usageBytes" | "diskPercent" | "queues">): Figure[] => {
const q = parse<{ waiting?: number; failed?: number }[]>(r.queues);
const sum = (k: "waiting" | "failed") => (Array.isArray(q) ? q.reduce((n, x) => n + (isNum(x?.[k]) ? (x[k] as number) : 0), 0) : null);
return figures([
["photos", "Photos", "count", r.photos],
["videos", "Videos", "count", r.videos],
["usageBytes", "Library size", "bytes", r.usageBytes],
["diskPercent", "Disk used", "percent", r.diskPercent],
["waiting", "Jobs waiting", "count", sum("waiting")],
["failedJobs", "Jobs failed", "count", sum("failed")],
]);
};
export const extractNextcloud = (r: Pick<Row<typeof nextcloudState>, "users" | "files" | "active24h" | "freeBytes">): Figure[] =>
figures([
["users", "Users", "count", r.users],
["active24h", "Active in the last day", "count", r.active24h],
["files", "Files", "count", r.files],
["freeBytes", "Free space", "bytes", r.freeBytes],
]);
export const extractNetbox = (r: Pick<Row<typeof netboxState>, "devices" | "virtualMachines" | "ipAddresses">): Figure[] =>
figures([
["devices", "Devices", "count", r.devices],
["virtualMachines", "Virtual machines", "count", r.virtualMachines],
["ipAddresses", "IP addresses", "count", r.ipAddresses],
]);
export const extractN8n = (r: Pick<Row<typeof n8nState>, "workflows" | "executionsKnown">): Figure[] => {
const w = parse<{ active?: boolean; failed24h?: number }[]>(r.workflows);
if (!Array.isArray(w)) return [];
return figures([
["active", "Active workflows", "count", w.filter((x) => x?.active).length],
["failed24h", "Failed runs in the last day", "count", r.executionsKnown ? w.reduce((n, x) => n + (isNum(x?.failed24h) ? (x.failed24h as number) : 0), 0) : null],
]);
};
export const extractGrafana = (r: Pick<Row<typeof grafanaState>, "rules">): Figure[] => {
const rules = parse<{ total?: number; items?: { state?: string; health?: string }[] }>(r.rules);
if (!rules) return [];
const items = Array.isArray(rules.items) ? rules.items : [];
return figures([
["rules", "Alert rules", "count", rules.total],
["firing", "Firing", "count", items.filter((i) => i?.state === "firing").length],
["broken", "Cannot be evaluated", "count", items.filter((i) => i?.health === "error").length],
]);
};
export const extractPrometheus = (r: Pick<Row<typeof prometheusState>, "jobs" | "down" | "alerts">): Figure[] => {
const jobs = parse<{ up?: number; down?: number }[]>(r.jobs);
const alerts = parse<{ state?: string }[]>(r.alerts);
return figures([
["targetsUp", "Targets up", "count", Array.isArray(jobs) ? jobs.reduce((n, j) => n + (isNum(j?.up) ? (j.up as number) : 0), 0) : null],
["targetsDown", "Targets down", "count", Array.isArray(jobs) ? jobs.reduce((n, j) => n + (isNum(j?.down) ? (j.down as number) : 0), 0) : null],
["firing", "Alerts firing", "count", Array.isArray(alerts) ? alerts.filter((a) => a?.state === "firing").length : null],
]);
};
export const extractAuthentik = (r: Pick<Row<typeof authentikState>, "failedLogins24h" | "configErrors24h">): Figure[] =>
figures([
["failedLogins24h", "Failed sign-ins in the last day", "count", r.failedLogins24h],
["configErrors24h", "Configuration errors in the last day", "count", r.configErrors24h],
]);
export const extractJellyfin = (r: Pick<Row<typeof jellyfinState>, "movies" | "series" | "episodes" | "songs" | "failedLogins24h">): Figure[] =>
figures([
["movies", "Films", "count", r.movies],
["series", "Series", "count", r.series],
["episodes", "Episodes", "count", r.episodes],
["songs", "Songs", "count", r.songs],
["failedLogins24h", "Failed sign-ins in the last day", "count", r.failedLogins24h],
]);
export const extractHetzner = (r: Pick<Row<typeof hetznerState>, "servers" | "cost">): Figure[] => {
const cost = parse<{ currency?: string; total?: number }>(r.cost);
const servers = parse<unknown[]>(r.servers);
return figures([
["servers", "Servers", "count", Array.isArray(servers) ? servers.length : null],
["cost", "Monthly cost, estimated", `money:${typeof cost?.currency === "string" && /^[A-Za-z]{3}$/.test(cost.currency) ? cost.currency.toUpperCase() : "EUR"}` as FigureUnit, cost?.total],
]);
};
interface Source {
/** The stored reading of one integration (null when it has none), with when it was read and its figures. */
load: (integrationId: number) => Promise<{ at: string; figures: Figure[] } | null>;
}
/** A source that reads one table's row for the integration and runs an extractor over it. */
// (`extract` is checked where it is written, against the columns it reads; here it is handed the whole row.)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function source<R extends { updatedAt: string }>(read: (integrationId: number) => Promise<R | undefined>, extract: (row: any) => Figure[]): Source {
return {
load: async (id) => {
const row = await read(id);
return row ? { at: row.updatedAt, figures: extract(row) } : null;
},
};
}
const SOURCES: Partial<Record<IntegrationType, Source>> = {
adguard: source(async (id) => (await db.select().from(adguardState).where(eq(adguardState.integrationId, id)).limit(1))[0], extractAdguard),
technitium: source(async (id) => (await db.select().from(technitiumState).where(eq(technitiumState.integrationId, id)).limit(1))[0], extractTechnitium),
paperless: source(async (id) => (await db.select().from(paperlessState).where(eq(paperlessState.integrationId, id)).limit(1))[0], extractPaperless),
immich: source(async (id) => (await db.select().from(immichState).where(eq(immichState.integrationId, id)).limit(1))[0], extractImmich),
nextcloud: source(async (id) => (await db.select().from(nextcloudState).where(eq(nextcloudState.integrationId, id)).limit(1))[0], extractNextcloud),
netbox: source(async (id) => (await db.select().from(netboxState).where(eq(netboxState.integrationId, id)).limit(1))[0], extractNetbox),
n8n: source(async (id) => (await db.select().from(n8nState).where(eq(n8nState.integrationId, id)).limit(1))[0], extractN8n),
grafana: source(async (id) => (await db.select().from(grafanaState).where(eq(grafanaState.integrationId, id)).limit(1))[0], extractGrafana),
prometheus: source(async (id) => (await db.select().from(prometheusState).where(eq(prometheusState.integrationId, id)).limit(1))[0], extractPrometheus),
authentik: source(async (id) => (await db.select().from(authentikState).where(eq(authentikState.integrationId, id)).limit(1))[0], extractAuthentik),
jellyfin: source(async (id) => (await db.select().from(jellyfinState).where(eq(jellyfinState.integrationId, id)).limit(1))[0], extractJellyfin),
hetzner: source(async (id) => (await db.select().from(hetznerState).where(eq(hetznerState.integrationId, id)).limit(1))[0], extractHetzner),
};
for (const k of ARR_KINDS) SOURCES[k] = source(async (id) => (await db.select().from(arrState).where(eq(arrState.integrationId, id)).limit(1))[0], extractArr);
for (const k of DOWNLOAD_KINDS) SOURCES[k] = source(async (id) => (await db.select().from(downloadState).where(eq(downloadState.integrationId, id)).limit(1))[0], extractDownload);
/** Whether figures are kept for this kind of integration. */
export const hasFigures = (type: IntegrationType): boolean => SOURCES[type] !== undefined;
// ─── Recording ─────────────────────────────────────────────────────────────
/** Adds a reading to its hour and day. Never allowed to break recording the reading itself. */
async function addToRollups(figureId: number, value: number, at: Date): Promise<void> {
try {
for (const grain of ["h", "d"] as const) {
const bucket = grain === "h" ? hourStart(at.getTime()) : dayStart(at.getTime());
await db
.insert(integrationFigureRollup)
.values({ figureId, grain, at: bucket, sum: value, n: 1, min: value, max: value })
.onConflictDoUpdate({
target: [integrationFigureRollup.figureId, integrationFigureRollup.grain, integrationFigureRollup.at],
set: {
sum: sql`${integrationFigureRollup.sum} + ${value}`,
n: sql`${integrationFigureRollup.n} + 1`,
min: sql`min(${integrationFigureRollup.min}, ${value})`,
max: sql`max(${integrationFigureRollup.max}, ${value})`,
},
});
}
} catch (err) {
console.error("[figures] couldn't add to the hourly and daily figures:", err);
}
}
/** Keeps one reading of each figure of one integration, as of `at` (when it was read). Returns how many were stored (those too close to the last one are not). */
export async function recordFigures(integrationId: number, list: Figure[], at: Date): Promise<number> {
let stored = 0;
const iso = at.toISOString();
for (const f of list) {
const [row] = await db
.insert(integrationFigures)
.values({ integrationId, key: f.key, label: f.label, unit: f.unit, latest: f.value, updatedAt: iso })
.onConflictDoUpdate({ target: [integrationFigures.integrationId, integrationFigures.key], set: { label: f.label, unit: f.unit, latest: f.value, updatedAt: iso } })
.returning({ id: integrationFigures.id });
const [last] = await db
.select({ at: sql<string>`max(${integrationFigureSamples.at})` })
.from(integrationFigureSamples)
.where(eq(integrationFigureSamples.figureId, row.id));
if (last?.at && Math.abs(at.getTime() - Date.parse(last.at)) < MIN_GAP_MS) continue;
await db.insert(integrationFigureSamples).values({ figureId: row.id, at: iso, value: f.value });
await addToRollups(row.id, f.value, at);
stored++;
}
return stored;
}
/** Throws away what is older than it is kept: readings after five weeks, hours after four months, days after about two years and a bit. */
export async function pruneFigures(now: Date = new Date()): Promise<void> {
const before = (days: number) => new Date(now.getTime() - days * DAY_MS).toISOString();
await db.delete(integrationFigureSamples).where(lt(integrationFigureSamples.at, before(RETENTION_DAYS)));
await db.delete(integrationFigureRollup).where(and(eq(integrationFigureRollup.grain, "h"), lt(integrationFigureRollup.at, before(HOURLY_KEEP_DAYS))));
await db.delete(integrationFigureRollup).where(and(eq(integrationFigureRollup.grain, "d"), lt(integrationFigureRollup.at, before(DAILY_KEEP_DAYS))));
}
/** One pass: every enabled integration with a stored reading that is recent has its figures kept. Returns how many readings were stored. */
export async function recordAllFigures(now: Date = new Date()): Promise<number> {
let stored = 0;
for (const i of await db.select({ id: integrations.id, type: integrations.type }).from(integrations).where(eq(integrations.enabled, true))) {
const src = SOURCES[i.type];
if (!src) continue;
try {
const reading = await src.load(i.id);
if (!reading) continue;
const readAt = Date.parse(reading.at);
if (!Number.isFinite(readAt) || now.getTime() - readAt > STALE_MS || readAt > now.getTime() + 60_000) continue;
stored += await recordFigures(i.id, reading.figures, new Date(readAt));
} catch (err) {
console.error(`[figures] integration ${i.id}:`, err);
}
}
await pruneFigures(now);
return stored;
}
// ─── Reading ───────────────────────────────────────────────────────────────
export interface FigureSeries {
key: string;
label: string;
unit: FigureUnit;
/** The latest reading, whatever stretch is asked for. */
latest: number;
/** ISO — when it was read. */
readAt: string;
points: Point[];
/** The first point of the stretch, the lowest and the highest figure in it (null with no points). */
first: number | null;
low: number | null;
high: number | null;
}
/** How many points a chart over `days` is drawn from at most. */
const pointsFor = (days: number) => (days <= RETENTION_DAYS ? 96 : days <= HOURLY_KEEP_DAYS ? 200 : MAX_RANGE_DAYS + 5);
/** Every figure kept for an integration over the last `days` (up to a year), ready to draw; the ones with no point in the stretch are left out. */
export async function getIntegrationFigures(integrationId: number, days: number, now: Date = new Date()): Promise<FigureSeries[]> {
days = Math.min(MAX_RANGE_DAYS, Math.max(1, Math.round(days)));
const to = now.getTime();
const from = to - days * DAY_MS;
const kind = sourceFor(days);
const series = await db.select().from(integrationFigures).where(eq(integrationFigures.integrationId, integrationId)).orderBy(integrationFigures.id);
const out: FigureSeries[] = [];
for (const f of series) {
let points: Point[];
if (kind === "samples") {
const rows = await db
.select({ at: integrationFigureSamples.at, value: integrationFigureSamples.value })
.from(integrationFigureSamples)
.where(and(eq(integrationFigureSamples.figureId, f.id), gte(integrationFigureSamples.at, new Date(from).toISOString())))
.orderBy(integrationFigureSamples.at);
points = bucketPoints(rows.map((r) => ({ t: Date.parse(r.at), v: r.value })), from, to, pointsFor(days));
} else {
const rows = await db
.select()
.from(integrationFigureRollup)
.where(and(eq(integrationFigureRollup.figureId, f.id), eq(integrationFigureRollup.grain, kind === "hourly" ? "h" : "d"), gte(integrationFigureRollup.at, new Date(from).toISOString())))
.orderBy(integrationFigureRollup.at);
const list: RollupRow[] = rows.map((r) => ({ t: Date.parse(r.at), sum: r.sum, n: r.n, min: r.min, max: r.max }));
points = bucketRollups(list, from, to, pointsFor(days));
}
if (points.length === 0) continue;
out.push({
key: f.key,
label: f.label,
unit: f.unit as FigureUnit,
latest: f.latest,
readAt: f.updatedAt,
points,
first: points[0].v,
low: Math.min(...points.map((p) => p.lo ?? p.v)),
high: Math.max(...points.map((p) => p.hi ?? p.v)),
});
}
return out;
}
// ─── Running ───────────────────────────────────────────────────────────────
let timer: ReturnType<typeof setInterval> | null = null;
export function initIntegrationFigures(): void {
if (timer) return;
const pass = () => void recordAllFigures().catch((err) => console.error("[figures] couldn't keep the history:", err));
timer = setInterval(pass, PASS_INTERVAL_MS);
timer.unref();
setTimeout(pass, 75_000).unref();
console.log("[figures] keeping the history of integration figures");
}