fix(skills-admin): get_skills_stats 合并 market_apps(type=skill) 统计

修复顶部卡片仍显示 0 的问题:list_skills 已支持合并查询,
但 stats/overview 端点仍只读 evolution_skills。
新增对 market_apps 的统计: published→main, 其它→baby。
验证: total_skills=50, by_state.main=50, total_published=50
This commit is contained in:
QihuAI Design committed 2026-07-16 15:39:21 +08:00
1 parent ce81297159
commit 8942339744
1 file changed
+39 -8
+39 -8
View File
@@ -11,7 +11,7 @@ from typing import Optional, List
from fastapi import APIRouter, Depends, HTTPException, Query from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from sqlalchemy import desc, func, or_, and_ from sqlalchemy import desc, func, or_, and_, text
from ...database import get_db from ...database import get_db
from ...models import ( from ...models import (
@@ -839,27 +839,58 @@ async def get_skills_stats(
admin_user=Depends(get_current_admin), admin_user=Depends(get_current_admin),
db: Session = Depends(get_db), db: Session = Depends(get_db),
): ):
"""获取技能统计概览""" """获取技能统计概览(EvolutionSkill + MarketApp type=skill 合并统计)"""
total = db.query(EvolutionSkill).count() # EvolutionSkill 维度
by_state = dict( evo_total = db.query(EvolutionSkill).count()
evo_by_state = dict(
db.query(EvolutionSkill.state, func.count(EvolutionSkill.id)) db.query(EvolutionSkill.state, func.count(EvolutionSkill.id))
.group_by(EvolutionSkill.state).all() .group_by(EvolutionSkill.state).all()
) )
by_category = dict( evo_by_category = dict(
db.query(EvolutionSkill.category, func.count(EvolutionSkill.id)) db.query(EvolutionSkill.category, func.count(EvolutionSkill.id))
.filter(EvolutionSkill.category.isnot(None)) .filter(EvolutionSkill.category.isnot(None))
.group_by(EvolutionSkill.category).all() .group_by(EvolutionSkill.category).all()
) )
total_published = db.query(SkillPublishRecord).filter(
# MarketApp(type=skill) 维度:状态映射 published→main, 其它→baby
market_total = db.query(MarketApp).filter(MarketApp.type == "skill").count()
market_rows = db.query(MarketApp.status, func.count(MarketApp.id)).filter(
MarketApp.type == "skill"
).group_by(MarketApp.status).all()
market_published = sum(cnt for status, cnt in market_rows if (status or "") == "published")
# 各 category 计数(market 表 category 在 SQL 列有,ORM 无此属性,用 SQL 拿)
market_by_category = {}
try:
market_by_category_rows = db.execute(
text("SELECT category, COUNT(*) FROM market_apps WHERE type='skill' AND category IS NOT NULL GROUP BY category")
).fetchall()
market_by_category = {cat or "other": cnt for cat, cnt in market_by_category_rows}
except Exception:
pass
# 合并
total_skills = evo_total + market_total
by_state = {k: evo_by_state.get(k, 0) for k in SKILL_STATES}
by_state["main"] = by_state.get("main", 0) + market_published
by_state["baby"] = by_state.get("baby", 0) + (market_total - market_published)
by_category = {}
for c in (evo_by_category or {}).items():
by_category[c[0]] = by_category.get(c[0], 0) + c[1]
for c in (market_by_category or {}).items():
by_category[c[0]] = by_category.get(c[0], 0) + c[1]
# 原始发布记录 + market 已发布 = 全部已发布
skill_record_published = db.query(SkillPublishRecord).filter(
SkillPublishRecord.status == "published" SkillPublishRecord.status == "published"
).count() ).count()
total_published = skill_record_published + market_published
total_versions = db.query(SkillVersion).count() total_versions = db.query(SkillVersion).count()
return { return {
"code": 0, "code": 0,
"data": { "data": {
"total_skills": total, "total_skills": total_skills,
"by_state": {k: by_state.get(k, 0) for k in SKILL_STATES}, "by_state": by_state,
"by_category": by_category, "by_category": by_category,
"total_published": total_published, "total_published": total_published,
"total_versions": total_versions, "total_versions": total_versions,