feat: refine collected data overview and admin navigation

This commit is contained in:
linkong
2026-03-27 15:08:45 +08:00
parent a761dfc5fb
commit 3dd210a3e5
9 changed files with 310 additions and 75 deletions

View File

@@ -214,21 +214,35 @@ async def list_collected_data(
@router.get("/summary")
async def get_data_summary(
mode: str = Query("current", description="查询模式: current/history"),
source: Optional[str] = Query(None, description="数据源过滤"),
data_type: Optional[str] = Query(None, description="数据类型过滤"),
country: Optional[str] = Query(None, description="国家过滤"),
search: Optional[str] = Query(None, description="搜索名称"),
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""获取数据汇总统计"""
where_sql = "WHERE COALESCE(is_current, TRUE) = TRUE" if mode != "history" else ""
where_sql, params = build_where_clause(source, data_type, country, search)
if mode != "history":
where_sql = f"({where_sql}) AND COALESCE(is_current, TRUE) = TRUE"
overall_where_sql = "COALESCE(is_current, TRUE) = TRUE" if mode != "history" else "1=1"
overall_total_result = await db.execute(
text(f"SELECT COUNT(*) FROM collected_data WHERE {overall_where_sql}")
)
overall_total = overall_total_result.scalar() or 0
# By source and data_type
result = await db.execute(
text("""
text(f"""
SELECT source, data_type, COUNT(*) as count
FROM collected_data
""" + where_sql + """
WHERE {where_sql}
GROUP BY source, data_type
ORDER BY source, data_type
""")
"""),
params,
)
rows = result.fetchall()
source_name_map = await get_source_name_map(db)
@@ -248,18 +262,32 @@ async def get_data_summary(
# Total by source
source_totals = await db.execute(
text("""
text(f"""
SELECT source, COUNT(*) as count
FROM collected_data
""" + where_sql + """
WHERE {where_sql}
GROUP BY source
ORDER BY count DESC
""")
"""),
params,
)
source_rows = source_totals.fetchall()
type_totals = await db.execute(
text(f"""
SELECT data_type, COUNT(*) as count
FROM collected_data
WHERE {where_sql}
GROUP BY data_type
ORDER BY count DESC, data_type
"""),
params,
)
type_rows = type_totals.fetchall()
return {
"total_records": total,
"overall_total_records": overall_total,
"by_source": by_source,
"source_totals": [
{
@@ -269,6 +297,13 @@ async def get_data_summary(
}
for row in source_rows
],
"type_totals": [
{
"data_type": row[0],
"count": row[1],
}
for row in type_rows
],
}