feat(monitoring): add stats counters API and public status page

Add GET /api/stats exposing cumulative counters (feeds created/deleted,
emails received/rejected, recent date-times) plus live values (active
feeds, active WebSub subscriptions). Counters persist in a stats:counters
KV singleton and are incremented at the email-processing chokepoint and
feed create/delete paths. Replace the / → /admin redirect with a public
status page rendering these figures with a link to the admin.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
This commit is contained in:
Julien HerrandClaude Opus 4.7 committed 2026-05-23 09:50:51 +02:00
1 parent f4d5edda0e
commit b534ce5bf8
15 files changed
+484 -6

No files matched your search

+29
View File
@@ -6,6 +6,7 @@ import {
ProcessEmailInput,
RawAttachment,
} from "./email-processor";
import { getCounters } from "../utils/stats";
const VALID_FEED_ID = "apple.mountain.42";
const VALID_TO = `${VALID_FEED_ID}@test.getmynews.app`;
@@ -467,3 +468,31 @@ describe("processEmail — attachments", () => {
expect(mockR2._has(oldAttachmentId)).toBe(false);
});
});
describe("processEmail — monitoring counters", () => {
it("increments emails_received and sets last_email_at on success", async () => {
const env = createMockEnv();
await env.EMAIL_STORAGE.put(
`feed:${VALID_FEED_ID}:config`,
JSON.stringify({}),
);
await processEmail(makeInput(), env as any);
const counters = await getCounters(env.EMAIL_STORAGE as any);
expect(counters.emails_received).toBe(1);
expect(counters.emails_rejected).toBe(0);
expect(counters.last_email_at).toBeDefined();
});
it("increments emails_rejected when validation fails", async () => {
const env = createMockEnv();
// No feed config → 404 rejection
await processEmail(makeInput(), env as any);
const counters = await getCounters(env.EMAIL_STORAGE as any);
expect(counters.emails_rejected).toBe(1);
expect(counters.emails_received).toBe(0);
});
});