PR 3 du chantier admin. Tableau de bord metriques : snapshot de compteurs
+ series temporelles journalieres.
Schema (migration admin_metrics) :
- AnalyticsEvent (type String libre, actorType/actorId sans FK, context Json,
index [type, created_at]) + WorkerHeartbeat (cable en PR 4).
- colonnes createdAt @default(now()) sur UserProfile / Recipe / Planning /
PlanningItem (lecture admin uniquement ; lignes existantes = timestamp de
la migration).
Instrumentation (lib/analytics.service.ts, fire-and-forget) :
- analytics.recordEvent(type, {actorId?, context?}) : retourne void, insert
detache, echec loggue+avale, jamais de latence sur la requete.
- points d'appel : user.signup, recipe.created, recipe.imported,
planning.item_added, tech_step.correction_submitted, shopping_list.viewed.
API : GET /admin/metrics?days= (7-365, defaut 30, requireAdmin) ->
admin-metrics.service.ts. bucketByDay pur (zero-remplissage, teste sans
base). MetricsView dans packages/shared.
Front : DashboardPage (tuiles KPI + un graphe recharts par serie + listes
recettes-par-source / evenements), logique pure dans dashboard.ts, i18n
admin.dashboard.*. AdminApiClient.getMetrics.
reset-db.ts truncate analytics_events + worker_heartbeats.
Tests : Mocha admin-metrics.test.ts (bucketByDay pur x2 verts ; snapshot,
series zero-remplies, event user.signup fire-and-forget) ; Cypress
dashboard.cy.ts (2 verts). specs/backend-architecture.md : section admin.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
95 lines
3 KiB
TypeScript
95 lines
3 KiB
TypeScript
// Mocks the admin API via cy.intercept — no live backend.
|
|
|
|
const adminBody = {
|
|
id: 1,
|
|
email: "ops@example.com",
|
|
name: "Ops",
|
|
createdAt: "2026-08-01T00:00:00.000Z",
|
|
lastLoginAt: "2026-08-28T09:00:00.000Z",
|
|
};
|
|
|
|
/** A 3-day series helper for the fixture. */
|
|
function series(counts: number[]) {
|
|
return counts.map((count, i) => ({
|
|
date: `2026-08-${String(10 + i).padStart(2, "0")}`,
|
|
count,
|
|
}));
|
|
}
|
|
|
|
function metricsFixture() {
|
|
return {
|
|
generatedAt: "2026-08-28T09:00:00.000Z",
|
|
rangeDays: 30,
|
|
snapshot: {
|
|
admins: 2,
|
|
users: 42,
|
|
households: 15,
|
|
activeHouseholds: 9,
|
|
recipes: 120,
|
|
recipesManual: 30,
|
|
recipesImported: 90,
|
|
recipesBySource: [
|
|
{ key: "themealdb", label: "TheMealDB", count: 60 },
|
|
{ key: "marmiton", label: "Marmiton", count: 30 },
|
|
],
|
|
plannings: 18,
|
|
planningItems: 210,
|
|
steps: 640,
|
|
detectedTechniques: 900,
|
|
favorites: 55,
|
|
corrections: 12,
|
|
correctionsUnconsumed: 4,
|
|
correctionsRemoval: 2,
|
|
trainingSuggestions: 8,
|
|
trainingSuggestionsByStatus: [{ key: "pending", label: "pending", count: 8 }],
|
|
trainingSuggestionsBySourceType: [{ key: "correction", label: "correction", count: 8 }],
|
|
},
|
|
series: {
|
|
signups: series([1, 3, 2]),
|
|
recipesCreated: series([0, 2, 1]),
|
|
planningItemsAdded: series([4, 1, 5]),
|
|
correctionsSubmitted: series([0, 0, 1]),
|
|
trainingSuggestions: series([0, 1, 0]),
|
|
},
|
|
events: [{ type: "user.signup", buckets: series([1, 3, 2]) }],
|
|
};
|
|
}
|
|
|
|
describe("Admin dashboard", () => {
|
|
beforeEach(() => {
|
|
cy.viewport(1400, 900);
|
|
cy.intercept("GET", "**/admin/auth/me", { statusCode: 200, body: adminBody });
|
|
});
|
|
|
|
it("renders KPI tiles, a chart per series, and the breakdown lists", () => {
|
|
cy.intercept("GET", "**/admin/metrics*", { statusCode: 200, body: metricsFixture() }).as(
|
|
"getMetrics",
|
|
);
|
|
cy.visit("/");
|
|
cy.wait("@getMetrics").its("request.url").should("include", "days=30");
|
|
|
|
// KPI tiles — value + label.
|
|
cy.contains(".kpi-tile", "Utilisateurs").should("contain.text", "42");
|
|
cy.contains(".kpi-tile", "Recettes importées").should("contain.text", "90");
|
|
cy.contains(".kpi-tile", "Corrections à traiter").should("contain.text", "4");
|
|
|
|
// One chart card per instrumented series.
|
|
cy.get(".chart-card").should("have.length", 5);
|
|
cy.contains(".chart-card", "Inscriptions").should("contain.text", "6 sur 30 j");
|
|
|
|
// Breakdown lists.
|
|
cy.contains(".breakdown", "Recettes importées par source")
|
|
.should("contain.text", "TheMealDB")
|
|
.and("contain.text", "Marmiton");
|
|
cy.contains(".breakdown", "Évènements enregistrés").should("contain.text", "user.signup");
|
|
});
|
|
|
|
it("shows an error state when the metrics request fails", () => {
|
|
cy.intercept("GET", "**/admin/metrics*", {
|
|
statusCode: 500,
|
|
body: { code: 5000, message: "x" },
|
|
});
|
|
cy.visit("/");
|
|
cy.contains("Impossible de charger").should("be.visible");
|
|
});
|
|
});
|