Skip to content

Commit

Permalink
fix: statistics data bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wuranxu committed Jan 30, 2024
1 parent 922ba5c commit ad199bd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/crud/operation/PityOperationDao.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
5 changes: 3 additions & 2 deletions app/crud/test_case/TestCaseDao.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from collections import defaultdict
from datetime import datetime, timedelta
from typing import List, Dict

Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down
13 changes: 10 additions & 3 deletions app/routers/operation/operation_log.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import defaultdict
from datetime import datetime

from fastapi import APIRouter, Depends
Expand All @@ -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)
Expand All @@ -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)

0 comments on commit ad199bd

Please sign in to comment.