feat: add log and audit query api
这个提交存在于:
父节点
935cd1b3d4
当前提交
237b07e324
共有 3 个文件被更改,包括 408 次插入 和 0 次删除
78
backend/routes/log_routes.py
普通文件
78
backend/routes/log_routes.py
普通文件
|
|
@ -0,0 +1,78 @@
|
||||||
|
"""Log route handlers."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from backend.routes.common import handle_request
|
||||||
|
from backend.services.log_service import LogService
|
||||||
|
from core.identity import RequestContext
|
||||||
|
from core.security import require_authenticated, require_config_manager
|
||||||
|
from database.db_helper import Database, load_database_settings
|
||||||
|
|
||||||
|
|
||||||
|
def get_generation_logs(ctx: RequestContext, query: dict[str, Any] | None = None, service: LogService | None = None) -> dict:
|
||||||
|
return _read_logs(ctx, query, service, lambda log_service, limit: log_service.generation_logs(limit))
|
||||||
|
|
||||||
|
|
||||||
|
def get_workflow_logs(ctx: RequestContext, query: dict[str, Any] | None = None, service: LogService | None = None) -> dict:
|
||||||
|
params = query or {}
|
||||||
|
|
||||||
|
def action(log_service: LogService, limit: int) -> list[dict[str, Any]]:
|
||||||
|
return log_service.workflow_logs(_optional_str(params.get("workflow_id")), limit)
|
||||||
|
|
||||||
|
return _read_logs(ctx, query, service, action)
|
||||||
|
|
||||||
|
|
||||||
|
def get_error_logs(ctx: RequestContext, query: dict[str, Any] | None = None, service: LogService | None = None) -> dict:
|
||||||
|
return _read_logs(ctx, query, service, lambda log_service, limit: log_service.error_logs(limit))
|
||||||
|
|
||||||
|
|
||||||
|
def get_integration_logs(ctx: RequestContext, query: dict[str, Any] | None = None, service: LogService | None = None) -> dict:
|
||||||
|
params = query or {}
|
||||||
|
|
||||||
|
def action(log_service: LogService, limit: int) -> list[dict[str, Any]]:
|
||||||
|
return log_service.integration_logs(_optional_str(params.get("integration_name")), limit)
|
||||||
|
|
||||||
|
return _read_logs(ctx, query, service, action)
|
||||||
|
|
||||||
|
|
||||||
|
def get_audit_logs(ctx: RequestContext, query: dict[str, Any] | None = None, service: LogService | None = None) -> dict:
|
||||||
|
log_service = service or LogService(Database(load_database_settings()))
|
||||||
|
params = query or {}
|
||||||
|
|
||||||
|
def action() -> list[dict[str, Any]]:
|
||||||
|
require_config_manager(ctx)
|
||||||
|
return log_service.audit_logs(_int_or_default(params.get("limit"), 50))
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def _read_logs(
|
||||||
|
ctx: RequestContext,
|
||||||
|
query: dict[str, Any] | None,
|
||||||
|
service: LogService | None,
|
||||||
|
reader,
|
||||||
|
) -> dict:
|
||||||
|
log_service = service or LogService(Database(load_database_settings()))
|
||||||
|
params = query or {}
|
||||||
|
|
||||||
|
def action() -> list[dict[str, Any]]:
|
||||||
|
require_authenticated(ctx)
|
||||||
|
return reader(log_service, _int_or_default(params.get("limit"), 50))
|
||||||
|
|
||||||
|
return handle_request(ctx, action)
|
||||||
|
|
||||||
|
|
||||||
|
def _optional_str(value: Any) -> str | None:
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
text = str(value).strip()
|
||||||
|
return text or None
|
||||||
|
|
||||||
|
|
||||||
|
def _int_or_default(value: Any, default: int) -> int:
|
||||||
|
try:
|
||||||
|
return int(value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return default
|
||||||
203
backend/services/log_service.py
普通文件
203
backend/services/log_service.py
普通文件
|
|
@ -0,0 +1,203 @@
|
||||||
|
"""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)
|
||||||
127
tests/test_log_routes.py
普通文件
127
tests/test_log_routes.py
普通文件
|
|
@ -0,0 +1,127 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from backend.routes.log_routes import (
|
||||||
|
get_audit_logs,
|
||||||
|
get_error_logs,
|
||||||
|
get_generation_logs,
|
||||||
|
get_integration_logs,
|
||||||
|
get_workflow_logs,
|
||||||
|
)
|
||||||
|
from backend.services.history_service import HistoryService
|
||||||
|
from backend.services.image_service import ImageService
|
||||||
|
from backend.services.integration_service import IntegrationService
|
||||||
|
from backend.services.log_service import LogService
|
||||||
|
from backend.services.material_service import MaterialService
|
||||||
|
from backend.services.profile_service import ProfileService
|
||||||
|
from backend.services.queue_service import QueueService
|
||||||
|
from backend.services.wardrobe_service import WardrobeService
|
||||||
|
from backend.services.workflow_service import WorkflowService
|
||||||
|
from core.audit_service import AuditService
|
||||||
|
from core.identity import RequestContext
|
||||||
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||||
|
from database.repositories.image_repository import ImageRepository
|
||||||
|
from database.repositories.integration_repository import IntegrationRepository
|
||||||
|
from database.repositories.job_repository import JobRepository
|
||||||
|
from database.repositories.material_repository import MaterialRepository
|
||||||
|
from database.repositories.profile_repository import ProfileRepository
|
||||||
|
from database.repositories.wardrobe_repository import WardrobeRepository
|
||||||
|
from database.repositories.workflow_repository import WorkflowRepository
|
||||||
|
|
||||||
|
|
||||||
|
class LogRoutesTests(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.tmp = tempfile.TemporaryDirectory()
|
||||||
|
self.database = Database(
|
||||||
|
DatabaseSettings(sqlite=SqliteSettings(path=str(Path(self.tmp.name) / "imagestudio.db")))
|
||||||
|
)
|
||||||
|
self.database.initialize()
|
||||||
|
self.audit_service = AuditService(self.database)
|
||||||
|
self.profile_repository = ProfileRepository(self.database)
|
||||||
|
self.workflow_repository = WorkflowRepository(self.database)
|
||||||
|
self.image_repository = ImageRepository(self.database)
|
||||||
|
self.material_repository = MaterialRepository(self.database)
|
||||||
|
self.wardrobe_repository = WardrobeRepository(self.database)
|
||||||
|
self.integration_repository = IntegrationRepository(self.database)
|
||||||
|
self.profile_service = ProfileService(self.profile_repository, self.audit_service)
|
||||||
|
self.workflow_service = WorkflowService(
|
||||||
|
self.workflow_repository,
|
||||||
|
self.profile_repository,
|
||||||
|
self.audit_service,
|
||||||
|
)
|
||||||
|
self.image_service = ImageService(self.image_repository, self.workflow_repository, self.audit_service)
|
||||||
|
self.queue_service = QueueService(JobRepository(self.database), self.audit_service)
|
||||||
|
self.history_service = HistoryService(
|
||||||
|
self.workflow_repository,
|
||||||
|
self.workflow_service,
|
||||||
|
self.image_service,
|
||||||
|
self.audit_service,
|
||||||
|
)
|
||||||
|
self.material_service = MaterialService(self.material_repository, self.audit_service)
|
||||||
|
self.wardrobe_service = WardrobeService(
|
||||||
|
self.wardrobe_repository,
|
||||||
|
self.profile_repository,
|
||||||
|
self.material_repository,
|
||||||
|
self.audit_service,
|
||||||
|
)
|
||||||
|
self.integration_service = IntegrationService(self.wardrobe_service, self.integration_repository)
|
||||||
|
self.log_service = LogService(self.database)
|
||||||
|
self.ctx = RequestContext.from_astrbot(
|
||||||
|
username="admin",
|
||||||
|
user_id="user_001",
|
||||||
|
permissions={"can_write", "can_manage_config", "can_manage_materials"},
|
||||||
|
)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.tmp.cleanup()
|
||||||
|
|
||||||
|
def test_log_routes_return_operational_views(self):
|
||||||
|
profile = self.profile_service.create_profile({"name": "Nova"}, self.ctx)
|
||||||
|
workflow = self.workflow_service.create_workflow(
|
||||||
|
profile.profile_id,
|
||||||
|
{"workflow_type": "single_image", "display_name": "Single"},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
node_id = workflow.nodes[0].node_id
|
||||||
|
image = self.image_service.create_image(
|
||||||
|
{"node_id": node_id, "image_uri": "storage://images/sample.png"},
|
||||||
|
self.ctx,
|
||||||
|
)
|
||||||
|
self.history_service.adopt_node_image(node_id, {"image_id": image.image_id}, self.ctx)
|
||||||
|
self.queue_service.create_task({"status": "failed", "error_message": "mock failure"}, self.ctx)
|
||||||
|
self.integration_service.daily_outfit({"profile_id": profile.profile_id, "categories": ["top"]}, self.ctx)
|
||||||
|
|
||||||
|
generation = get_generation_logs(self.ctx, {"limit": 5}, self.log_service)
|
||||||
|
self.assertTrue(generation["ok"])
|
||||||
|
self.assertEqual(generation["data"][0]["status"], "failed")
|
||||||
|
|
||||||
|
workflow_logs = get_workflow_logs(self.ctx, {"workflow_id": workflow.workflow.workflow_id}, self.log_service)
|
||||||
|
self.assertTrue(workflow_logs["ok"])
|
||||||
|
self.assertEqual(workflow_logs["data"][0]["action"], "adopt_image")
|
||||||
|
|
||||||
|
integrations = get_integration_logs(self.ctx, {}, self.log_service)
|
||||||
|
self.assertTrue(integrations["ok"])
|
||||||
|
self.assertEqual(integrations["data"][0]["action"], "daily_outfit")
|
||||||
|
|
||||||
|
errors = get_error_logs(self.ctx, {}, self.log_service)
|
||||||
|
self.assertTrue(errors["ok"])
|
||||||
|
self.assertEqual(errors["data"][0]["source"], "generation_task")
|
||||||
|
|
||||||
|
audit = get_audit_logs(self.ctx, {"limit": 10}, self.log_service)
|
||||||
|
self.assertTrue(audit["ok"])
|
||||||
|
self.assertTrue(any(item["action"] == "create_profile" for item in audit["data"]))
|
||||||
|
|
||||||
|
def test_audit_logs_require_config_permission(self):
|
||||||
|
read_only = RequestContext.from_astrbot(username="viewer", user_id="viewer_001")
|
||||||
|
|
||||||
|
response = get_audit_logs(read_only, {}, self.log_service)
|
||||||
|
|
||||||
|
self.assertFalse(response["ok"])
|
||||||
|
self.assertEqual(response["error_code"], "FORBIDDEN")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
正在加载…
添加表格
在新工单中引用