feat(agent-office): /activity 통합 피드에 필터 추가 (agent_id/type/status/days)

오버사이트 UI용. get_activity_feed가 브랜치별 WHERE로 필터, total도 동일 반영. status는 task 전용(주면 log 제외). 값은 ? 바인딩, type은 브랜치 선택만이라 injection 안전. 신규 5 테스트.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-11 08:25:19 +09:00
parent c62e3e70b9
commit 2c2828c8f0
4 changed files with 128 additions and 25 deletions

View File

@@ -534,33 +534,58 @@ def get_conversation_stats(days: int = 7) -> Dict[str, Any]:
}
def get_activity_feed(limit: int = 50, offset: int = 0) -> dict:
with _conn() as conn:
total_row = conn.execute("""
SELECT (SELECT COUNT(*) FROM agent_tasks) + (SELECT COUNT(*) FROM agent_logs) AS total
""").fetchone()
total = total_row["total"] if total_row else 0
def get_activity_feed(limit: int = 50, offset: int = 0, agent_id: str = None,
type: str = None, status: str = None, days: int = None) -> dict:
# 브랜치별 WHERE (값은 ? 바인딩, type은 브랜치 선택용). status는 task 전용 → 주면 log 제외.
task_where, task_params = [], []
log_where, log_params = [], []
if agent_id:
task_where.append("agent_id=?"); task_params.append(agent_id)
log_where.append("agent_id=?"); log_params.append(agent_id)
if status:
task_where.append("status=?"); task_params.append(status)
if days and days > 0:
task_where.append("created_at >= datetime('now', ?)"); task_params.append(f"-{int(days)} days")
log_where.append("created_at >= datetime('now', ?)"); log_params.append(f"-{int(days)} days")
include_tasks = type in (None, "task")
include_logs = type in (None, "log") and not status
rows = conn.execute("""
task_clause = (" WHERE " + " AND ".join(task_where)) if task_where else ""
log_clause = (" WHERE " + " AND ".join(log_where)) if log_where else ""
branches, branch_params = [], []
if include_tasks:
branches.append(f"""
SELECT 'task' AS type, agent_id, id AS task_id, task_type,
status, NULL AS level,
COALESCE(
json_extract(result_data, '$.summary'),
task_type
) AS message,
created_at, completed_at,
result_data
FROM agent_tasks
UNION ALL
COALESCE(json_extract(result_data, '$.summary'), task_type) AS message,
created_at, completed_at, result_data
FROM agent_tasks{task_clause}""")
branch_params += task_params
if include_logs:
branches.append(f"""
SELECT 'log' AS type, agent_id, task_id, NULL AS task_type,
NULL AS status, level,
message,
created_at, NULL AS completed_at,
NULL AS result_data
FROM agent_logs
ORDER BY created_at DESC
LIMIT ? OFFSET ?
""", (limit, offset)).fetchall()
NULL AS status, level, message,
created_at, NULL AS completed_at, NULL AS result_data
FROM agent_logs{log_clause}""")
branch_params += log_params
if not branches:
return {"items": [], "total": 0}
union_sql = " UNION ALL ".join(branches) + " ORDER BY created_at DESC LIMIT ? OFFSET ?"
with _conn() as conn:
total = 0
if include_tasks:
total += conn.execute(
f"SELECT COUNT(*) AS c FROM agent_tasks{task_clause}", task_params
).fetchone()["c"]
if include_logs:
total += conn.execute(
f"SELECT COUNT(*) AS c FROM agent_logs{log_clause}", log_params
).fetchone()["c"]
rows = conn.execute(union_sql, branch_params + [limit, offset]).fetchall()
items = []
for r in rows: