feat: implement dashboard with authentication and event logging
- Added dashboard functionality at /status with Basic Auth using DASHBOARD_PASSWORD. - Created server-side event logging to track catalog and university status. - Implemented traffic metrics tracking and display on the dashboard. - Enhanced API tests to cover new dashboard features and authentication. - Introduced CSS and JS for dashboard styling and functionality. - Added tests for dashboard access control and data retrieval.
This commit is contained in:
39
server/events.js
Normal file
39
server/events.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { readFile, writeFile, mkdir } from "node:fs/promises";
|
||||
import { DATA } from "./paths.js";
|
||||
|
||||
// What happened to the catalog and the university, newest first, for the dashboard: the same
|
||||
// events the Telegram messages report, plus republished documents without new problems.
|
||||
// The last hundred are kept on disk, so a restart does not empty the feed.
|
||||
|
||||
const FILE = "events.json";
|
||||
const MAX = 100;
|
||||
let events;
|
||||
|
||||
async function load() {
|
||||
if (events) return events;
|
||||
try {
|
||||
events = JSON.parse(await readFile(DATA + FILE, "utf8"));
|
||||
if (!Array.isArray(events)) events = [];
|
||||
} catch {
|
||||
events = [];
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds events: `{kind, title, text?, doc?}`. Kinds: new, republished, failed, year, year_late,
|
||||
* source_down, source_up.
|
||||
*/
|
||||
export async function recordEvents(list) {
|
||||
if (!list.length) return;
|
||||
await load();
|
||||
const t = new Date().toISOString();
|
||||
events.unshift(...list.map((e) => ({ t, ...e })));
|
||||
events.length = Math.min(events.length, MAX);
|
||||
await mkdir(DATA, { recursive: true });
|
||||
await writeFile(DATA + FILE, JSON.stringify(events));
|
||||
}
|
||||
|
||||
export async function listEvents(limit = 50) {
|
||||
return (await load()).slice(0, limit);
|
||||
}
|
||||
Reference in New Issue
Block a user