Page de statistiques
This commit is contained in:
32
tools/stats/articles_avg_per_month.js
Normal file
32
tools/stats/articles_avg_per_month.js
Normal file
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const { loadArticles } = require("../lib/stats/articles");
|
||||
|
||||
function computeAveragePerMonth(articles) {
|
||||
let first = null;
|
||||
let last = null;
|
||||
|
||||
for (const article of articles) {
|
||||
if (!article.date) continue;
|
||||
if (!first || article.date < first) first = article.date;
|
||||
if (!last || article.date > last) last = article.date;
|
||||
}
|
||||
|
||||
if (!first || !last) {
|
||||
return { average: 0, months: 0 };
|
||||
}
|
||||
|
||||
const monthSpan = Math.max(1, Math.round(last.diff(first.startOf("month"), "months").months) + 1);
|
||||
const total = articles.filter((a) => a.date).length;
|
||||
const average = total / monthSpan;
|
||||
|
||||
return { average, months: monthSpan };
|
||||
}
|
||||
|
||||
async function run({ contentDir }) {
|
||||
const articles = await loadArticles(contentDir || "content");
|
||||
const { average, months } = computeAveragePerMonth(articles);
|
||||
return { value: average.toFixed(2), meta: { months } };
|
||||
}
|
||||
|
||||
module.exports = { run };
|
||||
Reference in New Issue
Block a user