SPB Git

Repository Public

marche-stats.ts 3.4 KB 86 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Vrai-Prix8 * File             : src/lib/marche-stats.ts9 * Module           : lib10 * 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/**43 * Statistiques « la mesure à l'épreuve du marché » (/stats, /stats/marche).44 *45 * Deux sources, dans l'ordre :46 *   1. data/marche-stats.json — produit chaque nuit par scripts/immoka-sync.sh47 *      (ré-évaluation de toutes les annonces Immo-Ka vivantes, MARCHE_STATS_OUT) ;48 *      lu à chaud, jamais versionné, mis en cache 10 min en mémoire ;49 *   2. src/data/marche-stats.json — instantané versionné, embarqué au build,50 *      utilisé si le fichier nocturne est absent ou illisible (laptop, premier51 *      déploiement).52 * Serveur seulement (fs).53 */54import fs from "fs";55import path from "path";56import type { MarcheStats } from "@/components/MarcheView";57import bundled from "@/data/marche-stats.json";5859// chemin statique (data/…) : Turbopack ne trace alors pas tout le projet dans la sortie serveur60const LIVE_PATH = path.join(process.cwd(), "data", "marche-stats.json");61const TTL_MS = 10 * 60 * 1000;6263let cache: { at: number; mtimeMs: number; stats: MarcheStats } | null = null;6465export function loadMarcheStats(): MarcheStats {66  const now = Date.now();67  try {68    const st = fs.statSync(LIVE_PATH);69    if (cache && cache.mtimeMs === st.mtimeMs && now - cache.at < TTL_MS) return cache.stats;70    const parsed = JSON.parse(fs.readFileSync(LIVE_PATH, "utf8")) as MarcheStats;71    if (parsed?.global?.n && parsed.source) {72      const stats = neutre(parsed);73      cache = { at: now, mtimeMs: st.mtimeMs, stats };74      return stats;75    }76  } catch {77    /* fichier nocturne absent ou illisible → instantané versionné */78  }79  return neutre(bundled as unknown as MarcheStats);80}8182/** Le fichier nocturne nomme sa source d'annonces ; le site ne la nomme pas. */83function neutre(s: MarcheStats): MarcheStats {84  return { ...s, source: { ...s.source, app: "annonces publiques" } };85}86
Files main
Menu

Theme