2026-07-05 10:45:22 +08:00
|
|
|
"""Operational log query service."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from database.db_helper import Database
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LogService:
|
|
|
|
|
def __init__(self, database: Database):
|
|
|
|
|
self.database = database
|
|
|
|
|
|
2026-07-05 15:28:28 +08:00
|
|
|
def generation_logs(
|
|
|
|
|
self,
|
|
|
|
|
limit: int = 50,
|
|
|
|
|
*,
|
|
|
|
|
status: str | None = None,
|
|
|
|
|
workflow_id: str | None = None,
|
|
|
|
|
profile_id: str | None = None,
|
|
|
|
|
node_id: str | None = None,
|
|
|
|
|
) -> list[dict[str, Any]]:
|
2026-07-05 10:45:22 +08:00
|
|
|
placeholder = self.database._placeholder
|
2026-07-05 15:28:28 +08:00
|
|
|
where: list[str] = []
|
|
|
|
|
params: list[Any] = []
|
|
|
|
|
for column, value in (
|
|
|
|
|
("status", status),
|
|
|
|
|
("workflow_id", workflow_id),
|
|
|
|
|
("profile_id", profile_id),
|
|
|
|
|
("node_id", node_id),
|
|
|
|
|
):
|
|
|
|
|
if value:
|
|
|
|
|
where.append(f"{column} = {placeholder}")
|
|
|
|
|
params.append(value)
|
|
|
|
|
where_sql = f"WHERE {' AND '.join(where)}" if where else ""
|
2026-07-05 10:45:22 +08:00
|
|
|
conn = self.database.connect()
|
|
|
|
|
try:
|
|
|
|
|
rows = conn.execute(
|
|
|
|
|
f"""
|
|
|
|
|
SELECT task_id, profile_id, workflow_id, node_id, status, progress, error_message, created_at, updated_at
|
|
|
|
|
FROM generation_tasks
|
2026-07-05 15:28:28 +08:00
|
|
|
{where_sql}
|
2026-07-05 10:45:22 +08:00
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT {placeholder}
|
|
|
|
|
""",
|
2026-07-05 15:28:28 +08:00
|
|
|
tuple(params + [_limit(limit)]),
|
2026-07-05 10:45:22 +08:00
|
|
|
).fetchall()
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
"task_id": row["task_id"],
|
|
|
|
|
"profile_id": row["profile_id"],
|
|
|
|
|
"workflow_id": row["workflow_id"],
|
|
|
|
|
"node_id": row["node_id"],
|
|
|
|
|
"status": row["status"],
|
|
|
|
|
"progress": int(row["progress"] or 0),
|
|
|
|
|
"error_message": row["error_message"],
|
|
|
|
|
"created_at": str(row["created_at"]),
|
|
|
|
|
"updated_at": str(row["updated_at"]),
|
|
|
|
|
}
|
|
|
|
|
for row in rows
|
|
|
|
|
]
|
|
|
|
|
|
2026-07-05 15:28:28 +08:00
|
|
|
def workflow_logs(
|
|
|
|
|
self,
|
|
|
|
|
workflow_id: str | None = None,
|
|
|
|
|
limit: int = 50,
|
|
|
|
|
*,
|
|
|
|
|
action: str | None = None,
|
|
|
|
|
node_id: str | None = None,
|
|
|
|
|
branch_name: str | None = None,
|
|
|
|
|
) -> list[dict[str, Any]]:
|
2026-07-05 10:45:22 +08:00
|
|
|
placeholder = self.database._placeholder
|
2026-07-05 15:28:28 +08:00
|
|
|
where: list[str] = []
|
2026-07-05 10:45:22 +08:00
|
|
|
params: list[Any] = []
|
|
|
|
|
if workflow_id:
|
2026-07-05 15:28:28 +08:00
|
|
|
where.append(f"workflow_id = {placeholder}")
|
2026-07-05 10:45:22 +08:00
|
|
|
params.append(workflow_id)
|
2026-07-05 15:28:28 +08:00
|
|
|
if action:
|
|
|
|
|
where.append(f"action = {placeholder}")
|
|
|
|
|
params.append(action)
|
|
|
|
|
if node_id:
|
|
|
|
|
where.append(f"node_id = {placeholder}")
|
|
|
|
|
params.append(node_id)
|
|
|
|
|
if branch_name:
|
|
|
|
|
where.append(f"branch_name = {placeholder}")
|
|
|
|
|
params.append(branch_name)
|
|
|
|
|
where_sql = f"WHERE {' AND '.join(where)}" if where else ""
|
2026-07-05 10:45:22 +08:00
|
|
|
conn = self.database.connect()
|
|
|
|
|
try:
|
|
|
|
|
rows = conn.execute(
|
|
|
|
|
f"""
|
|
|
|
|
SELECT history_id, profile_id, workflow_id, node_id, image_id, branch_name, action, title, created_at
|
|
|
|
|
FROM workflow_history
|
2026-07-05 15:28:28 +08:00
|
|
|
{where_sql}
|
2026-07-05 10:45:22 +08:00
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT {placeholder}
|
|
|
|
|
""",
|
|
|
|
|
tuple(params + [_limit(limit)]),
|
|
|
|
|
).fetchall()
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
"history_id": row["history_id"],
|
|
|
|
|
"profile_id": row["profile_id"],
|
|
|
|
|
"workflow_id": row["workflow_id"],
|
|
|
|
|
"node_id": row["node_id"],
|
|
|
|
|
"image_id": row["image_id"],
|
|
|
|
|
"branch_name": row["branch_name"],
|
|
|
|
|
"action": row["action"],
|
|
|
|
|
"title": row["title"],
|
|
|
|
|
"created_at": str(row["created_at"]),
|
|
|
|
|
}
|
|
|
|
|
for row in rows
|
|
|
|
|
]
|
|
|
|
|
|
2026-07-05 15:28:28 +08:00
|
|
|
def integration_logs(
|
|
|
|
|
self,
|
|
|
|
|
integration_name: str | None = None,
|
|
|
|
|
limit: int = 50,
|
|
|
|
|
*,
|
|
|
|
|
action: str | None = None,
|
|
|
|
|
status: str | None = None,
|
|
|
|
|
) -> list[dict[str, Any]]:
|
2026-07-05 10:45:22 +08:00
|
|
|
placeholder = self.database._placeholder
|
2026-07-05 15:28:28 +08:00
|
|
|
where: list[str] = []
|
2026-07-05 10:45:22 +08:00
|
|
|
params: list[Any] = []
|
|
|
|
|
if integration_name:
|
2026-07-05 15:28:28 +08:00
|
|
|
where.append(f"integration_name = {placeholder}")
|
2026-07-05 10:45:22 +08:00
|
|
|
params.append(integration_name)
|
2026-07-05 15:28:28 +08:00
|
|
|
if action:
|
|
|
|
|
where.append(f"action = {placeholder}")
|
|
|
|
|
params.append(action)
|
|
|
|
|
if status:
|
|
|
|
|
where.append(f"status = {placeholder}")
|
|
|
|
|
params.append(status)
|
|
|
|
|
where_sql = f"WHERE {' AND '.join(where)}" if where else ""
|
2026-07-05 10:45:22 +08:00
|
|
|
conn = self.database.connect()
|
|
|
|
|
try:
|
|
|
|
|
rows = conn.execute(
|
|
|
|
|
f"""
|
2026-07-05 15:28:28 +08:00
|
|
|
SELECT integration_log_id, integration_name, action, status,
|
|
|
|
|
request_json, response_json, error_message, created_at
|
2026-07-05 10:45:22 +08:00
|
|
|
FROM integration_logs
|
2026-07-05 15:28:28 +08:00
|
|
|
{where_sql}
|
2026-07-05 10:45:22 +08:00
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT {placeholder}
|
|
|
|
|
""",
|
|
|
|
|
tuple(params + [_limit(limit)]),
|
|
|
|
|
).fetchall()
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
"integration_log_id": row["integration_log_id"],
|
|
|
|
|
"integration_name": row["integration_name"],
|
|
|
|
|
"action": row["action"],
|
|
|
|
|
"status": row["status"],
|
2026-07-05 15:28:28 +08:00
|
|
|
"request": _json_loads(row["request_json"]) or {},
|
|
|
|
|
"response": _json_loads(row["response_json"]) or {},
|
2026-07-05 10:45:22 +08:00
|
|
|
"error_message": row["error_message"],
|
|
|
|
|
"created_at": str(row["created_at"]),
|
|
|
|
|
}
|
|
|
|
|
for row in rows
|
|
|
|
|
]
|
|
|
|
|
|
2026-07-05 15:28:28 +08:00
|
|
|
def error_logs(
|
|
|
|
|
self,
|
|
|
|
|
limit: int = 50,
|
|
|
|
|
*,
|
|
|
|
|
source: str | None = None,
|
|
|
|
|
status: str | None = None,
|
|
|
|
|
) -> list[dict[str, Any]]:
|
2026-07-05 10:45:22 +08:00
|
|
|
placeholder = self.database._placeholder
|
2026-07-05 15:28:28 +08:00
|
|
|
include_tasks = source in {None, "generation_task"}
|
|
|
|
|
include_integrations = source in {None, "integration"}
|
|
|
|
|
task_statuses = ("failed", "dead_letter")
|
|
|
|
|
task_status_filter = status if status in task_statuses else None
|
|
|
|
|
integration_status_filter = status if status else None
|
2026-07-05 10:45:22 +08:00
|
|
|
conn = self.database.connect()
|
|
|
|
|
try:
|
2026-07-05 15:28:28 +08:00
|
|
|
task_rows = []
|
|
|
|
|
integration_rows = []
|
|
|
|
|
if include_tasks:
|
|
|
|
|
task_status_sql = f"= {placeholder}" if task_status_filter else "IN ('failed', 'dead_letter')"
|
|
|
|
|
task_params: tuple[Any, ...] = (task_status_filter, _limit(limit)) if task_status_filter else (_limit(limit),)
|
|
|
|
|
task_rows = conn.execute(
|
|
|
|
|
f"""
|
|
|
|
|
SELECT task_id AS id, status, error_message, created_at
|
|
|
|
|
FROM generation_tasks
|
|
|
|
|
WHERE status {task_status_sql}
|
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT {placeholder}
|
|
|
|
|
""",
|
|
|
|
|
task_params,
|
|
|
|
|
).fetchall()
|
|
|
|
|
if include_integrations:
|
|
|
|
|
integration_where = f"status = {placeholder}" if integration_status_filter else "status != 'succeeded'"
|
|
|
|
|
integration_params: tuple[Any, ...] = (
|
|
|
|
|
(integration_status_filter, _limit(limit)) if integration_status_filter else (_limit(limit),)
|
|
|
|
|
)
|
|
|
|
|
integration_rows = conn.execute(
|
|
|
|
|
f"""
|
|
|
|
|
SELECT integration_log_id AS id, status, error_message, created_at
|
|
|
|
|
FROM integration_logs
|
|
|
|
|
WHERE {integration_where}
|
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT {placeholder}
|
|
|
|
|
""",
|
|
|
|
|
integration_params,
|
|
|
|
|
).fetchall()
|
2026-07-05 10:45:22 +08:00
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
errors = [
|
|
|
|
|
{
|
|
|
|
|
"source": "generation_task",
|
|
|
|
|
"id": row["id"],
|
|
|
|
|
"status": row["status"],
|
|
|
|
|
"error_message": row["error_message"],
|
|
|
|
|
"created_at": str(row["created_at"]),
|
|
|
|
|
}
|
|
|
|
|
for row in task_rows
|
|
|
|
|
]
|
|
|
|
|
errors.extend(
|
|
|
|
|
{
|
|
|
|
|
"source": "integration",
|
|
|
|
|
"id": row["id"],
|
|
|
|
|
"status": row["status"],
|
|
|
|
|
"error_message": row["error_message"],
|
|
|
|
|
"created_at": str(row["created_at"]),
|
|
|
|
|
}
|
|
|
|
|
for row in integration_rows
|
|
|
|
|
)
|
|
|
|
|
return sorted(errors, key=lambda item: item["created_at"], reverse=True)[: _limit(limit)]
|
|
|
|
|
|
2026-07-05 15:15:00 +08:00
|
|
|
def audit_logs(
|
|
|
|
|
self,
|
|
|
|
|
limit: int = 50,
|
|
|
|
|
*,
|
|
|
|
|
action: str | None = None,
|
|
|
|
|
target_type: str | None = None,
|
|
|
|
|
actor_id: str | None = None,
|
|
|
|
|
) -> list[dict[str, Any]]:
|
2026-07-05 10:45:22 +08:00
|
|
|
placeholder = self.database._placeholder
|
2026-07-05 15:15:00 +08:00
|
|
|
where: list[str] = []
|
|
|
|
|
params: list[Any] = []
|
|
|
|
|
if action:
|
|
|
|
|
where.append(f"action = {placeholder}")
|
|
|
|
|
params.append(action)
|
|
|
|
|
if target_type:
|
|
|
|
|
where.append(f"target_type = {placeholder}")
|
|
|
|
|
params.append(target_type)
|
|
|
|
|
if actor_id:
|
|
|
|
|
where.append(f"actor_id = {placeholder}")
|
|
|
|
|
params.append(actor_id)
|
|
|
|
|
where_sql = f"WHERE {' AND '.join(where)}" if where else ""
|
2026-07-05 10:45:22 +08:00
|
|
|
conn = self.database.connect()
|
|
|
|
|
try:
|
|
|
|
|
rows = conn.execute(
|
|
|
|
|
f"""
|
|
|
|
|
SELECT audit_id, request_id, actor_type, actor_id, action, target_type, target_id,
|
|
|
|
|
before_snapshot, after_snapshot, metadata_json, created_at
|
|
|
|
|
FROM audit_logs
|
2026-07-05 15:15:00 +08:00
|
|
|
{where_sql}
|
2026-07-05 10:45:22 +08:00
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT {placeholder}
|
|
|
|
|
""",
|
2026-07-05 15:15:00 +08:00
|
|
|
tuple(params + [_limit(limit)]),
|
2026-07-05 10:45:22 +08:00
|
|
|
).fetchall()
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
"audit_id": row["audit_id"],
|
|
|
|
|
"request_id": row["request_id"],
|
|
|
|
|
"actor_type": row["actor_type"],
|
|
|
|
|
"actor_id": row["actor_id"],
|
|
|
|
|
"action": row["action"],
|
|
|
|
|
"target_type": row["target_type"],
|
|
|
|
|
"target_id": row["target_id"],
|
|
|
|
|
"before_snapshot": _json_loads(row["before_snapshot"]),
|
|
|
|
|
"after_snapshot": _json_loads(row["after_snapshot"]),
|
|
|
|
|
"metadata": _json_loads(row["metadata_json"]),
|
|
|
|
|
"created_at": str(row["created_at"]),
|
|
|
|
|
}
|
|
|
|
|
for row in rows
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _limit(value: int) -> int:
|
|
|
|
|
return min(max(int(value or 50), 1), 200)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _json_loads(value: str | None) -> Any:
|
|
|
|
|
if not value:
|
|
|
|
|
return None
|
|
|
|
|
return json.loads(value)
|