From 894233974448a02bcbe0e4c88f3e90cec2c37fa5 Mon Sep 17 00:00:00 2001 From: QihuAI Design Date: Thu, 16 Jul 2026 15:39:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(skills-admin):=20get=5Fskills=5Fstats=20?= =?UTF-8?q?=E5=90=88=E5=B9=B6=20market=5Fapps(type=3Dskill)=20=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复顶部卡片仍显示 0 的问题:list_skills 已支持合并查询, 但 stats/overview 端点仍只读 evolution_skills。 新增对 market_apps 的统计: published→main, 其它→baby。 验证: total_skills=50, by_state.main=50, total_published=50 --- backend/python/app/api/admin/skills_admin.py | 47 ++++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/backend/python/app/api/admin/skills_admin.py b/backend/python/app/api/admin/skills_admin.py index 1e371db..e8657ae 100644 --- a/backend/python/app/api/admin/skills_admin.py +++ b/backend/python/app/api/admin/skills_admin.py @@ -11,7 +11,7 @@ from typing import Optional, List from fastapi import APIRouter, Depends, HTTPException, Query 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 ...models import ( @@ -839,27 +839,58 @@ async def get_skills_stats( admin_user=Depends(get_current_admin), db: Session = Depends(get_db), ): - """获取技能统计概览""" - total = db.query(EvolutionSkill).count() - by_state = dict( + """获取技能统计概览(EvolutionSkill + MarketApp type=skill 合并统计)""" + # EvolutionSkill 维度 + evo_total = db.query(EvolutionSkill).count() + evo_by_state = dict( db.query(EvolutionSkill.state, func.count(EvolutionSkill.id)) .group_by(EvolutionSkill.state).all() ) - by_category = dict( + evo_by_category = dict( db.query(EvolutionSkill.category, func.count(EvolutionSkill.id)) .filter(EvolutionSkill.category.isnot(None)) .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" ).count() + total_published = skill_record_published + market_published total_versions = db.query(SkillVersion).count() return { "code": 0, "data": { - "total_skills": total, - "by_state": {k: by_state.get(k, 0) for k in SKILL_STATES}, + "total_skills": total_skills, + "by_state": by_state, "by_category": by_category, "total_published": total_published, "total_versions": total_versions,