/* ----------------------------------------------------------------------------- Auteur : Simon-Pierre Boucher — contact@spboucher.ai Fichier : ka-stats/server.mjs Desc. : Ka·Stats — l'explorateur de statistiques du Québec (Groupe KA). Serveur Node ZÉRO dépendance : sert le front statique et agrège les dashboards /api/stats/dashboard (contrat ka-stats v2) des 12 plateformes du Groupe KA, avec cache mémoire + disque et stale-while-error (un satellite injoignable ⇒ dernière bonne copie). Usage : node server.mjs [port] (défaut 8140) — healthcheck: /healthz ----------------------------------------------------------------------------- */ import http from "node:http"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const PORT = Number(process.argv[2] || process.env.PORT || 8140); const ROOT = path.dirname(fileURLToPath(import.meta.url)); const PUB = path.join(ROOT, "public"); const DATA = path.join(ROOT, "data"); const CACHE_FILE = path.join(DATA, "cache.json"); const TTL_MS = 10 * 60 * 1000; // 10 min par (site, période) fs.mkdirSync(DATA, { recursive: true }); /* ---- Les 13 plateformes publiques du Groupe KA (contrat ka-stats v2) ---- Accents = ecosystem.json de ka-ui (job-ka relevé sur le site). */ const SITES = [ { 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" }, { 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" }, { id: "house-ka", wordmark: "House·Ka", domain: "www.house-ka.com", accent: "#0f6b4f", accentSoft: "#dcefe6", accentDeep: "#0a4a37", onAccent: "#ffffff", tagline: "Maisons à vendre — Canada hors Québec (EN)", theme: "habitation" }, { 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" }, { 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" }, { 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" }, { 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" }, { 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" }, { 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" }, { 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" }, { 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" }, { 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" }, { 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" }, { 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" }, ]; const SITE_BY_ID = Object.fromEntries(SITES.map((s) => [s.id, s])); const PERIODS = new Set(["auj", "7j", "30j", "3m", "6m", "12m", "annee", "tout"]); const DATE_RE = /^\d{4}-\d{2}-\d{2}$/; /* ---- Cache (mémoire + disque) ---- */ let cache = {}; try { cache = JSON.parse(fs.readFileSync(CACHE_FILE, "utf8")); } catch { cache = {}; } let persistTimer = null; function persistCache() { clearTimeout(persistTimer); persistTimer = setTimeout(() => { // On ne persiste que les périodes standard (pas les plages custom, illimitées) const keep = {}; for (const [k, v] of Object.entries(cache)) if (!k.includes("from=")) keep[k] = v; fs.writeFile(CACHE_FILE, JSON.stringify(keep), () => {}); }, 2000); } const inflight = new Map(); function buildQuery(q) { const period = PERIODS.has(q.period) ? q.period : "30j"; const parts = [`period=${period}`]; if (DATE_RE.test(q.from || "") && DATE_RE.test(q.to || "")) { parts.push(`from=${q.from}`, `to=${q.to}`); } return parts.join("&"); } async function fetchRemote(site, query) { const url = `https://${site.domain}/api/stats/dashboard?${query}`; const ac = new AbortController(); const timer = setTimeout(() => ac.abort(), 45000); try { const r = await fetch(url, { signal: ac.signal, headers: { "User-Agent": "ka-stats/1.0 (Groupe KA; +https://www.ka-stats.com)", Accept: "application/json" }, }); if (!r.ok) throw new Error(`HTTP ${r.status}`); let j = await r.json(); if (j && j.success === true && j.data) j = j.data; // enveloppe api-ka if (!j || typeof j !== "object" || (!j.kpis && !j.series)) throw new Error("payload inattendu"); return j; } finally { clearTimeout(timer); } } async function getDashboard(siteId, q) { const site = SITE_BY_ID[siteId]; if (!site) return null; const query = buildQuery(q); const key = `${siteId}|${query}`; const hit = cache[key]; const now = Date.now(); if (hit && now - hit.at < TTL_MS) return { site: siteId, ok: true, stale: false, fetched: hit.at, data: hit.data }; if (inflight.has(key)) return inflight.get(key); const p = (async () => { try { const data = await fetchRemote(site, query); cache[key] = { at: Date.now(), data }; persistCache(); return { site: siteId, ok: true, stale: false, fetched: cache[key].at, data }; } catch (e) { if (hit) return { site: siteId, ok: true, stale: true, fetched: hit.at, error: String(e.message || e), data: hit.data }; return { site: siteId, ok: false, stale: false, error: String(e.message || e), data: null }; } finally { inflight.delete(key); } })(); inflight.set(key, p); return p; } /* ---- Vue d'ensemble consolidée (calculée serveur, légère) ---- */ const MONEY_RE = /\$/; function summarize(res) { const site = SITE_BY_ID[res.site]; if (!res.ok || !res.data) return { id: res.site, ok: false, error: res.error || "injoignable" }; const d = res.data; const kpis = Array.isArray(d.kpis) ? d.kpis : []; const primary = kpis[0] || null; const spark = (primary && primary.spark) || (d.series && d.series[0] && d.series[0].points) || []; const money = kpis.filter((k) => MONEY_RE.test(k.unit || "")).slice(0, 4) .map((k) => ({ label: k.label, value: k.value, unit: k.unit, delta_pct: k.delta_pct ?? null, spark: k.spark || null })); return { id: res.site, ok: true, stale: !!res.stale, updated: d.updated || null, fetched: res.fetched, period: d.period || null, primary: primary ? { id: primary.id, label: primary.label, value: primary.value, unit: primary.unit || "", delta_pct: primary.delta_pct ?? null } : null, spark: spark.slice(-90), 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 })), money, gauges: (d.gauges || []).slice(0, 3), records: (d.records || []).slice(0, 4), sections: { series: (d.series || []).length, multiseries: (d.multiseries || []).length, stacked: (d.stacked || []).length, breakdowns: (d.breakdowns || []).length, distributions: (d.distributions || []).length, tables: (d.tables || []).length, geo: d.geo ? 1 : 0, heatmap: d.heatmap ? 1 : 0, hourly: d.hourly ? 1 : 0, records: (d.records || []).length, kpis: kpis.length, gauges: (d.gauges || []).length, }, theme: site.theme, }; } async function getOverview(q) { const results = await Promise.all(SITES.map((s) => getDashboard(s.id, q))); const sites = results.map(summarize); // Volume agrégé par jour — même règle que le hub : seuls les points datés au // JOUR comptent (vrai-prix publie une série annuelle → exclue), un site doit // avoir ≥ 8 points quotidiens, on reporte la dernière valeur connue (stocks) // et on ne garde que les jours couverts par ≥ 70 % des sites inclus. const daily = sites .filter((s) => s.ok && Array.isArray(s.spark)) .map((s) => ({ id: s.id, pts: s.spark.filter((p) => p && DATE_RE.test(String(p.t)) && typeof p.v === "number") })) .filter((s) => s.pts.length >= 8); const dates = [...new Set(daily.flatMap((s) => s.pts.map((p) => p.t)))].sort(); const totalSpark = []; const lastVal = new Map(); const ptMaps = daily.map((s) => new Map(s.pts.map((p) => [p.t, p.v]))); for (const t of dates) { let sum = 0, covered = 0; daily.forEach((s, i) => { const v = ptMaps[i].get(t); if (typeof v === "number") lastVal.set(s.id, v); if (lastVal.has(s.id)) { sum += lastVal.get(s.id); covered++; } }); if (covered >= Math.ceil(daily.length * 0.7)) totalSpark.push({ t, v: Math.round(sum) }); } const totalItems = sites.reduce((acc, s) => acc + (s.ok && s.primary && typeof s.primary.value === "number" ? s.primary.value : 0), 0); const indicators = []; for (const s of sites) { if (!s.ok) continue; for (const m of s.money) indicators.push({ site: s.id, ...m }); } const records = []; for (const [i, s] of sites.entries()) { if (!s.ok) continue; for (const r of s.records) records.push({ site: s.id, ...r }); } const chartsTotal = sites.reduce((acc, s) => { if (!s.ok || !s.sections) return acc; const c = s.sections; return acc + c.series + c.multiseries + c.stacked + c.breakdowns + c.distributions + c.geo + c.heatmap + c.hourly + c.kpis + c.gauges; }, 0); return { updated: new Date().toISOString(), period: sites.find((s) => s.ok && s.period)?.period || null, totals: { items: totalItems, sitesLive: sites.filter((s) => s.ok).length, sites: SITES.length, charts: chartsTotal, spark: totalSpark }, sites, indicators, records, }; } /* ---- Catalogue de métriques (pour le Studio d'indicateurs sur mesure) ---- Aplati tout ce qui est traçable dans les 13 dashboards en références adressables : site|kpi| · site|series| · site|multi|| · site|stacked||. /api/catalog = métadonnées ; /api/metric = points. */ function catalogOf(siteId, d) { const site = SITE_BY_ID[siteId]; const out = []; const push = (ref, label, unit, n) => out.push({ ref, site: siteId, wordmark: site.wordmark, label, unit: unit || "", n }); for (const k of d.kpis || []) { if (Array.isArray(k.spark) && k.spark.length > 2) push(`${siteId}|kpi|${k.id}`, k.label, k.unit, k.spark.length); } for (const s of d.series || []) { if (Array.isArray(s.points) && s.points.length > 1) push(`${siteId}|series|${s.id}`, s.title, s.unit, s.points.length); } for (const m of d.multiseries || []) { (m.series || []).forEach((sub, i) => { if (Array.isArray(sub.points) && sub.points.length > 1) push(`${siteId}|multi|${m.id}|${i}`, `${m.title} — ${sub.label}`, m.unit, sub.points.length); }); } for (const st of d.stacked || []) { (st.keys || []).forEach((key, i) => { const pts = (st.points || []).filter((p) => Array.isArray(p.values) && typeof p.values[i] === "number"); if (pts.length > 1) push(`${siteId}|stacked|${st.id}|${i}`, `${st.title} — ${key}`, st.unit, pts.length); }); } return out; } function resolveMetric(d, kind, id, sub) { if (kind === "kpi") { const k = (d.kpis || []).find((x) => x.id === id); return k && k.spark ? { points: k.spark, unit: k.unit || "", label: k.label } : null; } if (kind === "series") { const s = (d.series || []).find((x) => x.id === id); return s ? { points: s.points || [], unit: s.unit || "", label: s.title } : null; } if (kind === "multi") { const m = (d.multiseries || []).find((x) => x.id === id); const s = m && (m.series || [])[Number(sub)]; return s ? { points: s.points || [], unit: m.unit || "", label: `${m.title} — ${s.label}` } : null; } if (kind === "stacked") { const st = (d.stacked || []).find((x) => x.id === id); if (!st) return null; const i = Number(sub); const key = (st.keys || [])[i]; if (key === undefined) return null; const points = (st.points || []).filter((p) => Array.isArray(p.values)).map((p) => ({ t: p.t, v: p.values[i] ?? 0 })); return { points, unit: st.unit || "", label: `${st.title} — ${key}` }; } return null; } async function getCatalog(q) { const results = await Promise.all(SITES.map((s) => getDashboard(s.id, q))); const metrics = []; for (const r of results) if (r.ok && r.data) metrics.push(...catalogOf(r.site, r.data)); return { updated: new Date().toISOString(), count: metrics.length, metrics }; } async function getMetric(ref, q) { const [siteId, kind, id, sub] = String(ref).split("|"); if (!SITE_BY_ID[siteId]) return null; const r = await getDashboard(siteId, q); if (!r.ok || !r.data) return null; const m = resolveMetric(r.data, kind, id, sub); if (!m) return null; return { ref, site: siteId, ...m, stale: !!r.stale }; } /* ---- HTTP ---- */ const MIME = { ".html": "text/html; charset=utf-8", ".css": "text/css; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".json": "application/json; charset=utf-8", ".svg": "image/svg+xml", ".png": "image/png", ".ico": "image/x-icon", ".ttf": "font/ttf", ".woff2": "font/woff2", ".txt": "text/plain; charset=utf-8", ".xml": "application/xml; charset=utf-8", ".webmanifest": "application/manifest+json; charset=utf-8", }; function sendJSON(res, code, obj) { const body = JSON.stringify(obj); res.writeHead(code, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "public, max-age=120", "Access-Control-Allow-Origin": "*" }); res.end(body); } /* ---- SEO : SSR léger des metas — title/description/canonical/og + JSON-LD par route, injectés dans index.html (SPA rendue client, metas rendues serveur — même approche que les autres sites Ka). ---- */ const BASE_URL = "https://www.ka-stats.com"; const SEO_ROUTES = { "/": { title: "Ka·Stats — L'explorateur de statistiques du Québec", 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.", }, "/indicateurs": { title: "Les chiffres du Québec en direct — loyers, prix, salaires | Ka·Stats", 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.", }, "/comparer": { title: "Comparer les plateformes du Groupe KA — indice base 100 | Ka·Stats", 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.", }, "/studio": { title: "Studio d'indicateurs — construisez vos graphiques sur mesure | Ka·Stats", 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.", }, "/palmares": { title: "Palmarès & records des données du Québec | Ka·Stats", 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.", }, }; function seoFor(pathname) { const clean = pathname.replace(/\/+$/, "") || "/"; if (SEO_ROUTES[clean]) return { ...SEO_ROUTES[clean], path: clean === "/" ? "/" : clean }; const m = /^\/site\/([a-z0-9-]+)$/.exec(clean); if (m && SITE_BY_ID[m[1]]) { const s = SITE_BY_ID[m[1]]; return { title: `Statistiques ${s.wordmark} — ${s.tagline} | Ka·Stats`, 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.`, path: clean, }; } return { ...SEO_ROUTES["/"], path: "/" }; } const escHtml = (s) => String(s).replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); let indexCache = { mtime: 0, html: "" }; function renderIndex(pathname) { const file = path.join(PUB, "index.html"); const mtime = fs.statSync(file).mtimeMs; if (mtime !== indexCache.mtime) indexCache = { mtime, html: fs.readFileSync(file, "utf8") }; const m = seoFor(pathname); const url = BASE_URL + (m.path === "/" ? "/" : m.path); const jsonld = JSON.stringify({ "@context": "https://schema.org", "@graph": [ { "@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" }, { "@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" } }, { "@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" }, ], }); return indexCache.html .replace(/[^<]*<\/title>/, `<title>${escHtml(m.title)}`) .replace(/(", `\n `); } function sendFile(res, file) { const ext = path.extname(file).toLowerCase(); const mime = MIME[ext] || "application/octet-stream"; const cacheCtl = ext === ".html" ? "no-cache" : "public, max-age=3600"; fs.readFile(file, (err, buf) => { if (err) { res.writeHead(404, { "Content-Type": "text/plain" }); res.end("404"); return; } res.writeHead(200, { "Content-Type": mime, "Cache-Control": cacheCtl }); res.end(buf); }); } const server = http.createServer(async (req, res) => { const u = new URL(req.url, "http://x"); const p = u.pathname; const q = Object.fromEntries(u.searchParams.entries()); try { if (p === "/healthz" || p === "/api/health") return sendJSON(res, 200, { ok: true, app: "ka-stats", sites: SITES.length, uptime: process.uptime() }); if (p === "/api/sites") return sendJSON(res, 200, { sites: SITES }); if (p === "/api/overview") return sendJSON(res, 200, await getOverview(q)); if (p === "/api/catalog") return sendJSON(res, 200, await getCatalog(q)); if (p === "/api/metric") { const m = await getMetric(q.ref || "", q); return m ? sendJSON(res, 200, m) : sendJSON(res, 404, { error: "métrique introuvable", ref: q.ref || "" }); } if (p.startsWith("/api/dashboard/")) { const id = p.slice("/api/dashboard/".length).replace(/[^a-z0-9-]/g, ""); if (!SITE_BY_ID[id]) return sendJSON(res, 404, { error: "plateforme inconnue", id }); const r = await getDashboard(id, q); return sendJSON(res, r.ok ? 200 : 502, r); } if (p.startsWith("/api/")) return sendJSON(res, 404, { error: "route inconnue" }); // Statique + repli SPA avec metas SSR (routes /, /site/:id, /indicateurs, /comparer, /studio, /palmares) const file = path.normalize(path.join(PUB, p === "/" ? "index.html" : p)); if (!file.startsWith(PUB)) { res.writeHead(403); return res.end(); } if (p === "/" || p === "/index.html" || !path.extname(file) || !fs.existsSync(file)) { const html = renderIndex(p); res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-cache" }); return res.end(html); } return sendFile(res, file); } catch (e) { return sendJSON(res, 500, { error: String((e && e.message) || e) }); } }); server.listen(PORT, "0.0.0.0", () => { console.log(`[ka-stats] en écoute sur :${PORT} — ${SITES.length} plateformes agrégées, cache ${TTL_MS / 60000} min`); });