#!/usr/bin/env node const { loadArticles } = require("../lib/stats/articles"); const { renderWithPython } = require("../lib/stats/python"); const MONTH_LABELS = ["Jan", "Fev", "Mar", "Avr", "Mai", "Jun", "Jul", "Aou", "Sep", "Oct", "Nov", "Dec"]; function groupAverageWordsByMonth(articles) { const buckets = new Map(); const years = new Set(); let totalWords = 0; let totalArticles = 0; for (const article of articles) { if (!article.date) continue; const year = article.date.year; const month = article.date.month; const key = `${year}-${month}`; years.add(year); const current = buckets.get(key) || { words: 0, count: 0 }; current.words += article.wordCount || 0; current.count += 1; buckets.set(key, current); totalWords += article.wordCount || 0; totalArticles += 1; } const monthNumbers = Array.from({ length: 12 }, (_, index) => index + 1); const labels = monthNumbers.map((month) => MONTH_LABELS[month - 1]); const sortedYears = Array.from(years).sort((a, b) => a - b); const series = sortedYears.map((year) => { const values = monthNumbers.map((month) => { const entry = buckets.get(`${year}-${month}`); if (!entry || entry.count === 0) return 0; return Math.round(entry.words / entry.count); }); return { label: String(year), values, }; }); const average = totalArticles > 0 ? Math.round(totalWords / totalArticles) : 0; return { labels, series, average, articles: totalArticles }; } async function run({ contentDir, outputPath, publicPath }) { if (!outputPath) { throw new Error("outputPath manquant pour le graphique words_per_article"); } const articles = await loadArticles(contentDir || "content"); const { labels, series, average, articles: totalArticles } = groupAverageWordsByMonth(articles); await renderWithPython({ type: "words_per_article", outputPath, data: { labels, series, title: "Moyenne de mots par article (par mois)", }, }); return { image: publicPath, meta: { average, articles: totalArticles, }, }; } module.exports = { run };