Files
bobbanandClaude Sonnet 5.5 e7f23ba13f 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>
2026-11-12 21:12:47 +01:00

60 lines
2.7 KiB
TypeScript

/**
* How the figures of an integration are written on the page — numbers, sizes, speeds, money — and how a change over a stretch is put into words.
*
* Run with: npm test --workspace server
*/
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { axisFigure, changeWords, formatBytes, formatFigure } from "../../web/src/utils/figureUnits.js";
describe("writing a figure", () => {
test("sizes count in thousands, with a decimal only while it is small", () => {
assert.equal(formatBytes(0), "0 B");
assert.equal(formatBytes(999), "999 B");
assert.equal(formatBytes(1_536), "1.5 kB");
assert.equal(formatBytes(15_360_000), "15 MB");
assert.equal(formatBytes(5e9), "5 GB");
assert.equal(formatBytes(2.5e12), "2.5 TB");
assert.equal(formatBytes(-2_000), "-2 kB");
assert.equal(formatBytes(Number.NaN), "—");
});
test("each unit reads the way it should", () => {
assert.equal(formatFigure("count", 12345), "12,345");
assert.equal(formatFigure("count", 7.04), "7");
assert.equal(formatFigure("percent", 41.56), "41.6%");
assert.equal(formatFigure("ms", 11.5), "11.5 ms");
assert.equal(formatFigure("bytes", 5e9), "5 GB");
assert.equal(formatFigure("bytes_per_s", 5_000_000), "5 MB/s");
assert.equal(formatFigure("money:EUR", 42.4), "€42");
assert.equal(formatFigure("money:XXXX", 42.4), "42 XXXX", "an unknown currency is still a number");
assert.equal(formatFigure("count", Number.POSITIVE_INFINITY), "—");
});
});
describe("a figure in a chart's margin", () => {
test("short, so it fits", () => {
assert.equal(axisFigure("count", 12_500), "12.5K");
assert.equal(axisFigure("count", 950), "950");
assert.equal(axisFigure("percent", 41.6), "42%");
assert.equal(axisFigure("bytes", 340e9), "340 GB");
assert.equal(axisFigure("money:EUR", 1200), "EUR 1.2K");
assert.equal(axisFigure("count", Number.NaN), "");
});
});
describe("how a figure moved", () => {
test("up, down and unchanged, in the unit's own terms", () => {
assert.equal(changeWords("count", 100, 220), "up 120");
assert.equal(changeWords("count", 100, 99), "down 1");
assert.equal(changeWords("count", 100, 100.2), "unchanged");
assert.equal(changeWords("percent", 40, 43.5), "up 3.5 points");
assert.equal(changeWords("percent", 40, 39.98), "unchanged");
assert.equal(changeWords("bytes", 5e9, 5.5e9), "up 500 MB");
assert.equal(changeWords("bytes", 5e9, 5.00001e9), "unchanged", "a rounding-sized change in a big number is not a change");
assert.equal(changeWords("ms", 10, 7), "down 3 ms");
assert.equal(changeWords("count", null, 5), "");
assert.equal(changeWords("count", 5, null), "");
});
});