feat: implement Stats endpoints for web dashboard

This commit is contained in:
2026-04-07 14:58:09 -04:00
parent b46934db46
commit 697929a127
3 changed files with 40 additions and 1 deletions

View File

@@ -123,3 +123,14 @@ async def get_logs(
"offset": offset, "offset": offset,
"data": logs "data": logs
} }
class StatsResponse(BaseModel):
total_logs: int
unique_attackers: int
active_deckies: int
@app.get("/api/v1/stats", response_model=StatsResponse)
async def get_stats(current_user: str = Depends(get_current_user)) -> dict[str, Any]:
return await repo.get_stats_summary()

View File

@@ -95,9 +95,14 @@ class SQLiteRepository(BaseRepository):
row = await cursor.fetchone() row = await cursor.fetchone()
unique_attackers: int = row["unique_attackers"] if row else 0 unique_attackers: int = row["unique_attackers"] if row else 0
async with db.execute("SELECT COUNT(DISTINCT decky) as active_deckies FROM logs") as cursor:
row = await cursor.fetchone()
active_deckies: int = row["active_deckies"] if row else 0
return { return {
"total_logs": total_logs, "total_logs": total_logs,
"unique_attackers": unique_attackers "unique_attackers": unique_attackers,
"active_deckies": active_deckies
} }
async def get_user_by_username(self, username: str) -> Optional[dict[str, Any]]: async def get_user_by_username(self, username: str) -> Optional[dict[str, Any]]:

View File

@@ -72,3 +72,26 @@ def test_get_logs_success() -> None:
assert "data" in data assert "data" in data
assert data["total"] >= 0 assert data["total"] >= 0
assert isinstance(data["data"], list) assert isinstance(data["data"], list)
def test_get_stats_unauthorized() -> None:
with TestClient(app) as client:
response = client.get("/api/v1/stats")
assert response.status_code == 401
def test_get_stats_success() -> None:
with TestClient(app) as client:
login_response = client.post(
"/api/v1/auth/login",
json={"username": "admin", "password": "admin"}
)
token = login_response.json()["access_token"]
response = client.get(
"/api/v1/stats",
headers={"Authorization": f"Bearer {token}"}
)
assert response.status_code == 200
data = response.json()
assert "total_logs" in data
assert "unique_attackers" in data
assert "active_deckies" in data