1/* -----------------------------------------------------------------------------2 Auteur : Simon-Pierre Boucher — contact@spboucher.ai3 Fichier : ka-stats/server.mjs4 Desc. : Ka·Stats — l'explorateur de statistiques du Québec (Groupe KA).5 Serveur Node ZÉRO dépendance : sert le front statique et agrège les6 dashboards /api/stats/dashboard (contrat ka-stats v2) des 127 plateformes du Groupe KA, avec cache mémoire + disque et8 stale-while-error (un satellite injoignable ⇒ dernière bonne copie).9 Usage : node server.mjs [port] (défaut 8140) — healthcheck: /healthz10----------------------------------------------------------------------------- */11import http from "node:http";12import fs from "node:fs";13import path from "node:path";14import { fileURLToPath } from "node:url";1516const PORT = Number(process.argv[2] || process.env.PORT || 8140);17const ROOT = path.dirname(fileURLToPath(import.meta.url));18const PUB = path.join(ROOT, "public");19const DATA = path.join(ROOT, "data");20const CACHE_FILE = path.join(DATA, "cache.json");21const TTL_MS = 10 * 60 * 1000; // 10 min par (site, période)22fs.mkdirSync(DATA, { recursive: true });2324/* ---- Les 13 plateformes publiques du Groupe KA (contrat ka-stats v2) ----25 Accents = ecosystem.json de ka-ui (job-ka relevé sur le site). */26const SITES = [27 { id: "lou-ka", wordmark: "Lou·Ka", domain: "www.lou-ka.com", accent: "#ff6a00", accentSoft: "#fff1e6", accentDeep: "#cc5500", onAccent: "#ffffff", tagline: "Tous les logements à louer", theme: "habitation" },28 { id: "immo-ka", wordmark: "Immo·Ka", domain: "www.immo-ka.com", accent: "#e23744", accentSoft: "#fbe0e2", accentDeep: "#a8232e", onAccent: "#ffffff", tagline: "Toutes les propriétés à vendre", theme: "habitation" },29 { id: "vrai-prix", wordmark: "Vrai-Prix", domain: "www.vrai-prix.com", accent: "#ff5148", accentSoft: "#ffe3e0", accentDeep: "#9e2a25", onAccent: "#ffffff", tagline: "La valeur réelle de chaque propriété", theme: "habitation" },30 { id: "auto-ka", wordmark: "Auto·Ka", domain: "www.auto-ka.com", accent: "#ff5a2a", accentSoft: "#ffe8de", accentDeep: "#cc3f16", onAccent: "#ffffff", tagline: "Les voitures usagées du Québec", theme: "mobilite" },31 { id: "job-ka", wordmark: "Job·Ka", domain: "www.job-ka.com", accent: "#0c8599", accentSoft: "#def0f4", accentDeep: "#095c6b", onAccent: "#ffffff", tagline: "Tous les emplois des employeurs québécois", theme: "emploi" },32 { id: "food-ka", wordmark: "Food·Ka", domain: "www.food-ka.com", accent: "#1f9d55", accentSoft: "#e2f5ea", accentDeep: "#157a40", onAccent: "#ffffff", tagline: "Les prix d'épicerie, suivis à la source", theme: "consommation" },33 { id: "resto-ka", wordmark: "Resto·Ka", domain: "www.resto-ka.com", accent: "#f08c00", accentSoft: "#fdeed7", accentDeep: "#b96a00", onAccent: "#141814", tagline: "Chaque resto, chaque plat, chaque prix", theme: "consommation" },34 { id: "fabri-ka", wordmark: "Fabri·Ka", domain: "www.fabri-ka.com", accent: "#c4532e", accentSoft: "#f7e3da", accentDeep: "#a94525", onAccent: "#ffffff", tagline: "Les produits fabriqués au Québec", theme: "consommation" },35 { id: "sorti-ka", wordmark: "Sorti·Ka", domain: "www.sorti-ka.com", accent: "#d6336c", accentSoft: "#fbe0eb", accentDeep: "#a12551", onAccent: "#ffffff", tagline: "Toutes les sorties, dans les 17 régions", theme: "culture" },36 { id: "crea-ka", wordmark: "Créa·Ka", domain: "www.crea-ka.com", accent: "#7048e8", accentSoft: "#ece5fc", accentDeep: "#5433b8", onAccent: "#ffffff", tagline: "Les créateurs d'ici, tous leurs liens", theme: "culture" },37 { id: "trouve-ka", wordmark: "Trouve·Ka", domain: "www.trouve-ka.com", accent: "#1c7ed6", accentSoft: "#e7f2fd", accentDeep: "#14508f", onAccent: "#ffffff", tagline: "Le moteur de recherche du web québécois", theme: "culture" },38 { id: "rent-ka", wordmark: "Rent·Ka", domain: "www.rent-ka.com", accent: "#2456e6", accentSoft: "#e9efff", accentDeep: "#1738a8", onAccent: "#ffffff", tagline: "Locations résidentielles — Canada hors Québec", theme: "habitation" },39 { id: "api-ka", wordmark: "API·Ka", domain: "www.api-ka.com", accent: "#3b5bdb", accentSoft: "#e4eafb", accentDeep: "#2b44a8", onAccent: "#ffffff", tagline: "La donnée de l'écosystème, par API", theme: "infrastructure" },40];41const SITE_BY_ID = Object.fromEntries(SITES.map((s) => [s.id, s]));4243const PERIODS = new Set(["auj", "7j", "30j", "3m", "6m", "12m", "annee", "tout"]);44const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;4546/* ---- Cache (mémoire + disque) ---- */47let cache = {};48try { cache = JSON.parse(fs.readFileSync(CACHE_FILE, "utf8")); } catch { cache = {}; }49let persistTimer = null;50function persistCache() {51 clearTimeout(persistTimer);52 persistTimer = setTimeout(() => {53 // On ne persiste que les périodes standard (pas les plages custom, illimitées)54 const keep = {};55 for (const [k, v] of Object.entries(cache)) if (!k.includes("from=")) keep[k] = v;56 fs.writeFile(CACHE_FILE, JSON.stringify(keep), () => {});57 }, 2000);58}5960const inflight = new Map();6162function buildQuery(q) {63 const period = PERIODS.has(q.period) ? q.period : "30j";64 const parts = [`period=${period}`];65 if (DATE_RE.test(q.from || "") && DATE_RE.test(q.to || "")) {66 parts.push(`from=${q.from}`, `to=${q.to}`);67 }68 return parts.join("&");69}7071async function fetchRemote(site, query) {72 const url = `https://${site.domain}/api/stats/dashboard?${query}`;73 const ac = new AbortController();74 const timer = setTimeout(() => ac.abort(), 45000);75 try {76 const r = await fetch(url, {77 signal: ac.signal,78 headers: { "User-Agent": "ka-stats/1.0 (Groupe KA; +https://www.ka-stats.com)", Accept: "application/json" },79 });80 if (!r.ok) throw new Error(`HTTP ${r.status}`);81 let j = await r.json();82 if (j && j.success === true && j.data) j = j.data; // enveloppe api-ka83 if (!j || typeof j !== "object" || (!j.kpis && !j.series)) throw new Error("payload inattendu");84 return j;85 } finally {86 clearTimeout(timer);87 }88}8990async function getDashboard(siteId, q) {91 const site = SITE_BY_ID[siteId];92 if (!site) return null;93 const query = buildQuery(q);94 const key = `${siteId}|${query}`;95 const hit = cache[key];96 const now = Date.now();97 if (hit && now - hit.at < TTL_MS) return { site: siteId, ok: true, stale: false, fetched: hit.at, data: hit.data };98 if (inflight.has(key)) return inflight.get(key);99 const p = (async () => {100 try {101 const data = await fetchRemote(site, query);102 cache[key] = { at: Date.now(), data };103 persistCache();104 return { site: siteId, ok: true, stale: false, fetched: cache[key].at, data };105 } catch (e) {106 if (hit) return { site: siteId, ok: true, stale: true, fetched: hit.at, error: String(e.message || e), data: hit.data };107 return { site: siteId, ok: false, stale: false, error: String(e.message || e), data: null };108 } finally {109 inflight.delete(key);110 }111 })();112 inflight.set(key, p);113 return p;114}115116/* ---- Vue d'ensemble consolidée (calculée serveur, légère) ---- */117const MONEY_RE = /\$/;118function summarize(res) {119 const site = SITE_BY_ID[res.site];120 if (!res.ok || !res.data) return { id: res.site, ok: false, error: res.error || "injoignable" };121 const d = res.data;122 const kpis = Array.isArray(d.kpis) ? d.kpis : [];123 const primary = kpis[0] || null;124 const spark = (primary && primary.spark) || (d.series && d.series[0] && d.series[0].points) || [];125 const money = kpis.filter((k) => MONEY_RE.test(k.unit || "")).slice(0, 4)126 .map((k) => ({ label: k.label, value: k.value, unit: k.unit, delta_pct: k.delta_pct ?? null, spark: k.spark || null }));127 return {128 id: res.site, ok: true, stale: !!res.stale, updated: d.updated || null, fetched: res.fetched,129 period: d.period || null,130 primary: primary ? { id: primary.id, label: primary.label, value: primary.value, unit: primary.unit || "", delta_pct: primary.delta_pct ?? null } : null,131 spark: spark.slice(-90),132 kpis: kpis.slice(0, 8).map((k) => ({ id: k.id, label: k.label, value: k.value, unit: k.unit || "", delta_pct: k.delta_pct ?? null })),133 money,134 gauges: (d.gauges || []).slice(0, 3),135 records: (d.records || []).slice(0, 4),136 sections: {137 series: (d.series || []).length, multiseries: (d.multiseries || []).length,138 stacked: (d.stacked || []).length, breakdowns: (d.breakdowns || []).length,139 distributions: (d.distributions || []).length, tables: (d.tables || []).length,140 geo: d.geo ? 1 : 0, heatmap: d.heatmap ? 1 : 0, hourly: d.hourly ? 1 : 0,141 records: (d.records || []).length, kpis: kpis.length, gauges: (d.gauges || []).length,142 },143 theme: site.theme,144 };145}146147async function getOverview(q) {148 const results = await Promise.all(SITES.map((s) => getDashboard(s.id, q)));149 const sites = results.map(summarize);150 // Volume agrégé par jour — même règle que le hub : seuls les points datés au151 // JOUR comptent (vrai-prix publie une série annuelle → exclue), un site doit152 // avoir ≥ 8 points quotidiens, on reporte la dernière valeur connue (stocks)153 // et on ne garde que les jours couverts par ≥ 70 % des sites inclus.154 const daily = sites155 .filter((s) => s.ok && Array.isArray(s.spark))156 .map((s) => ({ id: s.id, pts: s.spark.filter((p) => p && DATE_RE.test(String(p.t)) && typeof p.v === "number") }))157 .filter((s) => s.pts.length >= 8);158 const dates = [...new Set(daily.flatMap((s) => s.pts.map((p) => p.t)))].sort();159 const totalSpark = [];160 const lastVal = new Map();161 const ptMaps = daily.map((s) => new Map(s.pts.map((p) => [p.t, p.v])));162 for (const t of dates) {163 let sum = 0, covered = 0;164 daily.forEach((s, i) => {165 const v = ptMaps[i].get(t);166 if (typeof v === "number") lastVal.set(s.id, v);167 if (lastVal.has(s.id)) { sum += lastVal.get(s.id); covered++; }168 });169 if (covered >= Math.ceil(daily.length * 0.7)) totalSpark.push({ t, v: Math.round(sum) });170 }171 const totalItems = sites.reduce((acc, s) => acc + (s.ok && s.primary && typeof s.primary.value === "number" ? s.primary.value : 0), 0);172 const indicators = [];173 for (const s of sites) {174 if (!s.ok) continue;175 for (const m of s.money) indicators.push({ site: s.id, ...m });176 }177 const records = [];178 for (const [i, s] of sites.entries()) {179 if (!s.ok) continue;180 for (const r of s.records) records.push({ site: s.id, ...r });181 }182 const chartsTotal = sites.reduce((acc, s) => {183 if (!s.ok || !s.sections) return acc;184 const c = s.sections;185 return acc + c.series + c.multiseries + c.stacked + c.breakdowns + c.distributions + c.geo + c.heatmap + c.hourly + c.kpis + c.gauges;186 }, 0);187 return {188 updated: new Date().toISOString(),189 period: sites.find((s) => s.ok && s.period)?.period || null,190 totals: { items: totalItems, sitesLive: sites.filter((s) => s.ok).length, sites: SITES.length, charts: chartsTotal, spark: totalSpark },191 sites, indicators, records,192 };193}194195/* ---- Catalogue de métriques (pour le Studio d'indicateurs sur mesure) ----196 Aplati tout ce qui est traçable dans les 13 dashboards en références197 adressables : site|kpi|<id> · site|series|<id> · site|multi|<id>|<i> ·198 site|stacked|<id>|<i>. /api/catalog = métadonnées ; /api/metric = points. */199function catalogOf(siteId, d) {200 const site = SITE_BY_ID[siteId];201 const out = [];202 const push = (ref, label, unit, n) => out.push({ ref, site: siteId, wordmark: site.wordmark, label, unit: unit || "", n });203 for (const k of d.kpis || []) {204 if (Array.isArray(k.spark) && k.spark.length > 2) push(`${siteId}|kpi|${k.id}`, k.label, k.unit, k.spark.length);205 }206 for (const s of d.series || []) {207 if (Array.isArray(s.points) && s.points.length > 1) push(`${siteId}|series|${s.id}`, s.title, s.unit, s.points.length);208 }209 for (const m of d.multiseries || []) {210 (m.series || []).forEach((sub, i) => {211 if (Array.isArray(sub.points) && sub.points.length > 1) push(`${siteId}|multi|${m.id}|${i}`, `${m.title} — ${sub.label}`, m.unit, sub.points.length);212 });213 }214 for (const st of d.stacked || []) {215 (st.keys || []).forEach((key, i) => {216 const pts = (st.points || []).filter((p) => Array.isArray(p.values) && typeof p.values[i] === "number");217 if (pts.length > 1) push(`${siteId}|stacked|${st.id}|${i}`, `${st.title} — ${key}`, st.unit, pts.length);218 });219 }220 return out;221}222223function resolveMetric(d, kind, id, sub) {224 if (kind === "kpi") {225 const k = (d.kpis || []).find((x) => x.id === id);226 return k && k.spark ? { points: k.spark, unit: k.unit || "", label: k.label } : null;227 }228 if (kind === "series") {229 const s = (d.series || []).find((x) => x.id === id);230 return s ? { points: s.points || [], unit: s.unit || "", label: s.title } : null;231 }232 if (kind === "multi") {233 const m = (d.multiseries || []).find((x) => x.id === id);234 const s = m && (m.series || [])[Number(sub)];235 return s ? { points: s.points || [], unit: m.unit || "", label: `${m.title} — ${s.label}` } : null;236 }237 if (kind === "stacked") {238 const st = (d.stacked || []).find((x) => x.id === id);239 if (!st) return null;240 const i = Number(sub);241 const key = (st.keys || [])[i];242 if (key === undefined) return null;243 const points = (st.points || []).filter((p) => Array.isArray(p.values)).map((p) => ({ t: p.t, v: p.values[i] ?? 0 }));244 return { points, unit: st.unit || "", label: `${st.title} — ${key}` };245 }246 return null;247}248249async function getCatalog(q) {250 const results = await Promise.all(SITES.map((s) => getDashboard(s.id, q)));251 const metrics = [];252 for (const r of results) if (r.ok && r.data) metrics.push(...catalogOf(r.site, r.data));253 return { updated: new Date().toISOString(), count: metrics.length, metrics };254}255256async function getMetric(ref, q) {257 const [siteId, kind, id, sub] = String(ref).split("|");258 if (!SITE_BY_ID[siteId]) return null;259 const r = await getDashboard(siteId, q);260 if (!r.ok || !r.data) return null;261 const m = resolveMetric(r.data, kind, id, sub);262 if (!m) return null;263 return { ref, site: siteId, ...m, stale: !!r.stale };264}265266/* ---- HTTP ---- */267const MIME = {268 ".html": "text/html; charset=utf-8", ".css": "text/css; charset=utf-8", ".js": "text/javascript; charset=utf-8",269 ".json": "application/json; charset=utf-8", ".svg": "image/svg+xml", ".png": "image/png", ".ico": "image/x-icon",270 ".ttf": "font/ttf", ".woff2": "font/woff2", ".txt": "text/plain; charset=utf-8", ".xml": "application/xml; charset=utf-8",271 ".webmanifest": "application/manifest+json; charset=utf-8",272};273274function sendJSON(res, code, obj) {275 const body = JSON.stringify(obj);276 res.writeHead(code, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "public, max-age=120", "Access-Control-Allow-Origin": "*" });277 res.end(body);278}279280/* ---- SEO : SSR léger des metas — title/description/canonical/og + JSON-LD281 par route, injectés dans index.html (SPA rendue client, metas rendues282 serveur — même approche que les autres sites Ka). ---- */283const BASE_URL = "https://www.ka-stats.com";284const SEO_ROUTES = {285 "/": {286 title: "Ka·Stats — L'explorateur de statistiques du Québec",287 desc: "Loyers, propriétés, autos, emplois, épicerie, restos, sorties, créateurs : les statistiques vivantes du Québec, agrégées en direct depuis les 13 plateformes du Groupe KA. Graphiques interactifs, tendances, palmarès et rapports PDF.",288 },289 "/indicateurs": {290 title: "Les chiffres du Québec en direct — loyers, prix, salaires | Ka·Stats",291 desc: "Les indicateurs du Québec par thème : loyer moyen, prix des propriétés, valeur foncière, prix des voitures usagées, salaires affichés, prix d'épicerie — extraits en direct des plateformes du Groupe KA.",292 },293 "/comparer": {294 title: "Comparer les plateformes du Groupe KA — indice base 100 | Ka·Stats",295 desc: "Comparez la croissance des plateformes du Groupe KA sur un seul axe (indice base 100) : logements, propriétés, autos, emplois, produits, événements, créateurs, pages indexées.",296 },297 "/studio": {298 title: "Studio d'indicateurs — construisez vos graphiques sur mesure | Ka·Stats",299 desc: "Construisez votre propre indicateur du Québec : choisissez jusqu'à 4 métriques parmi plus de 200, appliquez une transformation (indice 100, variation, moyenne mobile, cumul) ou un ratio, choisissez le type de graphique, enregistrez et partagez.",300 },301 "/palmares": {302 title: "Palmarès & records des données du Québec | Ka·Stats",303 desc: "Jours records, plus fortes croissances et faits marquants détectés dans les données des 13 plateformes du Groupe KA : logements, propriétés, autos, emplois, épicerie, restos, sorties et plus.",304 },305};306function seoFor(pathname) {307 const clean = pathname.replace(/\/+$/, "") || "/";308 if (SEO_ROUTES[clean]) return { ...SEO_ROUTES[clean], path: clean === "/" ? "/" : clean };309 const m = /^\/site\/([a-z0-9-]+)$/.exec(clean);310 if (m && SITE_BY_ID[m[1]]) {311 const s = SITE_BY_ID[m[1]];312 return {313 title: `Statistiques ${s.wordmark} — ${s.tagline} | Ka·Stats`,314 desc: `Le tableau de bord statistique complet de ${s.wordmark} (${s.tagline.charAt(0).toLowerCase()}${s.tagline.slice(1)}) : indicateurs clés, évolution quotidienne, répartitions, distributions, records et rapports PDF — mis à jour en continu par Ka·Stats, l'observatoire du Groupe KA.`,315 path: clean,316 };317 }318 return { ...SEO_ROUTES["/"], path: "/" };319}320const escHtml = (s) => String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);321let indexCache = { mtime: 0, html: "" };322function renderIndex(pathname) {323 const file = path.join(PUB, "index.html");324 const mtime = fs.statSync(file).mtimeMs;325 if (mtime !== indexCache.mtime) indexCache = { mtime, html: fs.readFileSync(file, "utf8") };326 const m = seoFor(pathname);327 const url = BASE_URL + (m.path === "/" ? "/" : m.path);328 const jsonld = JSON.stringify({329 "@context": "https://schema.org",330 "@graph": [331 { "@type": "Organization", "@id": "https://www.groupe-ka.com/#org", name: "Groupe KA", url: "https://www.groupe-ka.com", logo: BASE_URL + "/apple-touch-icon.png", email: "contact@groupe-ka.com", areaServed: "CA-QC" },332 { "@type": "WebSite", "@id": BASE_URL + "/#website", name: "Ka·Stats", alternateName: "Ka-Stats — L'explorateur de statistiques du Québec", url: BASE_URL + "/", inLanguage: "fr-CA", publisher: { "@id": "https://www.groupe-ka.com/#org" } },333 { "@type": "WebPage", "@id": url + "#webpage", url, name: m.title, description: m.desc, inLanguage: "fr-CA", isPartOf: { "@id": BASE_URL + "/#website" }, primaryImageOfPage: BASE_URL + "/og.png" },334 ],335 });336 return indexCache.html337 .replace(/<title>[^<]*<\/title>/, `<title>${escHtml(m.title)}</title>`)338 .replace(/(<meta name="description" content=")[^"]*(")/, `$1${escHtml(m.desc)}$2`)339 .replace(/(<meta property="og:title" content=")[^"]*(")/, `$1${escHtml(m.title)}$2`)340 .replace(/(<meta property="og:description" content=")[^"]*(")/, `$1${escHtml(m.desc)}$2`)341 .replace(/(<meta name="twitter:title" content=")[^"]*(")/, `$1${escHtml(m.title)}$2`)342 .replace(/(<meta property="og:url" content=")[^"]*(")/, `$1${url}$2`)343 .replace("<!--SEO-->", `<link rel="canonical" href="${url}" />\n <script type="application/ld+json">${jsonld}</script>`);344}345346function sendFile(res, file) {347 const ext = path.extname(file).toLowerCase();348 const mime = MIME[ext] || "application/octet-stream";349 const cacheCtl = ext === ".html" ? "no-cache" : "public, max-age=3600";350 fs.readFile(file, (err, buf) => {351 if (err) { res.writeHead(404, { "Content-Type": "text/plain" }); res.end("404"); return; }352 res.writeHead(200, { "Content-Type": mime, "Cache-Control": cacheCtl });353 res.end(buf);354 });355}356357const server = http.createServer(async (req, res) => {358 const u = new URL(req.url, "http://x");359 const p = u.pathname;360 const q = Object.fromEntries(u.searchParams.entries());361 try {362 if (p === "/healthz" || p === "/api/health") return sendJSON(res, 200, { ok: true, app: "ka-stats", sites: SITES.length, uptime: process.uptime() });363 if (p === "/api/sites") return sendJSON(res, 200, { sites: SITES });364 if (p === "/api/overview") return sendJSON(res, 200, await getOverview(q));365 if (p === "/api/catalog") return sendJSON(res, 200, await getCatalog(q));366 if (p === "/api/metric") {367 const m = await getMetric(q.ref || "", q);368 return m ? sendJSON(res, 200, m) : sendJSON(res, 404, { error: "métrique introuvable", ref: q.ref || "" });369 }370 if (p.startsWith("/api/dashboard/")) {371 const id = p.slice("/api/dashboard/".length).replace(/[^a-z0-9-]/g, "");372 if (!SITE_BY_ID[id]) return sendJSON(res, 404, { error: "plateforme inconnue", id });373 const r = await getDashboard(id, q);374 return sendJSON(res, r.ok ? 200 : 502, r);375 }376 if (p.startsWith("/api/")) return sendJSON(res, 404, { error: "route inconnue" });377 // Statique + repli SPA avec metas SSR (routes /, /site/:id, /indicateurs, /comparer, /studio, /palmares)378 const file = path.normalize(path.join(PUB, p === "/" ? "index.html" : p));379 if (!file.startsWith(PUB)) { res.writeHead(403); return res.end(); }380 if (p === "/" || p === "/index.html" || !path.extname(file) || !fs.existsSync(file)) {381 const html = renderIndex(p);382 res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-cache" });383 return res.end(html);384 }385 return sendFile(res, file);386 } catch (e) {387 return sendJSON(res, 500, { error: String((e && e.message) || e) });388 }389});390391server.listen(PORT, "0.0.0.0", () => {392 console.log(`[ka-stats] en écoute sur :${PORT} — ${SITES.length} plateformes agrégées, cache ${TTL_MS / 60000} min`);393});394