feat: add password-protected calculation admin
This commit is contained in:
1 parent
0cb5889ce2
commit
3d58b4fc9b
10 files changed
+169
-4
No files matched your search
@@ -15,6 +15,7 @@ class CalculationStore:
|
||||
def __init__(self, database_url: str | None = None, sqlite_path: str | Path | None = None) -> None:
|
||||
self.database_url = database_url or ""
|
||||
self.sqlite_path = Path(sqlite_path) if sqlite_path else None
|
||||
fetch_limit = 5000
|
||||
if self.database_url.startswith(("postgres://", "postgresql://")):
|
||||
import psycopg
|
||||
self._psycopg = psycopg
|
||||
@@ -85,3 +86,34 @@ class CalculationStore:
|
||||
)
|
||||
return int(cursor.lastrowid)
|
||||
|
||||
def list_calculations(self, *, sort_by: str = "kitten_id", descending: bool = True,
|
||||
limit: int = 100) -> list[dict[str, Any]]:
|
||||
"""Return admin rows with safe, application-level sorting by kitten identity."""
|
||||
limit = max(1, min(int(limit), 500))
|
||||
fetch_limit = 5000
|
||||
if self.database_url.startswith(("postgres://", "postgresql://")):
|
||||
with self._psycopg.connect(self.database_url) as connection:
|
||||
rows = connection.execute(
|
||||
"""SELECT id, actor_id, source, mode, input_json, result_json, message, created_at
|
||||
FROM belki_calculations ORDER BY created_at DESC LIMIT %s""", (fetch_limit,)
|
||||
).fetchall()
|
||||
else:
|
||||
with sqlite3.connect(self.sqlite_path) as connection:
|
||||
rows = connection.execute(
|
||||
"""SELECT id, actor_id, source, mode, input_json, result_json, message, created_at
|
||||
FROM calculations ORDER BY created_at DESC LIMIT ?""", (fetch_limit,)
|
||||
).fetchall()
|
||||
records = []
|
||||
for row in rows:
|
||||
input_data = row[4] if isinstance(row[4], dict) else json.loads(row[4])
|
||||
result_data = row[5] if isinstance(row[5], dict) or row[5] is None else json.loads(row[5])
|
||||
records.append({
|
||||
"id": row[0], "actor_id": row[1], "source": row[2], "mode": row[3],
|
||||
"input": input_data, "result": result_data, "message": row[6],
|
||||
"created_at": row[7].isoformat() if hasattr(row[7], "isoformat") else row[7],
|
||||
})
|
||||
if sort_by not in {"kitten_id", "kitten_name"}:
|
||||
sort_by = "kitten_id"
|
||||
records.sort(key=lambda item: (item["input"].get(sort_by) is None, item["input"].get(sort_by, "")),
|
||||
reverse=descending)
|
||||
return records[:limit]
|
||||
Reference in new issue
Block a user