fix dashboard

This commit is contained in:
jxxghp 2023-07-11 09:04:17 +08:00
parent 1ec65a127a
commit 5f757e9d60
3 changed files with 34 additions and 2 deletions

View File

@ -2,12 +2,15 @@ from pathlib import Path
from typing import Any, List from typing import Any, List
from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends
from requests import Session
from app import schemas from app import schemas
from app.chain.dashboard import DashboardChain from app.chain.dashboard import DashboardChain
from app.core.config import settings from app.core.config import settings
from app.core.security import verify_token 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.string import StringUtils
from app.utils.system import SystemUtils from app.utils.system import SystemUtils
from app.utils.timer import TimerUtils from app.utils.timer import TimerUtils
@ -100,3 +103,13 @@ def schedule(_: schemas.TokenPayload = Depends(verify_token)) -> Any:
)) ))
return schedulers 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]

View File

@ -1,6 +1,6 @@
import time 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 sqlalchemy.orm import Session
from app.db.models import Base from app.db.models import Base
@ -58,3 +58,15 @@ class TransferHistory(Base):
@staticmethod @staticmethod
def get_by_hash(db: Session, download_hash: str): def get_by_hash(db: Session, download_hash: str):
return db.query(TransferHistory).filter(TransferHistory.download_hash == download_hash).first() 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()

View File

@ -29,3 +29,10 @@ class TransferHistoryOper(DbOper):
"date": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) "date": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
}) })
return TransferHistory(**kwargs).create(self._db) return TransferHistory(**kwargs).create(self._db)
def statistic(self, days: int = 7):
"""
统计最近days天的下载历史数量
"""
return TransferHistory.statistic(self._db, days)