Added history

This commit is contained in:
erik 2025-04-30 21:28:20 +00:00
parent d63cb4ee06
commit 0627dfb29a

56
main.py
View file

@ -3,7 +3,7 @@ import json
import sqlite3
from typing import Dict
from fastapi import FastAPI, Header, HTTPException
from fastapi import FastAPI, Header, HTTPException, Query
from fastapi.responses import JSONResponse
from fastapi.routing import APIRoute
from fastapi.staticfiles import StaticFiles
@ -97,6 +97,60 @@ def get_live_players():
]
return JSONResponse(content={"players": players})
@app.get("/history/")
@app.get("/history")
def get_history(
from_ts: str | None = Query(None, alias="from"),
to_ts: str | None = Query(None, alias="to"),
):
"""
Returns a timeordered list of telemetry snapshots:
- timestamp: ISO8601 string
- character_name: str
- kills: cumulative kill count (int)
- kph: kills_per_hour (float)
"""
conn = sqlite3.connect(DB_FILE)
conn.row_factory = sqlite3.Row
# Build the base query
sql = """
SELECT
timestamp,
character_name,
kills,
CAST(kills_per_hour AS REAL) AS kph
FROM telemetry_log
"""
params: list[str] = []
conditions: list[str] = []
# Add optional filters
if from_ts:
conditions.append("timestamp >= ?")
params.append(from_ts)
if to_ts:
conditions.append("timestamp <= ?")
params.append(to_ts)
if conditions:
sql += " WHERE " + " AND ".join(conditions)
sql += " ORDER BY timestamp"
rows = conn.execute(sql, params).fetchall()
conn.close()
data = [
{
"timestamp": row["timestamp"],
"character_name":row["character_name"],
"kills": row["kills"],
"kph": row["kph"],
}
for row in rows
]
return JSONResponse(content={"data": data})
# -------------------- static frontend ---------------------------
app.mount("/", StaticFiles(directory="static", html=True), name="static")