Page de statistiques
This commit is contained in:
42
tools/stats/goaccess_monthly.js
Normal file
42
tools/stats/goaccess_monthly.js
Normal file
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const { fetchGoAccessJson, aggregateLastNDays } = require("../lib/stats/goaccess");
|
||||
const { loadToolsConfig } = require("../lib/config");
|
||||
|
||||
let cache = null;
|
||||
|
||||
async function loadData(url) {
|
||||
if (!cache) {
|
||||
cache = await fetchGoAccessJson(url);
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
function latestMonthEntry(months) {
|
||||
if (!months || months.length === 0) return null;
|
||||
return months[months.length - 1];
|
||||
}
|
||||
|
||||
async function run({ stat }) {
|
||||
const toolsConfig = await loadToolsConfig();
|
||||
const url = stat.url || toolsConfig.goaccess?.url || "";
|
||||
const metric = stat.metric || "hits";
|
||||
const windowDays = Number.isFinite(stat.days) ? stat.days : 30;
|
||||
const data = await loadData(url);
|
||||
const window = aggregateLastNDays(data, windowDays, { adjustCrawlers: true });
|
||||
if (!window || !window.to) return { value: 0 };
|
||||
|
||||
const value = metric === "visitors" ? window.visitors : window.hits;
|
||||
return {
|
||||
value,
|
||||
meta: {
|
||||
from: window.from || null,
|
||||
to: window.to || null,
|
||||
days: windowDays,
|
||||
metric,
|
||||
raw: value,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { run };
|
||||
Reference in New Issue
Block a user