优化情报分栏和日报展示

This commit is contained in:
Codex
2026-07-08 23:39:41 +08:00
parent 88e1cf4b7a
commit 8e15211f07
16 changed files with 371 additions and 93 deletions
+28 -7
View File
@@ -1,6 +1,6 @@
from __future__ import annotations
from typing import Optional
from typing import Literal, Optional
from fastapi import APIRouter, Depends
from fastapi.responses import HTMLResponse
@@ -42,11 +42,19 @@ def list_runs(db: Session = Depends(get_db)) -> list[AgentRun]:
@router.get("/signals", response_model=list[SignalRead])
def list_signals(limit: int = 50, db: Session = Depends(get_db)) -> list[Signal]:
def list_signals(
limit: int = 100,
source_type: Optional[Literal["news", "github"]] = None,
db: Session = Depends(get_db),
) -> list[Signal]:
# 情报列表按采集时间倒序返回,是仪表盘的主要阅读入口。
safe_limit = max(1, min(limit, 200))
return list(
db.scalars(_visible_signals_query().order_by(Signal.id.desc()).limit(safe_limit)).all()
db.scalars(
_visible_signals_query(source_type=source_type)
.order_by(Signal.id.desc())
.limit(safe_limit)
).all()
)
@@ -60,12 +68,22 @@ def latest_report(db: Session = Depends(get_db)) -> Optional[Report]:
def dashboard(db: Session = Depends(get_db)) -> DashboardRead:
# 单个仪表盘接口返回运行、信号、日报和主题,前端不需要理解内部调度细节。
runs = list(db.scalars(select(AgentRun).order_by(AgentRun.id.desc()).limit(10)).all())
signals = list(db.scalars(_visible_signals_query().order_by(Signal.id.desc()).limit(80)).all())
news_signals = list(
db.scalars(
_visible_signals_query(source_type="news").order_by(Signal.id.desc()).limit(80)
).all()
)
github_signals = list(
db.scalars(
_visible_signals_query(source_type="github").order_by(Signal.id.desc()).limit(80)
).all()
)
report = db.scalar(select(Report).order_by(Report.id.desc()).limit(1))
topics = list(db.scalars(select(Topic).order_by(Topic.id.asc())).all())
return DashboardRead(
runs=runs,
signals=signals,
news_signals=news_signals,
github_signals=github_signals,
latest_report=report,
topics=topics,
)
@@ -96,6 +114,9 @@ def list_topics(db: Session = Depends(get_db)) -> list[Topic]:
return list(db.scalars(select(Topic).order_by(Topic.id.asc())).all())
def _visible_signals_query():
def _visible_signals_query(source_type: Optional[Literal["news", "github"]] = None):
# 运行中的信号和完成后的信号走同一张表,查询函数保留统一排序入口。
return select(Signal).join(AgentRun, Signal.run_id == AgentRun.id)
query = select(Signal).join(AgentRun, Signal.run_id == AgentRun.id)
if source_type is not None:
query = query.where(Signal.source_type == source_type)
return query