import { readFileSync } from "fs";
import express from "express";
import grimm from "../grimm.js";
const router = express.Router();
const count = (list, sort) => {
let counted = [...new Set(list)].map(si => ({
name: si,
count: list.filter(li => li === si).length
}));
return sort ? counted.sort((a, b) => b.count - a.count) : counted;
};
router.get("/", (req, res, next) => {
const uiLang = req.user.prefs.uiLang || "en";
let analytics = readFileSync("analytics/access.log", { encoding: "utf8" })
.split("\n")
.map(visit => visit.split(" "))
.map(visit => [...visit.slice(0, 3), visit.slice(3).join(" ")])
.filter(
visit =>
visit.length >= 3 &&
!visit[1].match(
/^\/garden(\/tagged|.*?\/feed|.*?\/page\/)|^https|post-comment|wp-admin|new\/feed\.xml/
) &&
!visit[3].match(/\+|https?\:/)
)
.map(visit => ({
time: visit[0],
page: visit[1].replace(/\?.*/, "").replace(/(.)\/$/, "$1"),
referrer:
visit[2].match(
/^-$|satyrs\.eu|marijn\.uk|satyrwoud|marijnflorence|vpn|binance/
) || !visit[2].match(/^http/)
? null
: visit[2]
.replace(/\?.*/, "")
.replace(/https?:\/\//, "")
.replace(/^www\./, "")
.replace(/^([^\/]*)\/$/, "$1")
}));
let stats = {
dailyViews: count(
analytics.map(v => v.time.slice(0, 10)),
false
),
popularPages: count(
analytics.map(v => v.page),
true
),
backlinks: count(
analytics.filter(v => v.referrer).map(v => v.referrer),
true
)
};
stats.maxDailyViews = Math.max(...stats.dailyViews.map(v => v.count));
return res.render("../admin/stats/stats", {
stats: stats,
grimm: grimm,
tr: grimm.translator(uiLang),
lang: uiLang
});
});
export default router;