Spaces:
Sleeping
Sleeping
Commit ·
066f51c
1
Parent(s): 3e05e9e
Update store.py
Browse files- backend/app/data/store.py +28 -12
backend/app/data/store.py
CHANGED
|
@@ -1,13 +1,18 @@
|
|
| 1 |
-
# same content as your current store.py, just moved here
|
| 2 |
from __future__ import annotations
|
| 3 |
import json, sqlite3
|
| 4 |
from datetime import datetime, timezone, timedelta
|
| 5 |
from typing import Dict, Any, List, Optional
|
| 6 |
from pathlib import Path
|
| 7 |
-
from ..data.geo import haversine_km
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
_CONN.execute("""
|
| 12 |
CREATE TABLE IF NOT EXISTS reports (
|
| 13 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
@@ -24,18 +29,29 @@ def _row_to_feature(row: tuple) -> Dict[str, Any]:
|
|
| 24 |
_id, lat, lon, text, props_json, created_at = row
|
| 25 |
props = {"type": "user_report", "text": text, "reported_at": created_at}
|
| 26 |
if props_json:
|
| 27 |
-
try:
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def add_report(lat: float, lon: float, text: str = "User report", props: dict | None = None):
|
| 32 |
created_at = datetime.now(timezone.utc).isoformat()
|
| 33 |
props_json = json.dumps(props or {})
|
| 34 |
-
_CONN.execute(
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
_CONN.commit()
|
| 37 |
-
return {
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
def get_feature_collection() -> Dict[str, Any]:
|
| 41 |
cur = _CONN.execute("SELECT id, lat, lon, text, props_json, created_at FROM reports ORDER BY id DESC")
|
|
@@ -66,4 +82,4 @@ def find_reports_near(lat: float, lon: float, radius_km: float = 10.0, limit: in
|
|
| 66 |
def clear_reports() -> dict[str, any]:
|
| 67 |
_CONN.execute("DELETE FROM reports")
|
| 68 |
_CONN.commit()
|
| 69 |
-
return {"ok": True, "message": "All reports cleared."}
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
import json, sqlite3
|
| 3 |
from datetime import datetime, timezone, timedelta
|
| 4 |
from typing import Dict, Any, List, Optional
|
| 5 |
from pathlib import Path
|
|
|
|
| 6 |
|
| 7 |
+
from ..config.settings import settings
|
| 8 |
+
from .geo import haversine_km
|
| 9 |
+
|
| 10 |
+
# Use writable, absolute paths from settings (prefers /data in containers)
|
| 11 |
+
DB_PATH: Path = settings.REPORTS_DB
|
| 12 |
+
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 13 |
+
|
| 14 |
+
# Single connection
|
| 15 |
+
_CONN = sqlite3.connect(str(DB_PATH), check_same_thread=False)
|
| 16 |
_CONN.execute("""
|
| 17 |
CREATE TABLE IF NOT EXISTS reports (
|
| 18 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
| 29 |
_id, lat, lon, text, props_json, created_at = row
|
| 30 |
props = {"type": "user_report", "text": text, "reported_at": created_at}
|
| 31 |
if props_json:
|
| 32 |
+
try:
|
| 33 |
+
props.update(json.loads(props_json))
|
| 34 |
+
except Exception:
|
| 35 |
+
props["raw_props"] = props_json
|
| 36 |
+
return {
|
| 37 |
+
"type": "Feature",
|
| 38 |
+
"geometry": {"type": "Point", "coordinates": [lon, lat]},
|
| 39 |
+
"properties": props,
|
| 40 |
+
}
|
| 41 |
|
| 42 |
def add_report(lat: float, lon: float, text: str = "User report", props: dict | None = None):
|
| 43 |
created_at = datetime.now(timezone.utc).isoformat()
|
| 44 |
props_json = json.dumps(props or {})
|
| 45 |
+
_CONN.execute(
|
| 46 |
+
"INSERT INTO reports (lat, lon, text, props_json, created_at) VALUES (?,?,?,?,?)",
|
| 47 |
+
(float(lat), float(lon), text, props_json, created_at),
|
| 48 |
+
)
|
| 49 |
_CONN.commit()
|
| 50 |
+
return {
|
| 51 |
+
"type": "Feature",
|
| 52 |
+
"geometry": {"type": "Point", "coordinates": [float(lon), float(lat)]},
|
| 53 |
+
"properties": {"type": "user_report", "text": text, "reported_at": created_at, **(props or {})},
|
| 54 |
+
}
|
| 55 |
|
| 56 |
def get_feature_collection() -> Dict[str, Any]:
|
| 57 |
cur = _CONN.execute("SELECT id, lat, lon, text, props_json, created_at FROM reports ORDER BY id DESC")
|
|
|
|
| 82 |
def clear_reports() -> dict[str, any]:
|
| 83 |
_CONN.execute("DELETE FROM reports")
|
| 84 |
_CONN.commit()
|
| 85 |
+
return {"ok": True, "message": "All reports cleared."}
|