Spaces:
Sleeping
Sleeping
Update backend.py
Browse files- backend.py +30 -0
backend.py
CHANGED
|
@@ -301,6 +301,36 @@ def _log_routes():
|
|
| 301 |
if isinstance(r, APIRoute):
|
| 302 |
print(" ", r.path, r.methods)
|
| 303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
|
| 305 |
|
| 306 |
def main():
|
|
|
|
| 301 |
if isinstance(r, APIRoute):
|
| 302 |
print(" ", r.path, r.methods)
|
| 303 |
|
| 304 |
+
@app.get("/UserResults/")
|
| 305 |
+
def get_user_results(user_id: str):
|
| 306 |
+
cursor = conn.cursor()
|
| 307 |
+
cursor.execute(
|
| 308 |
+
"SELECT ocr_text, anomalies, created_at FROM reports WHERE user_id = ? ORDER BY created_at ASC",
|
| 309 |
+
(user_id,)
|
| 310 |
+
)
|
| 311 |
+
rows = cursor.fetchall()
|
| 312 |
+
|
| 313 |
+
# On-the-fly "latest anomaly per measurement"
|
| 314 |
+
latest_anomalies = {} # measurement -> anomaly dict
|
| 315 |
+
combined_reports = [] # optional: keep full report data if needed
|
| 316 |
+
|
| 317 |
+
for ocr_text, anomalies_json, created_at in rows:
|
| 318 |
+
anomalies = json.loads(anomalies_json or "[]")
|
| 319 |
+
for a in anomalies:
|
| 320 |
+
meas = a.get("measurement")
|
| 321 |
+
# Always keep the latest anomaly (rows are ordered oldest → newest)
|
| 322 |
+
latest_anomalies[meas] = a
|
| 323 |
+
combined_reports.append({
|
| 324 |
+
"ocr_text": ocr_text,
|
| 325 |
+
"created_at": created_at,
|
| 326 |
+
"anomalies": anomalies
|
| 327 |
+
})
|
| 328 |
+
|
| 329 |
+
return {
|
| 330 |
+
"user_id": user_id,
|
| 331 |
+
"latest_anomalies": list(latest_anomalies.values()),
|
| 332 |
+
"all_reports": combined_reports
|
| 333 |
+
}
|
| 334 |
|
| 335 |
|
| 336 |
def main():
|