llm-router / apps /api /src /services /expense /analytics /analytics.controller.ts
ClaraSu's picture
Upload folder using huggingface_hub
30ac2fc verified
Raw History Blame Contribute Delete
1 kB
import type { Request, RequestHandler } from 'express'
import type { AuthenticatedRequest } from '@llm-router/auth-client'
import { container } from '../loader.js'
function tenantOf(req: Request): string {
return (req as AuthenticatedRequest).auth!.tenantId
}
function monthOf(req: Request): string | undefined {
const raw = req.query['month']
return typeof raw === 'string' && /^\d{4}-(0[1-9]|1[0-2])$/.test(raw) ? raw : undefined
}
export const getSummary: RequestHandler = async (req, res, next) => {
try {
res.json(await container.analyticsRepo.getSummary(tenantOf(req), monthOf(req)))
} catch (err: unknown) {
next(err)
}
}
export const getTrends: RequestHandler = async (req, res, next) => {
try {
const raw = Number(req.query['months'] ?? 6)
const months = Number.isFinite(raw) && raw > 0 ? Math.min(Math.floor(raw), 24) : 6
res.json(await container.analyticsRepo.getTrends(tenantOf(req), months, monthOf(req)))
} catch (err: unknown) {
next(err)
}
}