38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
|
|
from fastapi import APIRouter, Depends
|
||
|
|
from sqlalchemy import func
|
||
|
|
from sqlalchemy.orm import Session
|
||
|
|
|
||
|
|
from backend.auth import get_current_user
|
||
|
|
from backend.database import get_db
|
||
|
|
from backend.models import ImportHistory, Question
|
||
|
|
|
||
|
|
router = APIRouter(dependencies=[Depends(get_current_user)])
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("")
|
||
|
|
def get_stats(db: Session = Depends(get_db)) -> dict:
|
||
|
|
total = db.query(func.count(Question.id)).scalar() or 0
|
||
|
|
by_type = db.query(Question.question_type, func.count(Question.id)).group_by(Question.question_type).all()
|
||
|
|
by_difficulty = db.query(Question.difficulty, func.count(Question.id)).group_by(Question.difficulty).all()
|
||
|
|
by_chapter = db.query(Question.chapter, func.count(Question.id)).group_by(Question.chapter).all()
|
||
|
|
latest_imports = (
|
||
|
|
db.query(ImportHistory).order_by(ImportHistory.created_at.desc()).limit(10).all()
|
||
|
|
)
|
||
|
|
return {
|
||
|
|
"total": total,
|
||
|
|
"by_type": [{"name": n or "未分类", "value": v} for n, v in by_type],
|
||
|
|
"by_difficulty": [{"name": n or "未分类", "value": v} for n, v in by_difficulty],
|
||
|
|
"by_chapter": [{"name": n or "未分类", "value": v} for n, v in by_chapter],
|
||
|
|
"latest_imports": [
|
||
|
|
{
|
||
|
|
"id": i.id,
|
||
|
|
"filename": i.filename,
|
||
|
|
"method": i.method,
|
||
|
|
"question_count": i.question_count,
|
||
|
|
"status": i.status,
|
||
|
|
"created_at": i.created_at.isoformat(),
|
||
|
|
}
|
||
|
|
for i in latest_imports
|
||
|
|
],
|
||
|
|
}
|