mirror of
https://github.com/stardrophere/InsightRadar.git
synced 2026-06-05 23:32:49 +08:00
update
This commit is contained in:
@@ -30,7 +30,8 @@ MAX_RANKING_POINTS = 30
|
||||
@router.get("/unified", response_model=PaginatedUnifiedEventResponse)
|
||||
def list_unified_events(
|
||||
min_hot: int = Query(5, ge=0, description="热度阈值,仅返回 hot_score >= 此值的事件"),
|
||||
hours: int = Query(24, ge=1, le=720, description="查询最近多少小时的数据"),
|
||||
hours: int = Query(48, ge=1, le=720, description="查询最近多少小时的数据"),
|
||||
sort_by: str = Query("hot_score", description="排序字段: hot_score | created_at"),
|
||||
skip: int = Query(0, ge=0, description="分页偏移量"),
|
||||
limit: int = Query(10, ge=1, le=50, description="每页返回条数"),
|
||||
db: Session = Depends(get_db),
|
||||
@@ -46,13 +47,12 @@ def list_unified_events(
|
||||
total = base_query.count()
|
||||
|
||||
# 分页查询
|
||||
events = (
|
||||
base_query
|
||||
.order_by(UnifiedEvent.hot_score.desc())
|
||||
.offset(skip)
|
||||
.limit(limit)
|
||||
.all()
|
||||
)
|
||||
if sort_by == "created_at":
|
||||
base_query = base_query.order_by(UnifiedEvent.created_at.desc())
|
||||
else:
|
||||
base_query = base_query.order_by(UnifiedEvent.hot_score.desc(), UnifiedEvent.created_at.desc())
|
||||
|
||||
events = base_query.offset(skip).limit(limit).all()
|
||||
|
||||
if not events:
|
||||
return PaginatedUnifiedEventResponse(total=total, has_more=False, data=[])
|
||||
@@ -110,7 +110,8 @@ def list_unified_events(
|
||||
results: list[UnifiedEventResponse] = []
|
||||
for ev in events:
|
||||
platform_list: list[PlatformTrendResponse] = []
|
||||
for trend, source_name in trend_map.get(ev.id, []):
|
||||
trends_for_ev = trend_map.get(ev.id, [])
|
||||
for trend, source_name in trends_for_ev:
|
||||
history = ranking_map.get(trend.id, [])
|
||||
# 截取尾部,只保留最近的点
|
||||
if len(history) > MAX_RANKING_POINTS:
|
||||
@@ -127,6 +128,13 @@ def list_unified_events(
|
||||
)
|
||||
)
|
||||
|
||||
# 取所有关联热搜条目中最新的 updated_at,代表"最后一次在平台热搜榜看到"的时间
|
||||
last_active_at = (
|
||||
max(t.updated_at for t, _ in trends_for_ev)
|
||||
if trends_for_ev
|
||||
else ev.updated_at
|
||||
)
|
||||
|
||||
results.append(
|
||||
UnifiedEventResponse(
|
||||
event_id=ev.id,
|
||||
@@ -134,6 +142,7 @@ def list_unified_events(
|
||||
summary=ev.ai_comprehensive_summary,
|
||||
hot_score=ev.hot_score,
|
||||
created_at=ev.created_at,
|
||||
last_active_at=last_active_at,
|
||||
platforms=platform_list,
|
||||
tags=tag_map.get(ev.id, []),
|
||||
)
|
||||
@@ -204,12 +213,19 @@ def get_unified_event(
|
||||
)
|
||||
)
|
||||
|
||||
last_active_at = (
|
||||
max(t.updated_at for t, _ in trend_rows)
|
||||
if trend_rows
|
||||
else ev.updated_at
|
||||
)
|
||||
|
||||
return UnifiedEventResponse(
|
||||
event_id=ev.id,
|
||||
unified_title=ev.unified_title if ev.unified_title else "暂无标题",
|
||||
summary=ev.ai_comprehensive_summary,
|
||||
hot_score=ev.hot_score,
|
||||
created_at=ev.created_at,
|
||||
last_active_at=last_active_at,
|
||||
platforms=platform_list,
|
||||
tags=tags,
|
||||
)
|
||||
|
||||
@@ -120,6 +120,7 @@ def recommend_events(
|
||||
hours: int = Query(72, ge=1, le=24 * 30, description="仅匹配最近多少小时的事件"),
|
||||
limit: int = Query(20, ge=1, le=50, description="最多返回多少条推荐"),
|
||||
semantic_threshold: float = Query(0.78, ge=0.0, le=1.0, description="语义匹配相似度阈值"),
|
||||
sort_by: str = Query("match_score", description="排序方式: match_score | created_at"),
|
||||
db: Session = Depends(get_db),
|
||||
current_user: AppUser = Depends(get_current_user),
|
||||
):
|
||||
@@ -135,6 +136,9 @@ def recommend_events(
|
||||
semantic_threshold=semantic_threshold,
|
||||
)
|
||||
|
||||
if sort_by == "created_at":
|
||||
matched.sort(key=lambda x: x.event.created_at, reverse=True)
|
||||
|
||||
result_data: list[MatchedEventResponse] = []
|
||||
for item in matched:
|
||||
result_data.append(
|
||||
|
||||
@@ -21,6 +21,7 @@ class HeadlineRevisionResponse(BaseModel):
|
||||
revised_headline: str
|
||||
source_name: Optional[str] = None
|
||||
platform_icon: Optional[str] = None
|
||||
url: Optional[str] = None
|
||||
created_at: datetime
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
@@ -39,7 +40,7 @@ def list_headline_revisions(
|
||||
time_limit = utcnow() - timedelta(hours=hours)
|
||||
|
||||
rows = (
|
||||
db.query(HeadlineRevision, InfoSource.source_name)
|
||||
db.query(HeadlineRevision, InfoSource.source_name, TrendingEvent.event_url)
|
||||
.join(TrendingEvent, HeadlineRevision.event_id == TrendingEvent.id)
|
||||
.join(InfoSource, TrendingEvent.source_id == InfoSource.id)
|
||||
.filter(HeadlineRevision.created_at >= time_limit)
|
||||
@@ -59,7 +60,7 @@ def list_headline_revisions(
|
||||
}
|
||||
|
||||
results: list[HeadlineRevisionResponse] = []
|
||||
for revision, source_name in rows:
|
||||
for revision, source_name, event_url in rows:
|
||||
results.append(
|
||||
HeadlineRevisionResponse(
|
||||
id=revision.id,
|
||||
@@ -68,6 +69,7 @@ def list_headline_revisions(
|
||||
revised_headline=revision.revised_headline,
|
||||
source_name=source_name,
|
||||
platform_icon=icon_map.get(source_name, "newspaper"),
|
||||
url=event_url,
|
||||
created_at=revision.created_at,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -19,6 +19,7 @@ class UnifiedEventResponse(BaseModel):
|
||||
summary: Optional[str]
|
||||
hot_score: int
|
||||
created_at: datetime
|
||||
last_active_at: datetime
|
||||
platforms: List[PlatformTrendResponse]
|
||||
tags: List[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user