43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
#!/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 };
|