// Auteur : Simon-Pierre Boucher — contact@spboucher.ai /** * Statistiques « la mesure à l'épreuve du marché » (/stats, /stats/marche). * * Deux sources, dans l'ordre : * 1. data/marche-stats.json — produit chaque nuit par scripts/immoka-sync.sh * (ré-évaluation de toutes les annonces Immo-Ka vivantes, MARCHE_STATS_OUT) ; * lu à chaud, jamais versionné, mis en cache 10 min en mémoire ; * 2. src/data/marche-stats.json — instantané versionné, embarqué au build, * utilisé si le fichier nocturne est absent ou illisible (laptop, premier * déploiement). * Serveur seulement (fs). */ import fs from "fs"; import path from "path"; import type { MarcheStats } from "@/components/MarcheView"; import bundled from "@/data/marche-stats.json"; // chemin statique (data/…) : Turbopack ne trace alors pas tout le projet dans la sortie serveur const LIVE_PATH = path.join(process.cwd(), "data", "marche-stats.json"); const TTL_MS = 10 * 60 * 1000; let cache: { at: number; mtimeMs: number; stats: MarcheStats } | null = null; export function loadMarcheStats(): MarcheStats { const now = Date.now(); try { const st = fs.statSync(LIVE_PATH); if (cache && cache.mtimeMs === st.mtimeMs && now - cache.at < TTL_MS) return cache.stats; const parsed = JSON.parse(fs.readFileSync(LIVE_PATH, "utf8")) as MarcheStats; if (parsed?.global?.n && parsed.source) { cache = { at: now, mtimeMs: st.mtimeMs, stats: parsed }; return parsed; } } catch { /* fichier nocturne absent ou illisible → instantané versionné */ } return bundled as unknown as MarcheStats; }