mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
fix(insights,cache): surface revalidation to clients and evict on interactive writes
Stale-served insights responses now carry a revalidating flag; the advisor and comprehensive queries poll on a bounded 25 s cadence until the background warm lands, so an open session converges instead of pinning yesterday's briefing for an hour. Interactive measurement writes (single create, edit, delete, bulk-delete, restore) hard-evict the analytics cache so a manually entered value is reflected on the next read; background batch sync keeps the mark-stale path. The visit-triggered force warm collapses to one job per user instead of one per locale, and the umami script proxy failure now annotates a named action with the failure reason instead of a bare flag.
This commit is contained in:
1 parent
78fe82e348
commit
03453aaed2
23 files changed
+461
-55
No files matched your search
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Fail-soft contract for the Umami proxy route: an unreachable / refused
|
||||
* upstream degrades to the noop script (200, never a 500) AND stays
|
||||
* observable — the catch annotates a proper wide-event action
|
||||
* (`monitoring.umami_script.fetch_failed`) with a truncated reason so
|
||||
* dashboards can see the degraded state.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
vi.mock("@/lib/api-handler", () => ({
|
||||
apiHandler: <T extends (...args: unknown[]) => unknown>(fn: T) => fn,
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/logging/context", () => ({
|
||||
annotate: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/monitoring-settings", () => ({
|
||||
getPublicMonitoringSettings: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/safe-fetch", () => ({
|
||||
safeFetch: vi.fn(),
|
||||
}));
|
||||
|
||||
import { GET } from "../route";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { getPublicMonitoringSettings } from "@/lib/monitoring-settings";
|
||||
import { safeFetch } from "@/lib/safe-fetch";
|
||||
|
||||
const callGet = GET as unknown as () => Promise<Response>;
|
||||
|
||||
const ENABLED_SETTINGS = {
|
||||
umamiEnabled: true,
|
||||
umamiScriptUrl: "https://stats.example.com/script.js",
|
||||
umamiWebsiteId: "site-1",
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.mocked(getPublicMonitoringSettings).mockResolvedValue(
|
||||
ENABLED_SETTINGS as never,
|
||||
);
|
||||
});
|
||||
|
||||
describe("GET /api/monitoring/umami-script — fail-soft observability", () => {
|
||||
it("serves the noop script (200) and annotates the failure action when the upstream fetch throws", async () => {
|
||||
vi.mocked(safeFetch).mockRejectedValue(
|
||||
new Error("connect ETIMEDOUT 192.0.2.10:443"),
|
||||
);
|
||||
|
||||
const res = await callGet();
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.text()).toContain("umami disabled");
|
||||
|
||||
// The fail-soft must stay observable: a proper
|
||||
// `<surface>.<noun>.<verb>` action + a truncated reason, not a bare
|
||||
// meta flag.
|
||||
expect(annotate).toHaveBeenCalledWith({
|
||||
action: { name: "monitoring.umami_script.fetch_failed" },
|
||||
meta: { reason: "connect ETIMEDOUT 192.0.2.10:443" },
|
||||
});
|
||||
});
|
||||
|
||||
it("truncates an oversized error message in the reason meta", async () => {
|
||||
vi.mocked(safeFetch).mockRejectedValue(new Error("x".repeat(500)));
|
||||
|
||||
const res = await callGet();
|
||||
expect(res.status).toBe(200);
|
||||
|
||||
const failureCall = vi
|
||||
.mocked(annotate)
|
||||
.mock.calls.find(
|
||||
(call) =>
|
||||
call[0]?.action?.name === "monitoring.umami_script.fetch_failed",
|
||||
);
|
||||
expect(failureCall).toBeTruthy();
|
||||
expect(
|
||||
(failureCall?.[0]?.meta as { reason: string }).reason,
|
||||
).toHaveLength(200);
|
||||
});
|
||||
|
||||
it("serves the upstream script untouched on a healthy fetch (no failure action)", async () => {
|
||||
vi.mocked(safeFetch).mockResolvedValue(
|
||||
new Response("/* real script */", { status: 200 }) as never,
|
||||
);
|
||||
|
||||
const res = await callGet();
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.text()).toBe("/* real script */");
|
||||
|
||||
const failureCall = vi
|
||||
.mocked(annotate)
|
||||
.mock.calls.find(
|
||||
(call) =>
|
||||
call[0]?.action?.name === "monitoring.umami_script.fetch_failed",
|
||||
);
|
||||
expect(failureCall).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -43,8 +43,19 @@ export const GET = apiHandler(async () => {
|
||||
// the input-time `isPublicUrl` accept and the socket connect.
|
||||
{ requirePublicHost: true },
|
||||
);
|
||||
} catch {
|
||||
annotate({ meta: { umami_script_fetch_failed: true } });
|
||||
} catch (err) {
|
||||
// Fail-soft stays a 200, but the failure must stay observable:
|
||||
// a proper action name keeps the degraded state visible on the
|
||||
// wide-event dashboards instead of hiding behind a bare meta flag.
|
||||
annotate({
|
||||
action: { name: "monitoring.umami_script.fetch_failed" },
|
||||
meta: {
|
||||
reason:
|
||||
err instanceof Error
|
||||
? err.message.slice(0, 200)
|
||||
: String(err).slice(0, 200),
|
||||
},
|
||||
});
|
||||
return new NextResponse(NOOP_SCRIPT, {
|
||||
status: 200,
|
||||
headers: {
|
||||
|
||||
Reference in new issue
Block a user