feat: 添加用户查询统计接口

This commit is contained in:
皓月归尘 2025-02-19 01:20:04 +08:00
parent 2ced3bf707
commit 78ab92cdb6
4 changed files with 149 additions and 5 deletions

View File

@ -289,7 +289,8 @@ async def get_code_list(request: Request,
query_code = await QueryCode.create(
query_text=description.strip(),
result_text=jsonable_encoder(matches),
session_id=log.id
session_id=log.id,
status=1 if matches else 0
)
dataList.append({
"id": query_code.id,
@ -389,7 +390,8 @@ async def get_code_list(request: Request,
query_code = await QueryCode.create(
query_text=row['text'].strip(),
result_text=jsonable_encoder(matches),
session_id=log.id
session_id=log.id,
status=1 if matches else 0
)
dataList.append({
"id": query_code.id,

View File

@ -5,8 +5,9 @@
# @File : user.py
# @Software : PyCharm
# @Comment : 本程序
import calendar
import os
from datetime import datetime
from datetime import datetime, timedelta
from typing import Optional
from fastapi import APIRouter, Depends, Path, Query, UploadFile, File, Request
@ -20,13 +21,14 @@ from controller.login import LoginController
from controller.query import QueryController
from exceptions.exception import ModelValidatorException
from models import File as FileModel
from models import Role, Department
from models import Role, Department, QueryCode
from models.user import User, UserRole
from schemas.common import BaseResponse
from schemas.department import GetDepartmentListResponse
from schemas.file import UploadFileResponse
from schemas.user import AddUserParams, GetUserListResponse, GetUserInfoResponse, UpdateUserParams, \
AddUserRoleParams, GetUserRoleInfoResponse, UpdateUserRoleParams, GetUserPermissionListResponse, ResetPasswordParams
AddUserRoleParams, GetUserRoleInfoResponse, UpdateUserRoleParams, GetUserPermissionListResponse, \
ResetPasswordParams, GetUserStatisticsResponse
from utils.common import filterKeyValues
from utils.password import Password
from utils.response import Response
@ -347,3 +349,99 @@ async def reset_user_password(request: Request, params: ResetPasswordParams, id:
await user.save()
return Response.success(msg="重置密码成功!")
return Response.failure(msg="用户不存在!")
@userAPI.get("/statistics", response_model=GetUserStatisticsResponse, response_class=JSONResponse,
summary="获取用户查询统计")
@Log(title="获取用户查询统计", business_type=BusinessType.SELECT)
async def get_user_statistics(request: Request, current_user: dict = Depends(LoginController.get_current_user)):
user_id = current_user.get("id")
# 获取当前时间
now = datetime.now()
# 今日开始时间
today_start_time = now.replace(hour=0, minute=0, second=0, microsecond=0)
# 今日结束时间
today_end_time = now.replace(hour=23, minute=59, second=59, microsecond=999999)
# 当月开始时间
this_month_start_time = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
# 计算当月的最后一天
this_month_last_day = calendar.monthrange(now.year, now.month)[1]
# 当月结束时间
this_month_end_time = now.replace(
day=this_month_last_day, hour=23, minute=59, second=59, microsecond=999999
)
# 计算上个月的年和月
if now.month == 1: # 处理1月上月是去年的12月
last_month_year = now.year - 1
last_month = 12
else:
last_month_year = now.year
last_month = now.month - 1
# 上月开始时间
last_month_start_time = datetime(last_month_year, last_month, 1, 0, 0, 0, 0)
# 计算上个月最后一天
last_month_last_day = calendar.monthrange(last_month_year, last_month)[1]
# 上月结束时间
last_month_end_time = datetime(last_month_year, last_month, last_month_last_day, 23, 59, 59, 999999)
# 计算今日查询数量
today_count = await QueryCode.filter(create_time__gte=today_start_time, create_time__lte=today_end_time,
session__operator__id=user_id).count()
# 计算当月查询数量
this_month_count = await QueryCode.filter(create_time__gte=this_month_start_time,
create_time__lte=this_month_end_time,
session__operator__id=user_id).count()
# 计算上月查询数量
last_month_count = await QueryCode.filter(create_time__gte=last_month_start_time,
create_time__lte=last_month_end_time,
session__operator__id=user_id).count()
async def get_last_14_days_count(status: int = 1):
today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
# 保存结果的列表
result = []
# 遍历最近7天包括今天
for i in range(14):
day = today - timedelta(days=i)
# 当天的开始时间和结束时间
day_start = day # 00:00:00
day_end = (day + timedelta(days=1)) # 23:59:59
# 统计当天查询数量
count = await QueryCode.filter(
create_time__gte=day_start,
create_time__lte=day_end,
session__operator__id=user_id,
status=status
).count()
# 添加到结果列表(格式化日期为 YYYY-MM-DD
result.append({"date": day.strftime("%Y-%m-%d"), "count": count})
# 结果按日期升序排列(可选,确保从过去到现在排序)
result.sort(key=lambda x: x["date"])
return result
# 过去14天内的查询数量
before_14day_count_success = await get_last_14_days_count(1)
before_14day_count_fail = await get_last_14_days_count(0)
return Response.success(data={
"today_count": today_count,
"this_month_count": this_month_count,
"last_month_count": last_month_count,
"before_14day_count_success": before_14day_count_success,
"before_14day_count_fail": before_14day_count_fail,
})

View File

@ -151,6 +151,11 @@ class QueryCode(BaseModel):
description="结果文本",
source_field="result_text" # 映射到数据库字段 result_text
)
status = fields.SmallIntField(
default=1,
description="操作状态1成功0失败",
source_field="status" # 映射到数据库字段 status
)
class Meta:
table = "query_code"

View File

@ -348,3 +348,42 @@ class ResetPasswordParams(BaseModel):
"password": "123456"
}
}
class GetUserStatisticsResult(BaseModel):
"""
用户统计信息模型
"""
today_count: int = Field(default=0, description="今日查询次数")
this_month_count: int = Field(default=0, description="本月查询次数")
last_month_count: int = Field(default=0, description="上月查询次数")
before_14day_count_success: List[dict] = Field(default=[], description="最近14天成功查询次数")
before_14day_count_fail: List[dict] = Field(default=[], description="最近14天失败查询次数")
class Config:
json_schema_extra = {
"example": {
"today_count": 0,
"this_month_count": 0,
"last_month_count": 0,
"before_14day_count_success": [
{
"date": "2023-10-01",
"count": 0
}
],
"before_14day_count_fail": [
{
"date": "2023-10-01",
"count": 0
}
]
}
}
class GetUserStatisticsResponse(BaseResponse):
"""
获取用户统计信息响应模型
"""
data: GetUserStatisticsResult = Field(default=None, description="响应数据")