import { Client } from "@notionhq/client"; import { writeFileSync } from "fs"; import { checkDataConsistency } from "./data/checkDataConsistency"; import { fetchFamiliesWithEventsFromNotion } from "./notion/fetch/fetchFamiliesWithEventsFromNotion"; import { publishStatisticsToNotion } from "./notion/publish/v1/publishStatisticsToNotion"; import { publishStatsToPage } from "./notion/publish/v2/publishStatsToPage"; import { computeELStats } from "./statistiques/v1/computeELStats"; import { computeStatsParAnciennete } from "./statistiques/v1/computeEvenementsParAnciennete"; import { computeStatsPenales } from "./statistiques/v2/penales/computeStatsPenales"; import { statsPenalesDesc } from "./statistiques/v2/penales/StatsPenales"; import { computeStatsGenerales } from "./statistiques/v2/generales/computeStatsGenerales"; import { statsGeneralesDesc } from "./statistiques/v2/generales/StatsGenerales"; import { computeStatsSociales } from "./statistiques/v2/sociales/computeStatsSociales"; import { statsSocialesDesc } from "./statistiques/v2/sociales/StatsSociales"; import { checkDbSchemas } from "./notion/fetch/schemaCheck/checkDbSchemas"; import { computeSequencEvtPenalSankeyData } from "./statistiques/v2/penales/computeSankeyData"; import { sankeyDataToMermaidDiagram } from "./statistiques/v2/sankey/sankeyDataToMermaidDiagram"; import { formatDate } from "date-fns"; type ProcessOptions = { dryRun: boolean; notionApiToken: string; }; /** * Build options from process args and env */ function buildProcessOptions(): ProcessOptions { const notionApiToken = process.env.NOTION_TOKEN; if (!notionApiToken) { throw new Error("process.env.NOTION_TOKEN not defined"); } const dryRun = process.argv.length >= 2 && process.argv[2] === "--dry-run"; return { dryRun: dryRun, notionApiToken: notionApiToken, }; } (async function IIFE() { const options = buildProcessOptions(); const notionClient = new Client({ auth: options.notionApiToken, }); console.log("Checking DB schemas..."); await checkDbSchemas(notionClient); console.log("Fetching families..."); const doFetch = true; const familles = doFetch ? await fetchFamiliesWithEventsFromNotion(notionClient) : []; console.log("Checking Data Consistency issues..."); const consistencyReport = checkDataConsistency(familles); if (consistencyReport.errors.length > 0) { console.error( `Found ${consistencyReport.errors.length} consistency error(s) that prevent building stats:` ); console.error(consistencyReport.errors); process.exit(1); } if (consistencyReport.warnings.length > 0) { console.warn( `Found ${consistencyReport.warnings.length} non blocking consistency warning(s):` ); console.warn(consistencyReport.warnings); } const currentDate = new Date(Date.now()); console.log("Building statistics..."); const elStats = computeELStats(familles, currentDate); const statsParAnciennete = computeStatsParAnciennete(familles); writeFileSync( "./el-stats-par-anciennete.json", JSON.stringify(statsParAnciennete, null, " ") ); const statsGenerales = computeStatsGenerales(familles); const statsPenales = computeStatsPenales(familles); const statsSociales = computeStatsSociales(familles); console.log("Sankey Diagram:"); writeFileSync( "./el-stats-penal-sankey-diagram.txt", sankeyDataToMermaidDiagram(computeSequencEvtPenalSankeyData(familles)) ); if (options.dryRun) { console.log( "Dry run => Skip Publishing. Stats are dumped in file el-stats-xxx.json" ); writeFileSync("./el-stats-v1.json", JSON.stringify(elStats, null, " ")); writeFileSync( "./el-stats-v2.json", JSON.stringify( { generales: statsGenerales, penales: statsPenales, sociales: statsSociales, }, null, " " ) ); } else { console.log("Publishing statistics..."); await publishStatisticsToNotion(elStats, currentDate, notionClient); const header = ` Note: Sauf mention contraire les statistiques sont basées sur les familles résistantes & ex-résistantes. Dernière mise à jour le ${formatDate(currentDate, "dd/MM/yyyy à HH:mm")} `; await publishStatsToPage( notionClient, "313751fb-daed-4b33-992f-c86d7ac2de37", header, statsGeneralesDesc, statsGenerales ); await publishStatsToPage( notionClient, "969eac5c-a4eb-49d4-b4ad-c341c9c4c785", header, statsPenalesDesc, statsPenales ); await publishStatsToPage( notionClient, "68a8c03a-a4a2-4ff3-b044-7c8dbbdc28ce", header, statsSocialesDesc, statsSociales ); } })();