From ad199bd26d617dfe46a69ed2003d8959f32b7e03 Mon Sep 17 00:00:00 2001 From: "rx.wu" Date: Tue, 30 Jan 2024 15:18:18 +0800 Subject: [PATCH] fix: statistics data bug --- app/crud/operation/PityOperationDao.py | 1 + app/crud/test_case/TestCaseDao.py | 5 +++-- app/routers/operation/operation_log.py | 13 ++++++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/crud/operation/PityOperationDao.py b/app/crud/operation/PityOperationDao.py index 4299c332..cc2cf716 100644 --- a/app/crud/operation/PityOperationDao.py +++ b/app/crud/operation/PityOperationDao.py @@ -26,4 +26,5 @@ async def count_user_activities(cls, user_id, start_time: datetime, end_time: da PityOperationLog.user_id == user_id) \ .group_by(PityOperationLog.operate_time).order_by(PityOperationLog.operate_time) data = await session.execute(sql) + print(sql) return data.all() diff --git a/app/crud/test_case/TestCaseDao.py b/app/crud/test_case/TestCaseDao.py index 8edbddc8..6ea30756 100644 --- a/app/crud/test_case/TestCaseDao.py +++ b/app/crud/test_case/TestCaseDao.py @@ -1,4 +1,5 @@ import json +from collections import defaultdict from datetime import datetime, timedelta from typing import List, Dict @@ -395,7 +396,7 @@ async def query_user_case_rank(cls, session: AsyncSession = None) -> List: @staticmethod async def query_weekly_user_case(user_id: int, start_time: datetime, end_time: datetime) -> List: - ans = dict() + ans = defaultdict(int) async with async_session() as session: async with session.begin(): # date_ = func.date_format(TestCase.created_at, "%Y-%m-%d") @@ -406,7 +407,7 @@ async def query_weekly_user_case(user_id: int, start_time: datetime, end_time: d query = await session.execute(sql) for i, q in enumerate(query.all()): date, count = q - ans[date.strftime("%Y-%m-%d")] = count + ans[date.strftime("%Y-%m-%d")] += count return await TestCaseDao.fill_data(start_time, end_time, ans) @staticmethod diff --git a/app/routers/operation/operation_log.py b/app/routers/operation/operation_log.py index 05f41d18..be726caa 100644 --- a/app/routers/operation/operation_log.py +++ b/app/routers/operation/operation_log.py @@ -1,3 +1,4 @@ +from collections import defaultdict from datetime import datetime from fastapi import APIRouter, Depends @@ -15,8 +16,8 @@ @router.get("/list") async def list_user_operation(start_time: str, end_time: str, user_id: int, tag: str = None, _=Depends(Permission())): try: - start = datetime.strptime(start_time, "%Y-%m-%d") - end = datetime.strptime(end_time, "%Y-%m-%d") + start = datetime.strptime(start_time, "%Y-%m-%d").replace(hour=0, minute=0, second=0, microsecond=0) + end = datetime.strptime(end_time, "%Y-%m-%d").replace(hour=23, minute=59, second=59, microsecond=0) records = await PityOperationDao.select_list(user_id=user_id, tag=tag, condition=[ PityOperationLog.operate_time.between(start, end)], _sort=[desc(PityOperationLog.operate_time)]) return PityResponse.records(records) @@ -32,10 +33,16 @@ async def list_user_activities(user_id: int, start_time: str, end_time: str, _=D end = datetime.strptime(end_time, "%Y-%m-%d").replace(hour=23, minute=59, second=59, microsecond=0) records = await PityOperationDao.count_user_activities(user_id, start, end) ans = list() + date_index = dict() for r in records: # 解包日期和数量 date, count = r - ans.append(dict(date=date.strftime("%Y-%m-%d"), count=count)) + date_str = date.strftime("%Y-%m-%d") + if date_index.get(date_str) is None: + ans.append(dict(date=date_str, count=count)) + date_index[date_str] = len(ans) - 1 + else: + ans[date_index[date_str]]['count'] += 1 return PityResponse.success(ans) except Exception as e: return PityResponse.failed(e)