Alex got his trails
This commit is contained in:
parent
0627dfb29a
commit
66ed711fec
5 changed files with 90 additions and 4 deletions
37
main.py
37
main.py
|
|
@ -151,6 +151,43 @@ def get_history(
|
|||
]
|
||||
return JSONResponse(content={"data": data})
|
||||
|
||||
# ------------------------ GET Trails ---------------------------------
|
||||
@app.get("/trails")
|
||||
@app.get("/trails/")
|
||||
def get_trails(
|
||||
seconds: int = Query(600, ge=0, description="Lookback window in seconds")
|
||||
):
|
||||
"""
|
||||
Return position snapshots (timestamp, character_name, ew, ns, z)
|
||||
for the past `seconds` seconds.
|
||||
"""
|
||||
# match the same string format as stored timestamps (via str(datetime))
|
||||
cutoff_dt = datetime.utcnow().replace(tzinfo=timezone.utc) - timedelta(seconds=seconds)
|
||||
cutoff = str(cutoff_dt)
|
||||
conn = sqlite3.connect(DB_FILE)
|
||||
conn.row_factory = sqlite3.Row
|
||||
rows = conn.execute(
|
||||
"""
|
||||
SELECT timestamp, character_name, ew, ns, z
|
||||
FROM telemetry_log
|
||||
WHERE timestamp >= ?
|
||||
ORDER BY character_name, timestamp
|
||||
""",
|
||||
(cutoff,)
|
||||
).fetchall()
|
||||
conn.close()
|
||||
trails = [
|
||||
{
|
||||
"timestamp": r["timestamp"],
|
||||
"character_name": r["character_name"],
|
||||
"ew": r["ew"],
|
||||
"ns": r["ns"],
|
||||
"z": r["z"],
|
||||
}
|
||||
for r in rows
|
||||
]
|
||||
return JSONResponse(content={"trails": trails})
|
||||
|
||||
|
||||
# -------------------- static frontend ---------------------------
|
||||
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue