Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/app/api/admin/cost/usage/route.ts9 * Module : app10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-09-0812 * Last Modified : 2026-09-0813 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Vrai-Prix — admin méthode du coût : consommation IA (ai_usage) et budgets jour / mois.43import { NextResponse } from "next/server";44import { adminAuth } from "@/lib/cost/connectors/admin";45import { getCostDb } from "@/lib/cost/db";4647export const runtime = "nodejs";4849export function GET(req: Request) {50 const denied = adminAuth(req);51 if (denied) return denied;52 const d = getCostDb();53 const day = d.prepare("SELECT COUNT(*) n, COALESCE(SUM(estimated_cost_usd),0) usd, COALESCE(SUM(input_tokens),0) input, COALESCE(SUM(output_tokens),0) output FROM ai_usage WHERE created_at >= date('now')").get();54 const month = d.prepare("SELECT COUNT(*) n, COALESCE(SUM(estimated_cost_usd),0) usd, COALESCE(SUM(input_tokens),0) input, COALESCE(SUM(output_tokens),0) output FROM ai_usage WHERE created_at >= date('now','start of month')").get();55 const byPurpose = d.prepare("SELECT purpose, model, COUNT(*) n, ROUND(COALESCE(SUM(estimated_cost_usd),0),4) usd, ROUND(AVG(latency_ms)) latencyMs, ROUND(AVG(estimated_cost_usd),5) avgUsd FROM ai_usage GROUP BY purpose, model ORDER BY usd DESC").all();56 const recent = d.prepare("SELECT id, analysis_id analysisId, purpose, model, input_images inputImages, input_tokens inputTokens, output_tokens outputTokens, estimated_cost_usd usd, latency_ms latencyMs, created_at createdAt FROM ai_usage ORDER BY created_at DESC LIMIT 50").all();57 const budgets = { dailyUsd: Number(process.env.AI_DAILY_BUDGET_USD ?? 5), monthlyUsd: Number(process.env.AI_MONTHLY_BUDGET_USD ?? 60) };58 const dayUsd = (day as { usd: number }).usd;59 const monthUsd = (month as { usd: number }).usd;60 return NextResponse.json({ day, month, byPurpose, recent, budgets, alerts: [dayUsd > budgets.dailyUsd ? "budget quotidien dépassé" : null, monthUsd > budgets.monthlyUsd ? "budget mensuel dépassé" : null].filter(Boolean) });61}62