refactor: publish mileage metric catalog
This commit is contained in:
@@ -11,6 +11,7 @@ import eleRouter from './routes/ele/index.js';
|
||||
import feedbackRouter from './routes/feedback/index.js';
|
||||
import vehicleHeatmapRouter from './routes/vehicle-heatmap.js';
|
||||
import hydrogenHeatmapRouter from './routes/hydrogen-heatmap.js';
|
||||
import analyticsRouter from './routes/analytics/index.js';
|
||||
import { ensureSchedulingTables } from './routes/scheduling/db-schema.js';
|
||||
import authRouter from './auth/login.js';
|
||||
import { authMiddleware } from './auth/middleware.js';
|
||||
@@ -35,6 +36,7 @@ app.route('/api/ele', eleRouter);
|
||||
app.route('/api/feedback', feedbackRouter);
|
||||
app.route('/api/vehicle-heatmap', vehicleHeatmapRouter);
|
||||
app.route('/api/hydrogen-heatmap', hydrogenHeatmapRouter);
|
||||
app.route('/api/analytics', analyticsRouter);
|
||||
|
||||
app.get('/api/health', (c) => c.json({ status: 'ok', time: new Date().toISOString() }));
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import app from './index.js';
|
||||
|
||||
test('returns the published mileage metric contract', async () => {
|
||||
const response = await app.request('/metrics?domain=mileage');
|
||||
const payload = await response.json();
|
||||
|
||||
assert.equal(response.status, 200);
|
||||
assert.equal(payload.catalogVersion, 1);
|
||||
assert.deepEqual(payload.domains, ['mileage']);
|
||||
assert.ok(payload.metrics.some((metric: { id: string }) => metric.id === 'mileage.assessment_completion_rate'));
|
||||
});
|
||||
|
||||
test('rejects unpublished metric domains', async () => {
|
||||
const response = await app.request('/metrics?domain=energy');
|
||||
|
||||
assert.equal(response.status, 400);
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Hono } from 'hono';
|
||||
import {
|
||||
METRIC_CATALOG_VERSION,
|
||||
isMetricDomain,
|
||||
listMetricDefinitions,
|
||||
type MetricDomain,
|
||||
} from '../../../shared/analytics/catalog.js';
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
app.get('/metrics', (c) => {
|
||||
const requestedDomain = c.req.query('domain');
|
||||
let domain: MetricDomain | undefined;
|
||||
if (requestedDomain) {
|
||||
if (!isMetricDomain(requestedDomain)) {
|
||||
return c.json({ error: 'Unsupported metric domain', domain: requestedDomain }, 400);
|
||||
}
|
||||
domain = requestedDomain;
|
||||
}
|
||||
|
||||
const metrics = listMetricDefinitions(domain);
|
||||
return c.json({
|
||||
catalogVersion: METRIC_CATALOG_VERSION,
|
||||
domains: Array.from(new Set(metrics.map(metric => metric.domain))),
|
||||
metrics,
|
||||
});
|
||||
});
|
||||
|
||||
export default app;
|
||||
Reference in New Issue
Block a user