mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-22 05:02:57 +00:00
refactor: 重构数据库连接模块,提高可维护性 docs: 更新README.md项目描述 style: 调整日志输出格式和内容 refactor: 优化中间件日志记录逻辑 style: 统一代码中的空行和导入顺序 refactor: 分离数据库引擎和会话创建逻辑 style: 清理未使用的导入和代码 refactor: 重命名数据库会话变量 style: 调整jinja2模板变量命名 refactor: 优化异常处理和日志记录 style: 统一字符串格式化方式 refactor: 优化redis连接错误处理 style: 调整注释格式和位置 refactor: 优化数据库连接配置 style: 清理多余空行和注释
102 lines
3.3 KiB
Python
102 lines
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from fastapi import APIRouter, Body, Depends
|
|
from fastapi.responses import JSONResponse
|
|
from redis.asyncio.client import Redis
|
|
|
|
from app.common.request import PaginationService
|
|
from app.common.response import SuccessResponse,ErrorResponse
|
|
from app.core.dependencies import AuthPermission, redis_getter
|
|
from app.core.base_params import PaginationQueryParam
|
|
from app.core.router_class import OperationLogRoute
|
|
from app.core.logger import logger
|
|
|
|
from .param import OnlineQueryParam
|
|
from .service import OnlineService
|
|
|
|
|
|
OnlineRouter = APIRouter(route_class=OperationLogRoute, prefix="/online", tags=["在线用户"])
|
|
|
|
|
|
@OnlineRouter.get(
|
|
'/list',
|
|
dependencies=[Depends(AuthPermission(['module_monitor:online:query']))],
|
|
summary="获取在线用户列表",
|
|
description="获取在线用户列表"
|
|
)
|
|
async def get_online_list_controller(
|
|
redis: Redis = Depends(redis_getter),
|
|
paging_query: PaginationQueryParam = Depends(),
|
|
search: OnlineQueryParam = Depends()
|
|
)->JSONResponse:
|
|
"""
|
|
获取在线用户列表
|
|
|
|
参数:
|
|
- redis (Redis): Redis异步客户端实例。
|
|
- paging_query (PaginationQueryParam): 分页查询参数模型。
|
|
- search (OnlineQueryParam): 查询参数模型。
|
|
|
|
返回:
|
|
- JSONResponse: 包含在线用户列表的JSON响应。
|
|
"""
|
|
result_dict_list = await OnlineService.get_online_list_service(redis=redis, search=search)
|
|
result_dict = await PaginationService.paginate(data_list= result_dict_list, page_no= paging_query.page_no, page_size = paging_query.page_size)
|
|
logger.info('获取成功')
|
|
|
|
return SuccessResponse(data=result_dict,msg='获取成功')
|
|
|
|
|
|
@OnlineRouter.delete(
|
|
'/delete',
|
|
dependencies=[Depends(AuthPermission(['module_monitor:online:delete']))],
|
|
summary="强制下线",
|
|
description="强制下线"
|
|
)
|
|
async def delete_online_controller(
|
|
session_id: str = Body(..., description="会话编号"),
|
|
redis: Redis = Depends(redis_getter),
|
|
)->JSONResponse:
|
|
"""
|
|
强制下线指定在线用户
|
|
|
|
参数:
|
|
- session_id (str): 在线用户会话ID。
|
|
- redis (Redis): Redis异步客户端实例。
|
|
|
|
返回:
|
|
- JSONResponse: 包含操作结果的JSON响应。
|
|
"""
|
|
is_ok = await OnlineService.delete_online_service(redis=redis, session_id=session_id)
|
|
if is_ok:
|
|
logger.info("强制下线成功")
|
|
return SuccessResponse(msg="强制下线成功")
|
|
else:
|
|
logger.info("强制下线失败")
|
|
return ErrorResponse(msg="强制下线失败")
|
|
|
|
@OnlineRouter.delete(
|
|
'/clear',
|
|
dependencies=[Depends(AuthPermission(['module_monitor:online:delete']))],
|
|
summary="清除所有在线用户",
|
|
description="清除所有在线用户"
|
|
)
|
|
async def clear_online_controller(
|
|
redis: Redis = Depends(redis_getter),
|
|
)->JSONResponse:
|
|
"""
|
|
清除所有在线用户
|
|
|
|
参数:
|
|
- redis (Redis): Redis异步客户端实例。
|
|
|
|
返回:
|
|
- JSONResponse: 包含操作结果的JSON响应。
|
|
"""
|
|
is_ok = await OnlineService.clear_online_service(redis=redis)
|
|
if is_ok:
|
|
logger.info("清除所有在线用户成功")
|
|
return SuccessResponse(msg="清除所有在线用户成功")
|
|
else:
|
|
logger.info("清除所有在线用户失败")
|
|
return ErrorResponse(msg="清除所有在线用户失败") |