From 0627dfb29aab505ae4253b6710e9e6af2e45c150 Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 30 Apr 2025 21:28:20 +0000 Subject: [PATCH] Added history --- main.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index d8fa2ecc..d1995c4c 100644 --- a/main.py +++ b/main.py @@ -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 time‐ordered 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")