The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
import {
DATA_APP_DIAGNOSTICS_CALL_LIMIT,
DATA_APP_DIAGNOSTICS_LIMIT,
} from "../constants/diagnostics-channel";
import type { DataAppDiagnosticEntry } from "../types/diagnostics-channel";
import { DiagnosticsStore } from "./diagnostics-store";
const entry = (
over: Partial<DataAppDiagnosticEntry> = {},
): DataAppDiagnosticEntry => ({
time: 1700000000000,
buildId: 1,
kind: "error",
summary: "boom",
detail: null,
hint: null,
alert: true,
...over,
});
// The page stamps each load with a `sessionId`; a new one is how the server
// tells a reload (fresh id) from a soft HMR re-render (same id).
const message = (entries: DataAppDiagnosticEntry[], sessionId = "page-1") => ({
sessionId,
entries,
connection: null,
});
describe("DiagnosticsStore", () => {
it("stamps ids from one, so a poller can start at the beginning", () => {
const store = new DiagnosticsStore();
store.applyMessage(message([entry({ summary: "first" })]));
store.applyMessage(message([entry({ summary: "second" })]));
expect(
store.getReport(0).entries.map((e) => [e.eventId, e.summary]),
).toEqual([
[1, "first"],
[2, "second"],
]);
expect(store.getReport(0).nextEventId).toBe(3);
});
it("keeps ids climbing across a reload so a poller's cursor stays valid", () => {
const store = new DiagnosticsStore();
store.applyMessage(message([entry({ summary: "old page" })], "page-1"));
store.applyMessage(message([entry({ summary: "new page" })], "page-2"));
// Restarting at 1 would put the new page's events *behind* a cursor that
// already advanced, and the toolbar would look healthy while going blank.
const [, second] = store.getReport(0).entries;
expect(second.summary).toBe("new page");
expect(second.eventId).toBe(2);
});
it("stamps each entry with the page load that reported it", () => {
const store = new DiagnosticsStore();
store.applyMessage(message([entry({ summary: "old page" })], "page-1"));
store.applyMessage(message([entry({ summary: "new page" })], "page-2"));
// The toolbar shows only its own page's entries; the buffer keeps both, so
// a shell reader paging with `?startEventId=` still sees the older ones.
expect(
store.getReport(0).entries.map((e) => [e.sessionId, e.summary]),
).toEqual([
["page-1", "old page"],
["page-2", "new page"],
]);
});
it("keeps the previous page's events across a reload", () => {
const store = new DiagnosticsStore();
store.applyMessage(
message([entry({ summary: "before reload" })], "page-1"),
);
store.applyMessage(message([entry({ summary: "after reload" })], "page-2"));
// The errors that prompted the reload are the ones a reader most needs.
expect(store.getReport(0).entries.map((e) => e.summary)).toEqual([
"before reload",
"after reload",
]);
expect(store.getReport(0).sessionId).toBe("page-2");
});
it("serves only the new page's events to a reader that carried its cursor", () => {
const store = new DiagnosticsStore();
store.applyMessage(
message([entry({ summary: "before reload" })], "page-1"),
);
const cursor = store.getReport(0).nextEventId;
store.applyMessage(message([entry({ summary: "after reload" })], "page-2"));
// How the toolbar shows post-reload entries only, without the buffer
// having to forget anything.
expect(store.getReport(cursor).entries.map((e) => e.summary)).toEqual([
"after reload",
]);
});
it("keeps events reported under the same session", () => {
const store = new DiagnosticsStore();
store.applyMessage(message([entry({ summary: "one" })], "page-1"));
store.applyMessage(message([entry({ summary: "two" })], "page-1"));
expect(store.getReport(0).entries).toHaveLength(2);
});
it("adopts the first session without discarding what came with it", () => {
const store = new DiagnosticsStore();
store.applyMessage(message([entry({ summary: "first ever" })], "page-1"));
expect(store.getReport(0).entries).toHaveLength(1);
});
it("returns only what a cursor has not seen", () => {
const store = new DiagnosticsStore();
store.applyMessage(
message([
entry({ summary: "one" }),
entry({ summary: "two" }),
entry({ summary: "three" }),
]),
);
expect(store.getReport(3).entries.map((e) => e.summary)).toEqual(["three"]);
expect(store.getReport(store.getReport(0).nextEventId).entries).toEqual([]);
});
it("returns everything for a cursor that isn't a number", () => {
const store = new DiagnosticsStore();
store.applyMessage(message([entry()]));
// `?startEventId=abc` parses to NaN, and `eventId >= NaN` is false for
// every entry — so without the guard a typo yields an empty feed, which
// reads as "nothing is wrong". An absent param needs no guard: it parses
// to 0, and every id is >= 0.
expect(store.getReport(Number.NaN).entries).toHaveLength(1);
expect(store.getReport(0).entries).toHaveLength(1);
});
it("holds the last connection status reported, ignoring messages without one", () => {
const store = new DiagnosticsStore();
const connection = {
checkedAt: 1,
metabaseUrl: "http://localhost:3000",
reachable: true,
sdkVersion: "0.63.1",
error: null,
};
store.applyMessage({ sessionId: "page-1", entries: [], connection });
store.applyMessage(message([entry()]));
expect(store.getReport(0).connection).toEqual(connection);
});
it("empties on clear, without rewinding the ids", () => {
const store = new DiagnosticsStore();
store.applyMessage(message([entry(), entry()]));
store.clear();
store.applyMessage(message([entry({ summary: "after clear" })]));
// Reusing an id a poller already consumed would hide the new entry.
expect(store.getReport(0).entries).toEqual([
expect.objectContaining({ eventId: 3, summary: "after clear" }),
]);
});
it("keeps the cursor moving forward while the buffer empties", () => {
const store = new DiagnosticsStore();
store.applyMessage(message([entry(), entry()]));
expect(store.getReport(0).nextEventId).toBe(3);
store.clear();
// Derived from the last retained entry it would fall back to 1 here, and a
// poller that had already read up to 3 would re-read everything after.
expect(store.getReport(0).entries).toEqual([]);
expect(store.getReport(0).nextEventId).toBe(3);
});
it("does not let a flood of requests evict the errors that explain them", () => {
const store = new DiagnosticsStore();
store.applyMessage(
message([entry({ summary: "the error worth keeping" })]),
);
store.applyMessage(
message(
Array.from({ length: DATA_APP_DIAGNOSTICS_LIMIT * 3 }, () =>
entry({ kind: "sdk-call", summary: "GET /api/card/1 → 200" }),
),
),
);
const kept = store.getReport(0).entries;
expect(kept[0].summary).toBe("the error worth keeping");
expect(kept.filter((e) => e.kind === "sdk-call")).toHaveLength(
DATA_APP_DIAGNOSTICS_CALL_LIMIT,
);
});
describe("reporting whether anything changed", () => {
const connection = {
checkedAt: 1,
metabaseUrl: "http://localhost:3000",
reachable: true,
sdkVersion: "0.63.1",
error: null,
};
it("is true for new entries", () => {
const store = new DiagnosticsStore();
expect(store.applyMessage(message([entry()]))).toBe(true);
});
it("is true for a new session", () => {
const store = new DiagnosticsStore();
store.applyMessage(message([entry()], "page-1"));
expect(store.applyMessage(message([], "page-2"))).toBe(true);
});
it("is true when the connection check has re-run", () => {
const store = new DiagnosticsStore();
store.applyMessage({ sessionId: "page-1", entries: [], connection });
expect(
store.applyMessage({
sessionId: "page-1",
entries: [],
connection: { ...connection, checkedAt: 2 },
}),
).toBe(true);
});
it("is false for a flush that carries nothing new", () => {
const store = new DiagnosticsStore();
store.applyMessage({
sessionId: "page-1",
entries: [entry()],
connection,
});
// The reporter flushes on a timer whether or not anything happened, and
// the connection arrives as a fresh object every time — comparing it by
// reference would call every flush a change and undo the whole point of
// pushing instead of polling.
expect(
store.applyMessage({
sessionId: "page-1",
entries: [],
connection: { ...connection },
}),
).toBe(false);
});
});
it("starts empty rather than pretending a page has reported", () => {
const store = new DiagnosticsStore();
expect(store.getReport(0).entries).toEqual([]);
expect(store.getReport(0).sessionId).toBeNull();
expect(store.getReport(0).connection).toBeNull();
expect(store.getReport(0).lastReportAt).toBeNull();
expect(store.getReport(0).nextEventId).toBe(1);
});
});