2026-07-05 10:03:11 +08:00
|
|
|
"""Repository for integration logs."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from database.db_helper import Database
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IntegrationRepository:
|
|
|
|
|
def __init__(self, database: Database):
|
|
|
|
|
self.database = database
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def placeholder(self) -> str:
|
|
|
|
|
return self.database._placeholder
|
|
|
|
|
|
|
|
|
|
def record(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|
|
|
|
placeholder = self.placeholder
|
|
|
|
|
conn = self.database.connect()
|
|
|
|
|
try:
|
|
|
|
|
conn.execute(
|
|
|
|
|
f"""
|
|
|
|
|
INSERT INTO integration_logs (
|
|
|
|
|
integration_log_id,
|
|
|
|
|
integration_name,
|
|
|
|
|
action,
|
|
|
|
|
status,
|
|
|
|
|
request_json,
|
|
|
|
|
response_json,
|
|
|
|
|
error_message
|
|
|
|
|
) VALUES (
|
|
|
|
|
{placeholder}, {placeholder}, {placeholder}, {placeholder},
|
|
|
|
|
{placeholder}, {placeholder}, {placeholder}
|
|
|
|
|
)
|
|
|
|
|
""",
|
|
|
|
|
(
|
|
|
|
|
payload["integration_log_id"],
|
|
|
|
|
payload["integration_name"],
|
|
|
|
|
payload["action"],
|
|
|
|
|
payload["status"],
|
|
|
|
|
_json_or_none(payload.get("request")),
|
|
|
|
|
_json_or_none(payload.get("response")),
|
|
|
|
|
payload.get("error_message"),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
conn.commit()
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
return payload
|
|
|
|
|
|
2026-07-05 15:28:28 +08:00
|
|
|
def list(
|
|
|
|
|
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:03:11 +08:00
|
|
|
placeholder = self.placeholder
|
2026-07-05 15:28:28 +08:00
|
|
|
where: list[str] = []
|
2026-07-05 10:03:11 +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:03:11 +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:03:11 +08:00
|
|
|
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
|
2026-07-05 15:28:28 +08:00
|
|
|
{where_sql}
|
2026-07-05 10:03:11 +08:00
|
|
|
ORDER BY created_at DESC
|
|
|
|
|
LIMIT {placeholder}
|
|
|
|
|
""",
|
2026-07-05 19:35:22 +08:00
|
|
|
tuple(params + [_limit(limit)]),
|
2026-07-05 10:03:11 +08:00
|
|
|
).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"]),
|
|
|
|
|
"response": _json_loads(row["response_json"]),
|
|
|
|
|
"error_message": row["error_message"],
|
|
|
|
|
"created_at": str(row["created_at"]),
|
|
|
|
|
}
|
|
|
|
|
for row in rows
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _json_or_none(value: Any | None) -> str | None:
|
|
|
|
|
if value is None:
|
|
|
|
|
return None
|
|
|
|
|
return json.dumps(value, ensure_ascii=False, sort_keys=True)
|
|
|
|
|
|
|
|
|
|
|
2026-07-05 19:35:22 +08:00
|
|
|
def _limit(value: int) -> int:
|
|
|
|
|
try:
|
|
|
|
|
limit = int(value)
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
limit = 50
|
|
|
|
|
return min(max(limit, 1), 200)
|
|
|
|
|
|
|
|
|
|
|
2026-07-05 10:03:11 +08:00
|
|
|
def _json_loads(value: str | None) -> dict[str, Any]:
|
|
|
|
|
if not value:
|
|
|
|
|
return {}
|
|
|
|
|
return json.loads(value)
|