mirror of
https://github.com/stardrophere/InsightRadar.git
synced 2026-06-06 00:39:21 +08:00
backend 去ai化
This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
"""
|
||||
认证模块:用户注册、登录、邮箱验证码(支持 Redis / 数据库双存储与自动降级)
|
||||
"""
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
# 推送设置 API:管理用户的推送时间表和推送渠道
|
||||
# 关键约束:同一用户两条推送时间间隔至少 30 分钟
|
||||
from datetime import time as dt_time
|
||||
from typing import List
|
||||
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
# app/api/endpoints/events.py
|
||||
"""
|
||||
事件模块:统一事件列表、详情、搜索时间线(支持精确/语义/混合匹配)
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
@@ -41,10 +37,8 @@ SEARCH_MAX_HOURS = int(os.getenv("SEARCH_MAX_HOURS", "168"))
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# 排名轨迹最多返回的点数,避免时间跨度过大时响应体过重。
|
||||
MAX_RANKING_POINTS = 30
|
||||
|
||||
# 统一事件列表接口的短期缓存。
|
||||
_UNIFIED_EVENTS_CACHE: Dict[str, Tuple[float, PaginatedUnifiedEventResponse]] = {}
|
||||
CACHE_TTL_SECONDS = 60
|
||||
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
"""
|
||||
用户偏好模块:兴趣关键词的增删查、基于关键词的个性化事件推荐
|
||||
"""
|
||||
import time
|
||||
from typing import Any, Dict, List, Tuple
|
||||
|
||||
@@ -20,7 +17,6 @@ from app.services.matching_service import recommend_events_for_user
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# --- 轻量级接口缓存配置 ---
|
||||
_RECOMMEND_CACHE: Dict[str, Tuple[float, Any]] = {}
|
||||
CACHE_TTL_SECONDS = 60
|
||||
|
||||
@@ -29,7 +25,6 @@ def _invalidate_user_cache(user_id: int):
|
||||
keys_to_delete = [k for k in _RECOMMEND_CACHE.keys() if k.startswith(f"{user_id}:")]
|
||||
for k in keys_to_delete:
|
||||
_RECOMMEND_CACHE.pop(k, None)
|
||||
# ---------------------------
|
||||
|
||||
def _ensure_self_access(path_user_id: int, current_user: AppUser) -> None:
|
||||
"""校验路径 user_id 是否为当前登录用户本人。"""
|
||||
@@ -93,7 +88,7 @@ def create_user_preference(
|
||||
)
|
||||
|
||||
db.refresh(db_obj)
|
||||
_invalidate_user_cache(user_id) # 失效推荐缓存
|
||||
_invalidate_user_cache(user_id)
|
||||
return db_obj
|
||||
|
||||
|
||||
@@ -122,7 +117,7 @@ def delete_user_preference(
|
||||
|
||||
db.delete(preference)
|
||||
db.commit()
|
||||
_invalidate_user_cache(user_id) # 失效推荐缓存
|
||||
_invalidate_user_cache(user_id)
|
||||
return None
|
||||
|
||||
|
||||
@@ -143,7 +138,6 @@ def recommend_events(
|
||||
"""基于用户兴趣词推荐事件(精确匹配 + 语义匹配)。"""
|
||||
_ensure_self_access(user_id, current_user)
|
||||
|
||||
# 推荐结果缓存,避免频繁调用匹配服务
|
||||
cache_key = f"{user_id}:{min_hot}:{hours}:{limit}:{semantic_threshold}:{sort_by}"
|
||||
current_time = time.time()
|
||||
|
||||
@@ -151,7 +145,6 @@ def recommend_events(
|
||||
expire_time, cached_data = _RECOMMEND_CACHE[cache_key]
|
||||
if current_time < expire_time:
|
||||
return cached_data
|
||||
# -----------------------
|
||||
|
||||
matched = recommend_events_for_user(
|
||||
db,
|
||||
@@ -189,10 +182,8 @@ def recommend_events(
|
||||
|
||||
# 写入缓存,超过 2000 条时清空防止内存膨胀
|
||||
if len(_RECOMMEND_CACHE) > 2000:
|
||||
# 防止内存无限增长
|
||||
_RECOMMEND_CACHE.clear()
|
||||
|
||||
_RECOMMEND_CACHE[cache_key] = (current_time + CACHE_TTL_SECONDS, response)
|
||||
# ------------------
|
||||
|
||||
return response
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
# 公关修改追踪 API:查询热搜标题被偷偷修改的历史记录,用于舆情监测
|
||||
from datetime import timedelta
|
||||
from typing import List, Optional
|
||||
|
||||
@@ -39,7 +38,6 @@ def list_headline_revisions(
|
||||
"""
|
||||
time_limit = utcnow() - timedelta(hours=hours)
|
||||
|
||||
# 关联 TrendingEvent、InfoSource 获取平台名和链接
|
||||
rows = (
|
||||
db.query(HeadlineRevision, InfoSource.source_name, TrendingEvent.event_url)
|
||||
.join(TrendingEvent, HeadlineRevision.event_id == TrendingEvent.id)
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
# app/api/endpoints/sources.py
|
||||
"""
|
||||
信息源模块:信息源的增删改查,供爬虫与后台管理使用
|
||||
"""
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
@@ -43,6 +39,4 @@ async def update_info_source(source_id: int, source_in: InfoSourceUpdate, db: Se
|
||||
source = crud_source.get(db=db, source_id=source_id)
|
||||
if not source:
|
||||
raise HTTPException(status_code=404, detail="该信息源不存在")
|
||||
|
||||
# 直接把查出来的数据库对象和前端传来的 Pydantic 对象丢给 CRUD 处理
|
||||
return crud_source.update(db=db, db_obj=source, obj_in=source_in)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
# 系统状态监控 API:返回爬虫集群运行概况(信息源数、今日抓取量、最近同步时间等)
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
|
||||
@@ -28,7 +27,6 @@ def get_system_stats(db: Session = Depends(get_db)):
|
||||
"""获取爬虫集群的当日运行状态。"""
|
||||
today_start = utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
|
||||
# 信息源统计:总数与启用数
|
||||
total_sources = db.query(func.count(InfoSource.id)).scalar() or 0
|
||||
active_sources = (
|
||||
db.query(func.count(InfoSource.id))
|
||||
@@ -36,7 +34,6 @@ def get_system_stats(db: Session = Depends(get_db)):
|
||||
.scalar() or 0
|
||||
)
|
||||
|
||||
# 今日任务统计:抓取条数、成功/失败任务数
|
||||
today_tasks = (
|
||||
db.query(DataSyncTask)
|
||||
.filter(DataSyncTask.created_at >= today_start)
|
||||
@@ -47,7 +44,6 @@ def get_system_stats(db: Session = Depends(get_db)):
|
||||
success_count = sum(1 for t in today_tasks if t.task_status == TaskStatus.SUCCESS)
|
||||
error_count = sum(1 for t in today_tasks if t.task_status == TaskStatus.ERROR)
|
||||
|
||||
# 最后一次同步时间
|
||||
last_task = (
|
||||
db.query(DataSyncTask)
|
||||
.filter(DataSyncTask.task_status == TaskStatus.SUCCESS)
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
# app/api/router.py
|
||||
from fastapi import APIRouter
|
||||
from app.api.endpoints import auth, delivery, events, preferences, revisions, sources, stats
|
||||
|
||||
|
||||
Reference in New Issue
Block a user