"""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, *, status: str | None = None, workflow_id: str | None = None, profile_id: str | None = None, node_id: str | None = None, ) -> list[dict[str, Any]]: placeholder = self.database._placeholder 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 "" 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 {where_sql} ORDER BY created_at DESC LIMIT {placeholder} """, tuple(params + [_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, *, action: str | None = None, node_id: str | None = None, branch_name: str | None = None, ) -> list[dict[str, Any]]: placeholder = self.database._placeholder where: list[str] = [] params: list[Any] = [] if workflow_id: where.append(f"workflow_id = {placeholder}") params.append(workflow_id) 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 "" 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_sql} 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, *, action: str | None = None, status: str | None = None, ) -> list[dict[str, Any]]: placeholder = self.database._placeholder where: list[str] = [] params: list[Any] = [] if integration_name: where.append(f"integration_name = {placeholder}") params.append(integration_name) 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 "" conn = self.database.connect() try: rows = conn.execute( f""" SELECT integration_log_id, integration_name, action, status, request_json, response_json, error_message, created_at FROM integration_logs {where_sql} 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"], "request": _json_loads(row["request_json"]) or {}, "response": _json_loads(row["response_json"]) or {}, "error_message": row["error_message"], "created_at": str(row["created_at"]), } for row in rows ] def error_logs( self, limit: int = 50, *, source: str | None = None, status: str | None = None, ) -> list[dict[str, Any]]: placeholder = self.database._placeholder 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 conn = self.database.connect() try: 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() 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, *, action: str | None = None, target_type: str | None = None, actor_id: str | None = None, ) -> list[dict[str, Any]]: placeholder = self.database._placeholder 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 "" 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 {where_sql} ORDER BY created_at DESC LIMIT {placeholder} """, tuple(params + [_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)