operations

This commit is contained in:
JiriUhlir
2026-05-29 10:44:25 +02:00
parent 3804bb32c4
commit 6e5d9b3120
4 changed files with 323 additions and 1 deletions
+144
View File
@@ -0,0 +1,144 @@
from app.db.database import get_connection
from app.db.migrations import run_migrations
from app.db.workers import is_worker_online
def _table_columns(con, table_name: str) -> set[str]:
rows = con.execute(f"PRAGMA table_info({table_name})").fetchall()
return {row["name"] for row in rows}
def get_operations_summary():
run_migrations()
con = get_connection()
workers = con.execute(
"""
SELECT last_seen_at
FROM workers
"""
).fetchall()
workers_online = sum(1 for worker in workers if is_worker_online(worker["last_seen_at"]))
running_jobs = con.execute(
"""
SELECT COUNT(*) AS count
FROM jobs
WHERE status = 'running'
"""
).fetchone()["count"]
queued_jobs = con.execute(
"""
SELECT COUNT(*) AS count
FROM jobs
WHERE status = 'queued'
"""
).fetchone()["count"]
failed_jobs_24h = con.execute(
"""
SELECT COUNT(*) AS count
FROM jobs
WHERE status = 'failed'
AND created_at >= datetime('now', '-24 hours')
"""
).fetchone()["count"]
deployment_columns = _table_columns(con, "deployments")
deployments_today = 0
if "created_at" in deployment_columns:
deployments_today = con.execute(
"""
SELECT COUNT(*) AS count
FROM deployments
WHERE date(created_at) = date('now')
"""
).fetchone()["count"]
elif "started_at" in deployment_columns:
deployments_today = con.execute(
"""
SELECT COUNT(*) AS count
FROM deployments
WHERE date(started_at) = date('now')
"""
).fetchone()["count"]
app_columns = _table_columns(con, "apps")
if "enabled" in app_columns:
active_applications = con.execute(
"""
SELECT COUNT(*) AS count
FROM apps
WHERE enabled = 1
OR LOWER(CAST(enabled AS TEXT)) IN ('1', 'true', 'yes', 'enabled')
"""
).fetchone()["count"]
else:
active_applications = con.execute(
"""
SELECT COUNT(*) AS count
FROM apps
WHERE LOWER(COALESCE(status, '')) NOT IN ('disabled', 'deleted', 'archived')
"""
).fetchone()["count"]
con.close()
return {
"workers_online": workers_online,
"running_jobs": running_jobs,
"queued_jobs": queued_jobs,
"failed_jobs_24h": failed_jobs_24h,
"deployments_today": deployments_today,
"active_applications": active_applications,
}
def get_recent_jobs(limit: int = 20):
run_migrations()
con = get_connection()
rows = con.execute(
"""
SELECT id, type, target_type, target_id, status, source, created_at
FROM jobs
ORDER BY id DESC
LIMIT ?
""",
(limit,),
).fetchall()
con.close()
return [dict(row) for row in rows]
def get_recent_deployments(limit: int = 20):
con = get_connection()
rows = con.execute(
"""
SELECT id, app_id, kind, status, started_at, finished_at, trigger_source
FROM deployments
ORDER BY id DESC
LIMIT ?
""",
(limit,),
).fetchall()
con.close()
return [dict(row) for row in rows]
def get_recent_audit_events(limit: int = 20):
con = get_connection()
rows = con.execute(
"""
SELECT id, username, action, target_type, target_id, source, created_at
FROM audit_events
ORDER BY id DESC
LIMIT ?
""",
(limit,),
).fetchall()
con.close()
return [dict(row) for row in rows]