Add server-side web usage metrics with !petestats command

Track per-page/per-channel view counts and a privacy-preserving daily
unique-visitor estimate (salted IP+UA hash, salt rotated daily and never
persisted). No third-party analytics, no JS beacon. Surfaced via the
admin-gated !petestats Matrix command (named to avoid an existing !stats
bot in the rooms).
This commit is contained in:
prosolis
2026-06-22 01:54:34 -07:00
parent aaf6e551a0
commit e65ffb1373
8 changed files with 367 additions and 3 deletions

View File

@@ -9,6 +9,7 @@ import (
"io/fs"
"log/slog"
"net/http"
"sync"
"time"
"pete/internal/config"
@@ -56,6 +57,12 @@ type Server struct {
srv *http.Server
tpls map[string]*template.Template // keyed by page name (e.g. "index", "channel")
auth *Authenticator // nil when sign-in is disabled or unavailable
// Daily-rotated salt for the privacy-preserving unique-visitor estimate.
// Guarded by metricsMu; never persisted (see metrics.go).
metricsMu sync.Mutex
saltDay int64
salt [16]byte
}
// New builds the server. Templates are parsed once at startup. Each page