fixes and improvements
This commit is contained in:
@@ -125,6 +125,22 @@ class SettingsStore:
|
||||
conn.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_saved_task_runs_task_time ON saved_task_runs(task_id, created_at DESC)"
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS dashboard_shortcuts (
|
||||
id TEXT PRIMARY KEY,
|
||||
label TEXT NOT NULL,
|
||||
shortcut_type TEXT NOT NULL,
|
||||
target_json TEXT NOT NULL,
|
||||
enabled INTEGER NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
)
|
||||
"""
|
||||
)
|
||||
conn.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_dashboard_shortcuts_type ON dashboard_shortcuts(shortcut_type)"
|
||||
)
|
||||
conn.execute(
|
||||
"CREATE INDEX IF NOT EXISTS idx_monitoring_machine_actions_machine_time ON monitoring_machine_actions(machine_id, created_at DESC)"
|
||||
)
|
||||
@@ -628,43 +644,100 @@ class SettingsStore:
|
||||
).fetchall()
|
||||
return [dict(row) for row in rows]
|
||||
|
||||
def record_task_run(
|
||||
self,
|
||||
task: dict[str, Any],
|
||||
status: str,
|
||||
*,
|
||||
machine_id: str,
|
||||
machine_name: str,
|
||||
task_type: str,
|
||||
duration_ms: int,
|
||||
request_id: str = "",
|
||||
stdout_tail: str = "",
|
||||
stderr_tail: str = "",
|
||||
error: str = "",
|
||||
) -> None:
|
||||
def _row_to_shortcut(self, row: sqlite3.Row) -> dict[str, Any]:
|
||||
target = json.loads(row["target_json"] or "{}")
|
||||
return {
|
||||
"id": row["id"],
|
||||
"label": row["label"],
|
||||
"shortcut_type": row["shortcut_type"],
|
||||
"enabled": bool(row["enabled"]),
|
||||
"icon": target.get("icon", ""),
|
||||
"url": target.get("url", ""),
|
||||
"task_id": target.get("task_id", ""),
|
||||
"machine_id": target.get("machine_id", ""),
|
||||
"user_id": target.get("user_id", ""),
|
||||
"notes": target.get("notes", ""),
|
||||
"created_at": row["created_at"],
|
||||
"updated_at": row["updated_at"],
|
||||
}
|
||||
|
||||
def _normalize_shortcut_payload(self, payload: dict[str, Any], shortcut_id: str | None = None) -> dict[str, Any]:
|
||||
current = self.get_shortcut(shortcut_id) if shortcut_id else None
|
||||
shortcut_id = str(payload.get("id") or shortcut_id or uuid.uuid4().hex[:12]).strip() or uuid.uuid4().hex[:12]
|
||||
shortcut_type = str(payload.get("shortcut_type") or (current or {}).get("shortcut_type") or "website").strip().lower()
|
||||
if shortcut_type not in {"website", "action", "user"}:
|
||||
shortcut_type = "website"
|
||||
label = str(payload.get("label") or (current or {}).get("label") or "").strip() or shortcut_id
|
||||
enabled = bool(payload.get("enabled", (current or {}).get("enabled", True)))
|
||||
icon = str(payload.get("icon") if payload.get("icon") is not None else (current or {}).get("icon", "") or "").strip()
|
||||
url = str(payload.get("url") if payload.get("url") is not None else (current or {}).get("url", "") or "").strip()
|
||||
task_id = str(payload.get("task_id") if payload.get("task_id") is not None else (current or {}).get("task_id", "") or "").strip()
|
||||
machine_id = str(payload.get("machine_id") if payload.get("machine_id") is not None else (current or {}).get("machine_id", "") or "").strip()
|
||||
user_id = str(payload.get("user_id") if payload.get("user_id") is not None else (current or {}).get("user_id", "") or "").strip()
|
||||
notes = str(payload.get("notes") if payload.get("notes") is not None else (current or {}).get("notes", "") or "").strip()
|
||||
return {
|
||||
"id": shortcut_id,
|
||||
"label": label,
|
||||
"shortcut_type": shortcut_type,
|
||||
"enabled": enabled,
|
||||
"target": {
|
||||
"icon": icon,
|
||||
"url": url,
|
||||
"task_id": task_id,
|
||||
"machine_id": machine_id,
|
||||
"user_id": user_id,
|
||||
"notes": notes,
|
||||
},
|
||||
}
|
||||
|
||||
def list_shortcuts(self) -> list[dict[str, Any]]:
|
||||
self.init_schema()
|
||||
with self.connect() as conn:
|
||||
rows = conn.execute("SELECT * FROM dashboard_shortcuts ORDER BY label COLLATE NOCASE").fetchall()
|
||||
return [self._row_to_shortcut(row) for row in rows]
|
||||
|
||||
def get_shortcut(self, shortcut_id: str | None) -> dict[str, Any] | None:
|
||||
if not shortcut_id:
|
||||
return None
|
||||
self.init_schema()
|
||||
with self.connect() as conn:
|
||||
row = conn.execute("SELECT * FROM dashboard_shortcuts WHERE id = ?", (shortcut_id,)).fetchone()
|
||||
return self._row_to_shortcut(row) if row else None
|
||||
|
||||
def upsert_shortcut(self, payload: dict[str, Any], shortcut_id: str | None = None) -> dict[str, Any]:
|
||||
self.init_schema()
|
||||
shortcut = self._normalize_shortcut_payload(payload, shortcut_id)
|
||||
now = int(time.time())
|
||||
with self.connect() as conn:
|
||||
existing = conn.execute("SELECT created_at FROM dashboard_shortcuts WHERE id = ?", (shortcut["id"],)).fetchone()
|
||||
created_at = int(existing[0]) if existing else now
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO saved_task_runs (id, task_id, task_name, machine_id, machine_name, task_type, status, created_at, duration_ms, request_id, stdout_tail, stderr_tail, error)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO dashboard_shortcuts (id, label, shortcut_type, target_json, enabled, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
label = excluded.label,
|
||||
shortcut_type = excluded.shortcut_type,
|
||||
target_json = excluded.target_json,
|
||||
enabled = excluded.enabled,
|
||||
updated_at = excluded.updated_at
|
||||
""",
|
||||
(
|
||||
uuid.uuid4().hex,
|
||||
str(task.get("id") or ""),
|
||||
str(task.get("name") or ""),
|
||||
machine_id,
|
||||
machine_name,
|
||||
task_type,
|
||||
status,
|
||||
int(time.time()),
|
||||
duration_ms,
|
||||
request_id,
|
||||
stdout_tail,
|
||||
stderr_tail,
|
||||
error,
|
||||
shortcut["id"],
|
||||
shortcut["label"],
|
||||
shortcut["shortcut_type"],
|
||||
json.dumps(shortcut["target"]),
|
||||
1 if shortcut["enabled"] else 0,
|
||||
created_at,
|
||||
now,
|
||||
),
|
||||
)
|
||||
return self.get_shortcut(shortcut["id"]) or shortcut
|
||||
|
||||
def delete_shortcut(self, shortcut_id: str) -> None:
|
||||
self.init_schema()
|
||||
with self.connect() as conn:
|
||||
conn.execute("DELETE FROM dashboard_shortcuts WHERE id = ?", (shortcut_id,))
|
||||
|
||||
|
||||
_store: SettingsStore | None = None
|
||||
|
||||
Reference in New Issue
Block a user