SPB Git

spb/ka-stats

Public
21.3 KB · 393 lines javascript
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 12 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: "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" },39];40const SITE_BY_ID = Object.fromEntries(SITES.map((s) => [s.id, s]));4142const PERIODS = new Set(["auj", "7j", "30j", "3m", "6m", "12m", "annee", "tout"]);43const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;4445/* ---- Cache (mémoire + disque) ---- */46let cache = {};47try { cache = JSON.parse(fs.readFileSync(CACHE_FILE, "utf8")); } catch { cache = {}; }48let persistTimer = null;49function persistCache() {50  clearTimeout(persistTimer);51  persistTimer = setTimeout(() => {52    // On ne persiste que les périodes standard (pas les plages custom, illimitées)53    const keep = {};54    for (const [k, v] of Object.entries(cache)) if (!k.includes("from=")) keep[k] = v;55    fs.writeFile(CACHE_FILE, JSON.stringify(keep), () => {});56  }, 2000);57}5859const inflight = new Map();6061function buildQuery(q) {62  const period = PERIODS.has(q.period) ? q.period : "30j";63  const parts = [`period=${period}`];64  if (DATE_RE.test(q.from || "") && DATE_RE.test(q.to || "")) {65    parts.push(`from=${q.from}`, `to=${q.to}`);66  }67  return parts.join("&");68}6970async function fetchRemote(site, query) {71  const url = `https://${site.domain}/api/stats/dashboard?${query}`;72  const ac = new AbortController();73  const timer = setTimeout(() => ac.abort(), 45000);74  try {75    const r = await fetch(url, {76      signal: ac.signal,77      headers: { "User-Agent": "ka-stats/1.0 (Groupe KA; +https://www.ka-stats.com)", Accept: "application/json" },78    });79    if (!r.ok) throw new Error(`HTTP ${r.status}`);80    let j = await r.json();81    if (j && j.success === true && j.data) j = j.data; // enveloppe api-ka82    if (!j || typeof j !== "object" || (!j.kpis && !j.series)) throw new Error("payload inattendu");83    return j;84  } finally {85    clearTimeout(timer);86  }87}8889async function getDashboard(siteId, q) {90  const site = SITE_BY_ID[siteId];91  if (!site) return null;92  const query = buildQuery(q);93  const key = `${siteId}|${query}`;94  const hit = cache[key];95  const now = Date.now();96  if (hit && now - hit.at < TTL_MS) return { site: siteId, ok: true, stale: false, fetched: hit.at, data: hit.data };97  if (inflight.has(key)) return inflight.get(key);98  const p = (async () => {99    try {100      const data = await fetchRemote(site, query);101      cache[key] = { at: Date.now(), data };102      persistCache();103      return { site: siteId, ok: true, stale: false, fetched: cache[key].at, data };104    } catch (e) {105      if (hit) return { site: siteId, ok: true, stale: true, fetched: hit.at, error: String(e.message || e), data: hit.data };106      return { site: siteId, ok: false, stale: false, error: String(e.message || e), data: null };107    } finally {108      inflight.delete(key);109    }110  })();111  inflight.set(key, p);112  return p;113}114115/* ---- Vue d'ensemble consolidée (calculée serveur, légère) ---- */116const MONEY_RE = /\$/;117function summarize(res) {118  const site = SITE_BY_ID[res.site];119  if (!res.ok || !res.data) return { id: res.site, ok: false, error: res.error || "injoignable" };120  const d = res.data;121  const kpis = Array.isArray(d.kpis) ? d.kpis : [];122  const primary = kpis[0] || null;123  const spark = (primary && primary.spark) || (d.series && d.series[0] && d.series[0].points) || [];124  const money = kpis.filter((k) => MONEY_RE.test(k.unit || "")).slice(0, 4)125    .map((k) => ({ label: k.label, value: k.value, unit: k.unit, delta_pct: k.delta_pct ?? null, spark: k.spark || null }));126  return {127    id: res.site, ok: true, stale: !!res.stale, updated: d.updated || null, fetched: res.fetched,128    period: d.period || null,129    primary: primary ? { id: primary.id, label: primary.label, value: primary.value, unit: primary.unit || "", delta_pct: primary.delta_pct ?? null } : null,130    spark: spark.slice(-90),131    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 })),132    money,133    gauges: (d.gauges || []).slice(0, 3),134    records: (d.records || []).slice(0, 4),135    sections: {136      series: (d.series || []).length, multiseries: (d.multiseries || []).length,137      stacked: (d.stacked || []).length, breakdowns: (d.breakdowns || []).length,138      distributions: (d.distributions || []).length, tables: (d.tables || []).length,139      geo: d.geo ? 1 : 0, heatmap: d.heatmap ? 1 : 0, hourly: d.hourly ? 1 : 0,140      records: (d.records || []).length, kpis: kpis.length, gauges: (d.gauges || []).length,141    },142    theme: site.theme,143  };144}145146async function getOverview(q) {147  const results = await Promise.all(SITES.map((s) => getDashboard(s.id, q)));148  const sites = results.map(summarize);149  // Volume agrégé par jour — même règle que le hub : seuls les points datés au150  // JOUR comptent (vrai-prix publie une série annuelle → exclue), un site doit151  // avoir ≥ 8 points quotidiens, on reporte la dernière valeur connue (stocks)152  // et on ne garde que les jours couverts par ≥ 70 % des sites inclus.153  const daily = sites154    .filter((s) => s.ok && Array.isArray(s.spark))155    .map((s) => ({ id: s.id, pts: s.spark.filter((p) => p && DATE_RE.test(String(p.t)) && typeof p.v === "number") }))156    .filter((s) => s.pts.length >= 8);157  const dates = [...new Set(daily.flatMap((s) => s.pts.map((p) => p.t)))].sort();158  const totalSpark = [];159  const lastVal = new Map();160  const ptMaps = daily.map((s) => new Map(s.pts.map((p) => [p.t, p.v])));161  for (const t of dates) {162    let sum = 0, covered = 0;163    daily.forEach((s, i) => {164      const v = ptMaps[i].get(t);165      if (typeof v === "number") lastVal.set(s.id, v);166      if (lastVal.has(s.id)) { sum += lastVal.get(s.id); covered++; }167    });168    if (covered >= Math.ceil(daily.length * 0.7)) totalSpark.push({ t, v: Math.round(sum) });169  }170  const totalItems = sites.reduce((acc, s) => acc + (s.ok && s.primary && typeof s.primary.value === "number" ? s.primary.value : 0), 0);171  const indicators = [];172  for (const s of sites) {173    if (!s.ok) continue;174    for (const m of s.money) indicators.push({ site: s.id, ...m });175  }176  const records = [];177  for (const [i, s] of sites.entries()) {178    if (!s.ok) continue;179    for (const r of s.records) records.push({ site: s.id, ...r });180  }181  const chartsTotal = sites.reduce((acc, s) => {182    if (!s.ok || !s.sections) return acc;183    const c = s.sections;184    return acc + c.series + c.multiseries + c.stacked + c.breakdowns + c.distributions + c.geo + c.heatmap + c.hourly + c.kpis + c.gauges;185  }, 0);186  return {187    updated: new Date().toISOString(),188    period: sites.find((s) => s.ok && s.period)?.period || null,189    totals: { items: totalItems, sitesLive: sites.filter((s) => s.ok).length, sites: SITES.length, charts: chartsTotal, spark: totalSpark },190    sites, indicators, records,191  };192}193194/* ---- Catalogue de métriques (pour le Studio d'indicateurs sur mesure) ----195   Aplati tout ce qui est traçable dans les 12 dashboards en références196   adressables : site|kpi|<id> · site|series|<id> · site|multi|<id>|<i> ·197   site|stacked|<id>|<i>. /api/catalog = métadonnées ; /api/metric = points. */198function catalogOf(siteId, d) {199  const site = SITE_BY_ID[siteId];200  const out = [];201  const push = (ref, label, unit, n) => out.push({ ref, site: siteId, wordmark: site.wordmark, label, unit: unit || "", n });202  for (const k of d.kpis || []) {203    if (Array.isArray(k.spark) && k.spark.length > 2) push(`${siteId}|kpi|${k.id}`, k.label, k.unit, k.spark.length);204  }205  for (const s of d.series || []) {206    if (Array.isArray(s.points) && s.points.length > 1) push(`${siteId}|series|${s.id}`, s.title, s.unit, s.points.length);207  }208  for (const m of d.multiseries || []) {209    (m.series || []).forEach((sub, i) => {210      if (Array.isArray(sub.points) && sub.points.length > 1) push(`${siteId}|multi|${m.id}|${i}`, `${m.title} — ${sub.label}`, m.unit, sub.points.length);211    });212  }213  for (const st of d.stacked || []) {214    (st.keys || []).forEach((key, i) => {215      const pts = (st.points || []).filter((p) => Array.isArray(p.values) && typeof p.values[i] === "number");216      if (pts.length > 1) push(`${siteId}|stacked|${st.id}|${i}`, `${st.title} — ${key}`, st.unit, pts.length);217    });218  }219  return out;220}221222function resolveMetric(d, kind, id, sub) {223  if (kind === "kpi") {224    const k = (d.kpis || []).find((x) => x.id === id);225    return k && k.spark ? { points: k.spark, unit: k.unit || "", label: k.label } : null;226  }227  if (kind === "series") {228    const s = (d.series || []).find((x) => x.id === id);229    return s ? { points: s.points || [], unit: s.unit || "", label: s.title } : null;230  }231  if (kind === "multi") {232    const m = (d.multiseries || []).find((x) => x.id === id);233    const s = m && (m.series || [])[Number(sub)];234    return s ? { points: s.points || [], unit: m.unit || "", label: `${m.title} — ${s.label}` } : null;235  }236  if (kind === "stacked") {237    const st = (d.stacked || []).find((x) => x.id === id);238    if (!st) return null;239    const i = Number(sub);240    const key = (st.keys || [])[i];241    if (key === undefined) return null;242    const points = (st.points || []).filter((p) => Array.isArray(p.values)).map((p) => ({ t: p.t, v: p.values[i] ?? 0 }));243    return { points, unit: st.unit || "", label: `${st.title} — ${key}` };244  }245  return null;246}247248async function getCatalog(q) {249  const results = await Promise.all(SITES.map((s) => getDashboard(s.id, q)));250  const metrics = [];251  for (const r of results) if (r.ok && r.data) metrics.push(...catalogOf(r.site, r.data));252  return { updated: new Date().toISOString(), count: metrics.length, metrics };253}254255async function getMetric(ref, q) {256  const [siteId, kind, id, sub] = String(ref).split("|");257  if (!SITE_BY_ID[siteId]) return null;258  const r = await getDashboard(siteId, q);259  if (!r.ok || !r.data) return null;260  const m = resolveMetric(r.data, kind, id, sub);261  if (!m) return null;262  return { ref, site: siteId, ...m, stale: !!r.stale };263}264265/* ---- HTTP ---- */266const MIME = {267  ".html": "text/html; charset=utf-8", ".css": "text/css; charset=utf-8", ".js": "text/javascript; charset=utf-8",268  ".json": "application/json; charset=utf-8", ".svg": "image/svg+xml", ".png": "image/png", ".ico": "image/x-icon",269  ".ttf": "font/ttf", ".woff2": "font/woff2", ".txt": "text/plain; charset=utf-8", ".xml": "application/xml; charset=utf-8",270  ".webmanifest": "application/manifest+json; charset=utf-8",271};272273function sendJSON(res, code, obj) {274  const body = JSON.stringify(obj);275  res.writeHead(code, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "public, max-age=120", "Access-Control-Allow-Origin": "*" });276  res.end(body);277}278279/* ---- SEO : SSR léger des metas — title/description/canonical/og + JSON-LD280   par route, injectés dans index.html (SPA rendue client, metas rendues281   serveur — même approche que les autres sites Ka). ---- */282const BASE_URL = "https://www.ka-stats.com";283const SEO_ROUTES = {284  "/": {285    title: "Ka·Stats — L'explorateur de statistiques du Québec",286    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 12 plateformes du Groupe KA. Graphiques interactifs, tendances, palmarès et rapports PDF.",287  },288  "/indicateurs": {289    title: "Les chiffres du Québec en direct — loyers, prix, salaires | Ka·Stats",290    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.",291  },292  "/comparer": {293    title: "Comparer les plateformes du Groupe KA — indice base 100 | Ka·Stats",294    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.",295  },296  "/studio": {297    title: "Studio d'indicateurs — construisez vos graphiques sur mesure | Ka·Stats",298    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.",299  },300  "/palmares": {301    title: "Palmarès & records des données du Québec | Ka·Stats",302    desc: "Jours records, plus fortes croissances et faits marquants détectés dans les données des 12 plateformes du Groupe KA : logements, propriétés, autos, emplois, épicerie, restos, sorties et plus.",303  },304};305function seoFor(pathname) {306  const clean = pathname.replace(/\/+$/, "") || "/";307  if (SEO_ROUTES[clean]) return { ...SEO_ROUTES[clean], path: clean === "/" ? "/" : clean };308  const m = /^\/site\/([a-z0-9-]+)$/.exec(clean);309  if (m && SITE_BY_ID[m[1]]) {310    const s = SITE_BY_ID[m[1]];311    return {312      title: `Statistiques ${s.wordmark} — ${s.tagline} | Ka·Stats`,313      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.`,314      path: clean,315    };316  }317  return { ...SEO_ROUTES["/"], path: "/" };318}319const escHtml = (s) => String(s).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");320let indexCache = { mtime: 0, html: "" };321function renderIndex(pathname) {322  const file = path.join(PUB, "index.html");323  const mtime = fs.statSync(file).mtimeMs;324  if (mtime !== indexCache.mtime) indexCache = { mtime, html: fs.readFileSync(file, "utf8") };325  const m = seoFor(pathname);326  const url = BASE_URL + (m.path === "/" ? "/" : m.path);327  const jsonld = JSON.stringify({328    "@context": "https://schema.org",329    "@graph": [330      { "@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" },331      { "@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" } },332      { "@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" },333    ],334  });335  return indexCache.html336    .replace(/<title>[^<]*<\/title>/, `<title>${escHtml(m.title)}</title>`)337    .replace(/(<meta name="description" content=")[^"]*(")/, `$1${escHtml(m.desc)}$2`)338    .replace(/(<meta property="og:title" content=")[^"]*(")/, `$1${escHtml(m.title)}$2`)339    .replace(/(<meta property="og:description" content=")[^"]*(")/, `$1${escHtml(m.desc)}$2`)340    .replace(/(<meta name="twitter:title" content=")[^"]*(")/, `$1${escHtml(m.title)}$2`)341    .replace(/(<meta property="og:url" content=")[^"]*(")/, `$1${url}$2`)342    .replace("<!--SEO-->", `<link rel="canonical" href="${url}" />\n  <script type="application/ld+json">${jsonld}</script>`);343}344345function sendFile(res, file) {346  const ext = path.extname(file).toLowerCase();347  const mime = MIME[ext] || "application/octet-stream";348  const cacheCtl = ext === ".html" ? "no-cache" : "public, max-age=3600";349  fs.readFile(file, (err, buf) => {350    if (err) { res.writeHead(404, { "Content-Type": "text/plain" }); res.end("404"); return; }351    res.writeHead(200, { "Content-Type": mime, "Cache-Control": cacheCtl });352    res.end(buf);353  });354}355356const server = http.createServer(async (req, res) => {357  const u = new URL(req.url, "http://x");358  const p = u.pathname;359  const q = Object.fromEntries(u.searchParams.entries());360  try {361    if (p === "/healthz" || p === "/api/health") return sendJSON(res, 200, { ok: true, app: "ka-stats", sites: SITES.length, uptime: process.uptime() });362    if (p === "/api/sites") return sendJSON(res, 200, { sites: SITES });363    if (p === "/api/overview") return sendJSON(res, 200, await getOverview(q));364    if (p === "/api/catalog") return sendJSON(res, 200, await getCatalog(q));365    if (p === "/api/metric") {366      const m = await getMetric(q.ref || "", q);367      return m ? sendJSON(res, 200, m) : sendJSON(res, 404, { error: "métrique introuvable", ref: q.ref || "" });368    }369    if (p.startsWith("/api/dashboard/")) {370      const id = p.slice("/api/dashboard/".length).replace(/[^a-z0-9-]/g, "");371      if (!SITE_BY_ID[id]) return sendJSON(res, 404, { error: "plateforme inconnue", id });372      const r = await getDashboard(id, q);373      return sendJSON(res, r.ok ? 200 : 502, r);374    }375    if (p.startsWith("/api/")) return sendJSON(res, 404, { error: "route inconnue" });376    // Statique + repli SPA avec metas SSR (routes /, /site/:id, /indicateurs, /comparer, /studio, /palmares)377    const file = path.normalize(path.join(PUB, p === "/" ? "index.html" : p));378    if (!file.startsWith(PUB)) { res.writeHead(403); return res.end(); }379    if (p === "/" || p === "/index.html" || !path.extname(file) || !fs.existsSync(file)) {380      const html = renderIndex(p);381      res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-cache" });382      return res.end(html);383    }384    return sendFile(res, file);385  } catch (e) {386    return sendJSON(res, 500, { error: String((e && e.message) || e) });387  }388});389390server.listen(PORT, "0.0.0.0", () => {391  console.log(`[ka-stats] en écoute sur :${PORT} — ${SITES.length} plateformes agrégées, cache ${TTL_MS / 60000} min`);392});393
Files · 764cc03cc66bf0d1a41da03c7db94c2c8107259b
Menu

Theme