Add user stats and activity tracking
This commit is contained in:
@@ -1,15 +1,28 @@
|
||||
from typing import Dict, Any
|
||||
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi import Depends, HTTPException, status, Request
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
|
||||
from .db import get_user_by_username
|
||||
from .db import get_user_by_username, upsert_user_activity
|
||||
from .security import safe_decode_token, TokenError
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login")
|
||||
|
||||
def _extract_client_ip(request: Request) -> str:
|
||||
forwarded = request.headers.get("x-forwarded-for")
|
||||
if forwarded:
|
||||
parts = [part.strip() for part in forwarded.split(",") if part.strip()]
|
||||
if parts:
|
||||
return parts[0]
|
||||
real_ip = request.headers.get("x-real-ip")
|
||||
if real_ip:
|
||||
return real_ip.strip()
|
||||
if request.client and request.client.host:
|
||||
return request.client.host
|
||||
return "unknown"
|
||||
|
||||
def get_current_user(token: str = Depends(oauth2_scheme)) -> Dict[str, Any]:
|
||||
|
||||
def get_current_user(token: str = Depends(oauth2_scheme), request: Request = None) -> Dict[str, Any]:
|
||||
try:
|
||||
payload = safe_decode_token(token)
|
||||
except TokenError as exc:
|
||||
@@ -25,6 +38,11 @@ def get_current_user(token: str = Depends(oauth2_scheme)) -> Dict[str, Any]:
|
||||
if user.get("is_blocked"):
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="User is blocked")
|
||||
|
||||
if request is not None:
|
||||
ip = _extract_client_ip(request)
|
||||
user_agent = request.headers.get("user-agent", "unknown")
|
||||
upsert_user_activity(user["username"], ip, user_agent)
|
||||
|
||||
return {
|
||||
"username": user["username"],
|
||||
"role": user["role"],
|
||||
|
||||
@@ -103,6 +103,32 @@ def init_db() -> None:
|
||||
ON requests_cache (requested_by_norm)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS user_activity (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
username TEXT NOT NULL,
|
||||
ip TEXT NOT NULL,
|
||||
user_agent TEXT NOT NULL,
|
||||
first_seen_at TEXT NOT NULL,
|
||||
last_seen_at TEXT NOT NULL,
|
||||
hit_count INTEGER NOT NULL DEFAULT 1,
|
||||
UNIQUE(username, ip, user_agent)
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE INDEX IF NOT EXISTS idx_user_activity_username
|
||||
ON user_activity (username)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE INDEX IF NOT EXISTS idx_user_activity_last_seen
|
||||
ON user_activity (last_seen_at)
|
||||
"""
|
||||
)
|
||||
try:
|
||||
conn.execute("ALTER TABLE users ADD COLUMN last_login_at TEXT")
|
||||
except sqlite3.OperationalError:
|
||||
@@ -377,6 +403,164 @@ def _backfill_auth_providers() -> None:
|
||||
)
|
||||
|
||||
|
||||
def upsert_user_activity(username: str, ip: str, user_agent: str) -> None:
|
||||
if not username:
|
||||
return
|
||||
ip_value = ip.strip() if isinstance(ip, str) and ip.strip() else "unknown"
|
||||
agent_value = (
|
||||
user_agent.strip() if isinstance(user_agent, str) and user_agent.strip() else "unknown"
|
||||
)
|
||||
timestamp = datetime.now(timezone.utc).isoformat()
|
||||
with _connect() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO user_activity (username, ip, user_agent, first_seen_at, last_seen_at, hit_count)
|
||||
VALUES (?, ?, ?, ?, ?, 1)
|
||||
ON CONFLICT(username, ip, user_agent)
|
||||
DO UPDATE SET last_seen_at = excluded.last_seen_at, hit_count = hit_count + 1
|
||||
""",
|
||||
(username, ip_value, agent_value, timestamp, timestamp),
|
||||
)
|
||||
|
||||
|
||||
def get_user_activity(username: str, limit: int = 5) -> list[Dict[str, Any]]:
|
||||
limit = max(1, min(limit, 20))
|
||||
with _connect() as conn:
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT ip, user_agent, first_seen_at, last_seen_at, hit_count
|
||||
FROM user_activity
|
||||
WHERE username = ?
|
||||
ORDER BY last_seen_at DESC
|
||||
LIMIT ?
|
||||
""",
|
||||
(username, limit),
|
||||
).fetchall()
|
||||
results: list[Dict[str, Any]] = []
|
||||
for row in rows:
|
||||
results.append(
|
||||
{
|
||||
"ip": row[0],
|
||||
"user_agent": row[1],
|
||||
"first_seen_at": row[2],
|
||||
"last_seen_at": row[3],
|
||||
"hit_count": row[4],
|
||||
}
|
||||
)
|
||||
return results
|
||||
|
||||
|
||||
def get_user_activity_summary(username: str) -> Dict[str, Any]:
|
||||
with _connect() as conn:
|
||||
last_row = conn.execute(
|
||||
"""
|
||||
SELECT ip, user_agent, last_seen_at
|
||||
FROM user_activity
|
||||
WHERE username = ?
|
||||
ORDER BY last_seen_at DESC
|
||||
LIMIT 1
|
||||
""",
|
||||
(username,),
|
||||
).fetchone()
|
||||
count_row = conn.execute(
|
||||
"""
|
||||
SELECT COUNT(*)
|
||||
FROM user_activity
|
||||
WHERE username = ?
|
||||
""",
|
||||
(username,),
|
||||
).fetchone()
|
||||
return {
|
||||
"last_ip": last_row[0] if last_row else None,
|
||||
"last_user_agent": last_row[1] if last_row else None,
|
||||
"last_seen_at": last_row[2] if last_row else None,
|
||||
"device_count": int(count_row[0] or 0) if count_row else 0,
|
||||
}
|
||||
|
||||
|
||||
def get_user_request_stats(username_norm: str) -> Dict[str, Any]:
|
||||
if not username_norm:
|
||||
return {
|
||||
"total": 0,
|
||||
"ready": 0,
|
||||
"pending": 0,
|
||||
"approved": 0,
|
||||
"working": 0,
|
||||
"partial": 0,
|
||||
"declined": 0,
|
||||
"in_progress": 0,
|
||||
"last_request_at": None,
|
||||
}
|
||||
with _connect() as conn:
|
||||
total_row = conn.execute(
|
||||
"""
|
||||
SELECT COUNT(*)
|
||||
FROM requests_cache
|
||||
WHERE requested_by_norm = ?
|
||||
""",
|
||||
(username_norm,),
|
||||
).fetchone()
|
||||
status_rows = conn.execute(
|
||||
"""
|
||||
SELECT status, COUNT(*)
|
||||
FROM requests_cache
|
||||
WHERE requested_by_norm = ?
|
||||
GROUP BY status
|
||||
""",
|
||||
(username_norm,),
|
||||
).fetchall()
|
||||
last_row = conn.execute(
|
||||
"""
|
||||
SELECT MAX(created_at)
|
||||
FROM requests_cache
|
||||
WHERE requested_by_norm = ?
|
||||
""",
|
||||
(username_norm,),
|
||||
).fetchone()
|
||||
counts = {int(row[0]): int(row[1]) for row in status_rows if row[0] is not None}
|
||||
pending = counts.get(1, 0)
|
||||
approved = counts.get(2, 0)
|
||||
declined = counts.get(3, 0)
|
||||
ready = counts.get(4, 0)
|
||||
working = counts.get(5, 0)
|
||||
partial = counts.get(6, 0)
|
||||
in_progress = approved + working + partial
|
||||
return {
|
||||
"total": int(total_row[0] or 0) if total_row else 0,
|
||||
"ready": ready,
|
||||
"pending": pending,
|
||||
"approved": approved,
|
||||
"working": working,
|
||||
"partial": partial,
|
||||
"declined": declined,
|
||||
"in_progress": in_progress,
|
||||
"last_request_at": last_row[0] if last_row else None,
|
||||
}
|
||||
|
||||
|
||||
def get_global_request_leader() -> Optional[Dict[str, Any]]:
|
||||
with _connect() as conn:
|
||||
row = conn.execute(
|
||||
"""
|
||||
SELECT requested_by_norm, MAX(requested_by) as display_name, COUNT(*) as total
|
||||
FROM requests_cache
|
||||
WHERE requested_by_norm IS NOT NULL AND requested_by_norm != ''
|
||||
GROUP BY requested_by_norm
|
||||
ORDER BY total DESC
|
||||
LIMIT 1
|
||||
"""
|
||||
).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
return {"username": row[1] or row[0], "total": int(row[2] or 0)}
|
||||
|
||||
|
||||
def get_global_request_total() -> int:
|
||||
with _connect() as conn:
|
||||
row = conn.execute("SELECT COUNT(*) FROM requests_cache").fetchone()
|
||||
return int(row[0] or 0)
|
||||
|
||||
|
||||
def upsert_request_cache(
|
||||
request_id: int,
|
||||
media_id: Optional[int],
|
||||
|
||||
@@ -7,6 +7,11 @@ from ..db import (
|
||||
set_last_login,
|
||||
get_user_by_username,
|
||||
set_user_password,
|
||||
get_user_activity,
|
||||
get_user_activity_summary,
|
||||
get_user_request_stats,
|
||||
get_global_request_leader,
|
||||
get_global_request_total,
|
||||
)
|
||||
from ..runtime import get_runtime_settings
|
||||
from ..clients.jellyfin import JellyfinClient
|
||||
@@ -17,6 +22,10 @@ from ..auth import get_current_user
|
||||
router = APIRouter(prefix="/auth", tags=["auth"])
|
||||
|
||||
|
||||
def _normalize_username(value: str) -> str:
|
||||
return value.strip().lower()
|
||||
|
||||
|
||||
@router.post("/login")
|
||||
async def login(form_data: OAuth2PasswordRequestForm = Depends()) -> dict:
|
||||
user = verify_user_password(form_data.username, form_data.password)
|
||||
@@ -92,6 +101,31 @@ async def me(current_user: dict = Depends(get_current_user)) -> dict:
|
||||
return current_user
|
||||
|
||||
|
||||
@router.get("/profile")
|
||||
async def profile(current_user: dict = Depends(get_current_user)) -> dict:
|
||||
username = current_user.get("username") or ""
|
||||
username_norm = _normalize_username(username) if username else ""
|
||||
stats = get_user_request_stats(username_norm)
|
||||
global_total = get_global_request_total()
|
||||
leader = get_global_request_leader()
|
||||
share = (stats.get("total", 0) / global_total) if global_total else 0
|
||||
activity_summary = get_user_activity_summary(username) if username else {}
|
||||
activity_recent = get_user_activity(username, limit=5) if username else []
|
||||
return {
|
||||
"user": current_user,
|
||||
"stats": {
|
||||
**stats,
|
||||
"share": share,
|
||||
"global_total": global_total,
|
||||
"most_active_user": leader,
|
||||
},
|
||||
"activity": {
|
||||
**activity_summary,
|
||||
"recent": activity_recent,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@router.post("/password")
|
||||
async def change_password(payload: dict, current_user: dict = Depends(get_current_user)) -> dict:
|
||||
if current_user.get("auth_provider") != "local":
|
||||
|
||||
Reference in New Issue
Block a user