diff --git a/app/api/endpoints/dashboard.py b/app/api/endpoints/dashboard.py index 9634daf5..87d21543 100644 --- a/app/api/endpoints/dashboard.py +++ b/app/api/endpoints/dashboard.py @@ -2,12 +2,15 @@ from pathlib import Path from typing import Any, List from fastapi import APIRouter, Depends +from requests import Session from app import schemas from app.chain.dashboard import DashboardChain from app.core.config import settings from app.core.security import verify_token -from app.scheduler import SchedulerChain, Scheduler +from app.db import get_db +from app.db.models.transferhistory import TransferHistory +from app.scheduler import Scheduler from app.utils.string import StringUtils from app.utils.system import SystemUtils from app.utils.timer import TimerUtils @@ -100,3 +103,13 @@ def schedule(_: schemas.TokenPayload = Depends(verify_token)) -> Any: )) return schedulers + + +@router.get("/transfer", summary="文件整理统计", response_model=List[int]) +def transfer(days: int = 7, db: Session = Depends(get_db), + _: schemas.TokenPayload = Depends(verify_token)) -> Any: + """ + 查询文件整理统计信息 + """ + transfer_stat = TransferHistory.statistic(db, days) + return [stat[1] for stat in transfer_stat] diff --git a/app/db/models/transferhistory.py b/app/db/models/transferhistory.py index 4b840763..0b49b478 100644 --- a/app/db/models/transferhistory.py +++ b/app/db/models/transferhistory.py @@ -1,6 +1,6 @@ import time -from sqlalchemy import Column, Integer, String, Sequence, Boolean +from sqlalchemy import Column, Integer, String, Sequence, Boolean, func from sqlalchemy.orm import Session from app.db.models import Base @@ -58,3 +58,15 @@ class TransferHistory(Base): @staticmethod def get_by_hash(db: Session, download_hash: str): return db.query(TransferHistory).filter(TransferHistory.download_hash == download_hash).first() + + @staticmethod + def statistic(db: Session, days: int = 7): + """ + 统计最近days天的下载历史数量,按日期分组返回每日数量 + """ + sub_query = db.query(func.substr(TransferHistory.date, 1, 10).label('date'), + TransferHistory.id.label('id')).filter( + TransferHistory.date >= time.strftime("%Y-%m-%d %H:%M:%S", + time.localtime(time.time() - 86400 * days))).subquery() + return db.query(sub_query.c.date, func.count(sub_query.c.id)).group_by(sub_query.c.date).all() + diff --git a/app/db/transferhistory_oper.py b/app/db/transferhistory_oper.py index c74330e2..34549799 100644 --- a/app/db/transferhistory_oper.py +++ b/app/db/transferhistory_oper.py @@ -29,3 +29,10 @@ class TransferHistoryOper(DbOper): "date": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) }) return TransferHistory(**kwargs).create(self._db) + + def statistic(self, days: int = 7): + """ + 统计最近days天的下载历史数量 + """ + return TransferHistory.statistic(self._db, days) +