astrbot_plugin_imagestudio/backend/services/log_service.py

203 行
7 KiB
Python

"""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
def generation_logs(self, limit: int = 50) -> list[dict[str, Any]]:
placeholder = self.database._placeholder
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
ORDER BY created_at DESC
LIMIT {placeholder}
""",
(_limit(limit),),
).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
]
def workflow_logs(self, workflow_id: str | None = None, limit: int = 50) -> list[dict[str, Any]]:
placeholder = self.database._placeholder
where = ""
params: list[Any] = []
if workflow_id:
where = f"WHERE workflow_id = {placeholder}"
params.append(workflow_id)
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
{where}
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
]
def integration_logs(self, integration_name: str | None = None, limit: int = 50) -> list[dict[str, Any]]:
placeholder = self.database._placeholder
where = ""
params: list[Any] = []
if integration_name:
where = f"WHERE integration_name = {placeholder}"
params.append(integration_name)
conn = self.database.connect()
try:
rows = conn.execute(
f"""
SELECT integration_log_id, integration_name, action, status, error_message, created_at
FROM integration_logs
{where}
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"],
"error_message": row["error_message"],
"created_at": str(row["created_at"]),
}
for row in rows
]
def error_logs(self, limit: int = 50) -> list[dict[str, Any]]:
placeholder = self.database._placeholder
conn = self.database.connect()
try:
task_rows = conn.execute(
f"""
SELECT task_id AS id, status, error_message, created_at
FROM generation_tasks
WHERE status IN ('failed', 'dead_letter')
ORDER BY created_at DESC
LIMIT {placeholder}
""",
(_limit(limit),),
).fetchall()
integration_rows = conn.execute(
f"""
SELECT integration_log_id AS id, status, error_message, created_at
FROM integration_logs
WHERE status != 'succeeded'
ORDER BY created_at DESC
LIMIT {placeholder}
""",
(_limit(limit),),
).fetchall()
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)]
def audit_logs(self, limit: int = 50) -> list[dict[str, Any]]:
placeholder = self.database._placeholder
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
ORDER BY created_at DESC
LIMIT {placeholder}
""",
(_limit(limit),),
).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)