Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/app/api/stats/route.ts9 * Module : app10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-08-1712 * Last Modified : 2026-08-1713 *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/**43 * GET /api/stats — agrégats provinciaux publics de Vrai-Prix.44 * Import statique de src/data/stats.json (mêmes données que la page /stats) :45 * aucune base de données, réponse cachable 10 minutes.46 */47import { NextResponse } from "next/server";48import stats from "@/data/stats.json";49import { ventesOverview } from "@/lib/ventes";50import { getDb } from "@/lib/db";5152/** Ventes du corpus d'entraînement du modèle, 2021-01 → 2026-07 (constante, voir /methodologie). */53const TRANSACTIONS = 745119;5455/** Nombre réel de ventes au registre (compté au plus une fois par 10 minutes). */56let total: { at: number; n: number } | null = null;57function transactionsTotal(): number | null {58 try {59 if (!total || Date.now() - total.at > 600_000) {60 const n = (getDb().prepare("SELECT COUNT(*) AS n FROM transactions WHERE amount > 0").get() as { n: number }).n;61 total = { at: Date.now(), n };62 }63 return total.n;64 } catch {65 return null;66 }67}6869export const dynamic = "force-dynamic";7071export function GET() {72 let updatedTo: string | null = null;73 try {74 updatedTo = ventesOverview().maxDate;75 } catch {76 updatedTo = null;77 }78 return NextResponse.json(79 {80 units_total: stats.unites,81 value_total_2026: stats.valeur_totale_2026,82 value_median_2026: stats.valeur_mediane_2026,83 growth_2021_2026_pct: stats.croissance_2021_2026_pct,84 dwellings: stats.logements,85 municipalities: stats.municipalites,86 transactions: TRANSACTIONS,87 transactions_total: transactionsTotal(),88 transactions_updated_to: updatedTo,89 model_version: "hédonique LightGBM + comparables ajustés — erreur médiane 11 %",90 updated: stats.generated,91 source: "https://www.vrai-prix.com/stats",92 },93 { headers: { "Cache-Control": "public, max-age=600" } }94 );95}96