Feature: Analytics 30-Tage Dual-Chart, Referrers, Umami-Credentials — SW by-v489, APP_VER 466

This commit is contained in:
rene 2026-04-29 11:06:18 +02:00
parent a4da7144d6
commit e22dcc3c3d
4 changed files with 154 additions and 90 deletions

View file

@ -1,4 +1,5 @@
"""BAN YARO — Admin / Moderator Backend"""
import asyncio
import os
import sys
import time
@ -707,35 +708,44 @@ async def get_analytics(user=Depends(require_mod)):
now = datetime.now(_TZ)
today_start = now.replace(hour=0, minute=0, second=0, microsecond=0)
week_start = today_start - timedelta(days=6)
month_start = today_start - timedelta(days=29)
now_ms = int(now.timestamp() * 1000)
today_ms = int(today_start.timestamp() * 1000)
week_ms = int(week_start.timestamp() * 1000)
month_ms = int(month_start.timestamp() * 1000)
async with httpx.AsyncClient(timeout=10) as c:
r_today = await c.get(f"{url}/api/websites/{site_id}/stats",
params={"startAt": today_ms, "endAt": now_ms}, headers=headers)
r_week = await c.get(f"{url}/api/websites/{site_id}/stats",
params={"startAt": week_ms, "endAt": now_ms}, headers=headers)
r_pv = await c.get(f"{url}/api/websites/{site_id}/pageviews",
params={"startAt": week_ms, "endAt": now_ms,
"unit": "day", "timezone": "Europe/Berlin"}, headers=headers)
r_pages = await c.get(f"{url}/api/websites/{site_id}/metrics",
params={"startAt": week_ms, "endAt": now_ms,
"type": "url", "limit": 8}, headers=headers)
async with httpx.AsyncClient(timeout=15) as c:
r_today, r_week, r_month, r_pv_month, r_pages, r_refs = await asyncio.gather(
c.get(f"{url}/api/websites/{site_id}/stats",
params={"startAt": today_ms, "endAt": now_ms}, headers=headers),
c.get(f"{url}/api/websites/{site_id}/stats",
params={"startAt": week_ms, "endAt": now_ms}, headers=headers),
c.get(f"{url}/api/websites/{site_id}/stats",
params={"startAt": month_ms, "endAt": now_ms}, headers=headers),
c.get(f"{url}/api/websites/{site_id}/pageviews",
params={"startAt": month_ms, "endAt": now_ms,
"unit": "day", "timezone": "Europe/Berlin"}, headers=headers),
c.get(f"{url}/api/websites/{site_id}/metrics",
params={"startAt": month_ms, "endAt": now_ms,
"type": "url", "limit": 10}, headers=headers),
c.get(f"{url}/api/websites/{site_id}/metrics",
params={"startAt": month_ms, "endAt": now_ms,
"type": "referrer", "limit": 8}, headers=headers),
)
def _to_list(r):
j = r.json()
if isinstance(j, list):
return j
if isinstance(j, dict):
return j.get("data", j.get("metrics", []))
if isinstance(j, list): return j
if isinstance(j, dict): return j.get("data", j.get("metrics", []))
return []
return {
"today": r_today.json(),
"week": r_week.json(),
"pageviews": r_pv.json(),
"top_pages": _to_list(r_pages),
"today": r_today.json(),
"week": r_week.json(),
"month": r_month.json(),
"pageviews": r_pv_month.json(),
"top_pages": _to_list(r_pages),
"referrers": _to_list(r_refs),
}