refactor: 整合仪表盘功能到监控模块,清理冗余代码

- 移除原监控仪表盘独立模块,将相关功能合并到在线监控模块
- 重构租户配置字段名,统一使用logo_url和name替代tenant_logo/tenant_name
- 优化搜索工具函数,移除重复导入
- 调整参数配置模型字段长度限制,移除config_value的max_length约束
- 清理冗余的常量定义和导入语句
- 修复批量状态设置接口的redis依赖注入
- 增强OAuth登录安全性,添加租户默认归属和state一次性消费
- 优化资源目录缓存逻辑,减少重复计算
- 新增API Token模块基础框架
- 完善用户token版本管理,支持主动失效JWT
- 调整AI模型配置缓存过期时间
- 修复菜单类型字段索引,提升查询性能
- 简化前端刷新token调用逻辑
- 新增滑块验证完成接口和忘记密码验证码校验
- 调整系统配置默认值,添加操作日志保留天数和接口白名单配置
- 限制Mock支付回调仅在开发环境可用
- 重构websocket认证方式,支持更安全的subprotocol传参
This commit is contained in:
zhangtao
2026-07-13 01:14:20 +08:00
parent 6a5f8cf0dd
commit cf88ab8897
102 changed files with 4078 additions and 3777 deletions
@@ -1,7 +1,6 @@
from fastapi import APIRouter
from .cache.controller import CacheRouter
from .dashboard.controller import MonitorDashboardRouter
from .online.controller import OnlineRouter
from .resource.controller import ResourceRouter
from .server.controller import ServerRouter
@@ -9,7 +8,6 @@ from .server.controller import ServerRouter
monitor_router = APIRouter(prefix="/monitor")
monitor_router.include_router(CacheRouter)
monitor_router.include_router(MonitorDashboardRouter)
monitor_router.include_router(OnlineRouter)
monitor_router.include_router(ResourceRouter)
monitor_router.include_router(ServerRouter)
@@ -1,3 +0,0 @@
from .controller import MonitorDashboardRouter
__all__ = ["MonitorDashboardRouter"]
@@ -1,35 +0,0 @@
from typing import Annotated
from fastapi import APIRouter, Depends, Security
from fastapi.responses import JSONResponse
from redis.asyncio.client import Redis
from sqlalchemy.ext.asyncio import AsyncSession
from app.common.response import ResponseSchema, SuccessResponse
from app.core.base_schema import AuthSchema
from app.core.dependencies import AuthPermission, db_getter, redis_getter
from app.core.router_class import OperationLogRoute
from .schema import DashboardStatsSchema
from .service import MonitorDashboardService
MonitorDashboardRouter = APIRouter(
route_class=OperationLogRoute,
prefix="/dashboard",
tags=["仪表盘"],
)
@MonitorDashboardRouter.get(
"/stats",
summary="获取仪表盘统计数据",
response_model=ResponseSchema[DashboardStatsSchema],
)
async def get_dashboard_stats_controller(
db: Annotated[AsyncSession, Depends(db_getter)],
redis: Annotated[Redis, Depends(redis_getter)],
auth: Annotated[AuthSchema, Security(AuthPermission(["module_monitor:dashboard:query"]))],
) -> JSONResponse:
"""获取首页仪表盘统计数据(在线用户、用户数、租户数、订单数、登录统计等)"""
data = await MonitorDashboardService.get_dashboard_stats(db=db, redis=redis, auth=auth)
return SuccessResponse(data=data, msg="获取仪表盘统计成功")
@@ -1,26 +0,0 @@
from datetime import datetime
from pydantic import BaseModel
class RecentLoginItem(BaseModel):
"""最近登录记录"""
username: str
status: int # 1:成功 2:失败
login_time: datetime
login_ip: str | None = None
login_location: str | None = None
class DashboardStatsSchema(BaseModel):
"""首页仪表盘统计数据"""
online_users: int = 0
total_users: int = 0
total_tenants: int = 0
total_orders: int = 0
today_login_count: int = 0 # 今日登录人次
today_unique_users: int = 0 # 今日登录人数
week_user_created: int = 0 # 本周新增用户
week_tenant_created: int = 0 # 本周新增租户
paid_orders: int = 0 # 已支付订单数
recent_logins: list[RecentLoginItem] = []
@@ -1,114 +0,0 @@
from datetime import date, datetime, timedelta
from redis.asyncio.client import Redis
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.v1.module_monitor.online.service import OnlineService
from app.core.base_schema import AuthSchema
from .schema import DashboardStatsSchema, RecentLoginItem
class MonitorDashboardService:
"""仪表盘统计服务"""
@staticmethod
async def get_dashboard_stats(
db: AsyncSession,
redis: Redis,
auth: AuthSchema,
) -> DashboardStatsSchema:
from app.api.v1.module_platform.order.model import OrderModel
from app.api.v1.module_platform.tenant.model import TenantModel
from app.api.v1.module_system.log.model import LoginLogModel
from app.api.v1.module_system.user.model import UserModel
today_start = datetime.combine(date.today(), datetime.min.time())
week_start = today_start - timedelta(days=7)
# 在线用户
online_list = await OnlineService.get_online_list(redis)
online_count = len(online_list)
# 用户统计
users_sql = select(func.count()).select_from(UserModel).where(UserModel.is_deleted.is_(False))
user_count = (await db.execute(users_sql)).scalar() or 0
users_week_sql = (
select(func.count()).select_from(UserModel)
.where(UserModel.is_deleted.is_(False), UserModel.created_time >= week_start)
)
user_week_count = (await db.execute(users_week_sql)).scalar() or 0
# 租户统计
tenants_sql = select(func.count()).select_from(TenantModel).where(TenantModel.is_deleted.is_(False))
tenant_count = (await db.execute(tenants_sql)).scalar() or 0
tenants_week_sql = (
select(func.count()).select_from(TenantModel)
.where(TenantModel.is_deleted.is_(False), TenantModel.created_time >= week_start)
)
tenant_week_count = (await db.execute(tenants_week_sql)).scalar() or 0
# 订单统计
orders_sql = select(func.count()).select_from(OrderModel).where(OrderModel.is_deleted.is_(False))
order_count = (await db.execute(orders_sql)).scalar() or 0
paid_sql = (
select(func.count()).select_from(OrderModel)
.where(OrderModel.is_deleted.is_(False), OrderModel.status == 1)
)
paid_count = (await db.execute(paid_sql)).scalar() or 0
# 今日登录统计
today_login_sql = (
select(func.count()).select_from(LoginLogModel)
.where(LoginLogModel.created_time >= today_start)
)
today_login_count = (await db.execute(today_login_sql)).scalar() or 0
today_unique_sql = (
select(func.count(func.distinct(LoginLogModel.username)))
.select_from(LoginLogModel)
.where(LoginLogModel.created_time >= today_start)
)
today_unique_count = (await db.execute(today_unique_sql)).scalar() or 0
# 最近登录记录(最近 10 条)
recent_stmt = (
select(
LoginLogModel.username,
LoginLogModel.status,
LoginLogModel.created_time,
LoginLogModel.login_ip,
LoginLogModel.login_location,
)
.where(LoginLogModel.is_deleted.is_(False))
.order_by(LoginLogModel.created_time.desc())
.limit(10)
)
recent_rows = (await db.execute(recent_stmt)).all()
recent_logins = [
RecentLoginItem(
username=r.username,
status=r.status,
login_time=r.created_time,
login_ip=r.login_ip,
login_location=r.login_location,
)
for r in recent_rows
]
return DashboardStatsSchema(
online_users=online_count,
total_users=user_count,
total_tenants=tenant_count,
total_orders=order_count,
today_login_count=today_login_count,
today_unique_users=today_unique_count,
week_user_created=user_week_count,
week_tenant_created=tenant_week_count,
paid_orders=paid_count,
recent_logins=recent_logins,
)
@@ -2,19 +2,24 @@ from typing import Annotated
from fastapi import APIRouter, Body, Depends, Query, Security
from fastapi.responses import JSONResponse
from fastapi_cache import FastAPICache
from fastapi_cache.decorator import cache
from redis.asyncio.client import Redis
from sqlalchemy.ext.asyncio import AsyncSession
from app.common.request import PaginationService
from app.common.response import ResponseSchema, SuccessResponse
from app.core.base_schema import PaginationQueryParam
from app.core.dependencies import AuthPermission, redis_getter
from app.core.base_schema import AuthSchema, PaginationQueryParam
from app.core.dependencies import AuthPermission, db_getter, get_current_user, redis_getter
from app.core.router_class import OperationLogRoute
from .schema import OnlineOutSchema, OnlineQueryParam
from .schema import DashboardStatsSchema, OnlineOutSchema, OnlineQueryParam
from .service import OnlineService
OnlineRouter = APIRouter(route_class=OperationLogRoute, prefix="/online", tags=["在线用户"])
_STATS_NS = "online_stats"
@OnlineRouter.get("/list", summary="获取在线用户列表", response_model=ResponseSchema[list[OnlineOutSchema]], dependencies=[Security(AuthPermission(["module_monitor:online:query"]))])
async def get_online_list_controller(
@@ -37,6 +42,7 @@ async def delete_online_controller(
redis: Annotated[Redis, Depends(redis_getter)],
) -> JSONResponse:
await OnlineService.delete_online(redis=redis, session_id=session_id)
await FastAPICache.clear(namespace=_STATS_NS)
return SuccessResponse(msg="强制下线成功")
@@ -45,4 +51,16 @@ async def clear_online_controller(
redis: Annotated[Redis, Depends(redis_getter)],
) -> JSONResponse:
await OnlineService.clear_online(redis=redis)
await FastAPICache.clear(namespace=_STATS_NS)
return SuccessResponse(msg="清除所有在线用户成功")
@OnlineRouter.get("/stats", summary="获取仪表盘统计数据", response_model=ResponseSchema[DashboardStatsSchema])
@cache(expire=15, namespace=_STATS_NS)
async def get_dashboard_stats_controller(
db: Annotated[AsyncSession, Depends(db_getter)],
redis: Annotated[Redis, Depends(redis_getter)],
_auth: Annotated[AuthSchema, Depends(get_current_user)],
) -> JSONResponse:
data = await OnlineService.get_dashboard_stats(db=db, redis=redis)
return SuccessResponse(data=data, msg="获取仪表盘统计成功")
@@ -1,28 +1,13 @@
from datetime import datetime
from pydantic import BaseModel, Field, model_validator
from app.common.enums import QueueEnum
from app.core.validator import DateTimeStr
from app.core.base_schema import SessionInfoSchema
class OnlineOutSchema(BaseModel):
"""在线用户对应pydantic模型
"""
name: str = Field(..., description="用户名称")
session_id: str = Field(..., description="会话编号")
user_id: int = Field(..., description="用户ID")
tenant_id: int = Field(..., description="租户ID")
tenant_status: int = Field(default=0, description="租户状态(0:正常 1:欠费 2:试用 3:冻结 4:注销)")
is_superuser: bool = Field(default=False, description="是否为超级管理员")
user_status: int = Field(default=0, description="用户状态(0:启用 1:停用)")
user_name: str = Field(..., description="用户名")
permissions: list[str] = Field(default_factory=list, description="用户权限列表")
ipaddr: str | None = Field(default=None, description="登陆IP地址")
login_location: str | None = Field(default=None, description="登录所属地")
os: str | None = Field(default=None, description="操作系统")
browser: str | None = Field(default=None, description="浏览器")
login_time: DateTimeStr | None = Field(default=None, description="登录时间")
login_type: str | None = Field(default=None, description="登录类型 PC端 | 移动端")
class OnlineOutSchema(SessionInfoSchema):
"""在线用户响应模型 — ``SessionInfoSchema`` 的公开子集。"""
class OnlineQueryParam(BaseModel):
@@ -41,3 +26,26 @@ class OnlineQueryParam(BaseModel):
if isinstance(self.login_location, str):
self.login_location = (QueueEnum.like.value, self.login_location)
return self
class RecentLoginItem(BaseModel):
"""最近登录记录"""
username: str
status: int
login_time: datetime
login_ip: str | None = None
login_location: str | None = None
class DashboardStatsSchema(BaseModel):
"""仪表盘统计数据"""
online_users: int = 0
total_users: int = 0
total_tenants: int = 0
total_orders: int = 0
today_login_count: int = 0
today_unique_users: int = 0
week_user_created: int = 0
week_tenant_created: int = 0
paid_orders: int = 0
recent_logins: list[RecentLoginItem] = []
@@ -1,13 +1,20 @@
import json
from datetime import date, datetime, timedelta
from redis.asyncio.client import Redis
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.v1.module_platform.order.model import OrderModel
from app.api.v1.module_platform.tenant.model import TenantModel
from app.api.v1.module_system.log.model import LoginLogModel
from app.api.v1.module_system.user.model import UserModel
from app.common.enums import RedisInitKeyConfig
from app.core.logger import logger
from app.core.redis_crud import RedisCURD
from app.core.security import decode_access_token
from .schema import OnlineQueryParam
from .schema import DashboardStatsSchema, OnlineQueryParam, RecentLoginItem
class OnlineService:
@@ -15,7 +22,7 @@ class OnlineService:
@staticmethod
async def get_online_list(redis: Redis, search: OnlineQueryParam | None = None) -> list[dict]:
keys = await RedisCURD(redis).get_keys(f"{RedisInitKeyConfig.ACCESS_TOKEN.key}:*")
keys = await RedisCURD(redis).scan_keys(f"{RedisInitKeyConfig.ACCESS_TOKEN.key}:*")
tokens = await RedisCURD(redis).mget(keys)
online_users = []
@@ -68,3 +75,79 @@ class OnlineService:
await RedisCURD(redis).clear(f"{RedisInitKeyConfig.REFRESH_TOKEN.key}:*")
await RedisCURD(redis).clear(f"{RedisInitKeyConfig.USER_SESSION.key}:*")
logger.info("清除所有在线用户会话成功")
@staticmethod
async def get_dashboard_stats(db: AsyncSession, redis: Redis) -> DashboardStatsSchema:
"""获取仪表盘统计数据"""
today_start = datetime.combine(date.today(), datetime.min.time())
week_start = today_start - timedelta(days=7)
online_count = len(await OnlineService.get_online_list(redis))
users_sql = select(func.count()).select_from(UserModel).where(UserModel.is_deleted.is_(False))
user_count = (await db.execute(users_sql)).scalar() or 0
users_week_sql = (
select(func.count()).select_from(UserModel)
.where(UserModel.is_deleted.is_(False), UserModel.created_time >= week_start)
)
user_week_count = (await db.execute(users_week_sql)).scalar() or 0
tenants_sql = select(func.count()).select_from(TenantModel).where(TenantModel.is_deleted.is_(False))
tenant_count = (await db.execute(tenants_sql)).scalar() or 0
tenants_week_sql = (
select(func.count()).select_from(TenantModel)
.where(TenantModel.is_deleted.is_(False), TenantModel.created_time >= week_start)
)
tenant_week_count = (await db.execute(tenants_week_sql)).scalar() or 0
orders_sql = select(func.count()).select_from(OrderModel).where(OrderModel.is_deleted.is_(False))
order_count = (await db.execute(orders_sql)).scalar() or 0
paid_sql = (
select(func.count()).select_from(OrderModel)
.where(OrderModel.is_deleted.is_(False), OrderModel.status == 1)
)
paid_count = (await db.execute(paid_sql)).scalar() or 0
today_login_sql = (
select(func.count()).select_from(LoginLogModel)
.where(LoginLogModel.created_time >= today_start)
)
today_login_count = (await db.execute(today_login_sql)).scalar() or 0
today_unique_sql = (
select(func.count(func.distinct(LoginLogModel.username)))
.select_from(LoginLogModel)
.where(LoginLogModel.created_time >= today_start)
)
today_unique_count = (await db.execute(today_unique_sql)).scalar() or 0
recent_stmt = (
select(LoginLogModel.username, LoginLogModel.status, LoginLogModel.created_time,
LoginLogModel.login_ip, LoginLogModel.login_location)
.where(LoginLogModel.is_deleted.is_(False))
.order_by(LoginLogModel.created_time.desc())
.limit(10)
)
recent_rows = (await db.execute(recent_stmt)).all()
recent_logins = [
RecentLoginItem(username=r.username, status=r.status, login_time=r.created_time,
login_ip=r.login_ip, login_location=r.login_location)
for r in recent_rows
]
result = DashboardStatsSchema(
online_users=online_count,
total_users=user_count,
total_tenants=tenant_count,
total_orders=order_count,
today_login_count=today_login_count,
today_unique_users=today_unique_count,
week_user_created=user_week_count,
week_tenant_created=tenant_week_count,
paid_orders=paid_count,
recent_logins=recent_logins,
)
return result
@@ -1,4 +1,5 @@
import ast
import json
import os
import re
import shutil
@@ -7,6 +8,8 @@ from datetime import datetime
from pathlib import Path
from urllib.parse import urlparse
from fastapi_cache import FastAPICache
from app.config.setting import settings
from app.core.exceptions import CustomException
from app.core.logger import logger
@@ -215,6 +218,14 @@ class ResourceService:
include_hidden: bool = False,
base_url: str | None = None,
) -> ResourceDirectorySchema:
# 进程级缓存(目录内容变更极低频,30s 过期)
_RESOURCE_DIR_TTL = 30
cache_key = f"resource_dir:{path or 'root'}:{include_hidden}"
_backend = FastAPICache.get_backend()
cached = await _backend.get(cache_key)
if cached:
return ResourceDirectorySchema(**json.loads(cached.decode()))
try:
if path is None:
safe_path = ResourceService._get_resource_root()
@@ -253,7 +264,7 @@ class ResourceService:
except PermissionError:
raise CustomException(msg="没有权限访问此目录")
return ResourceDirectorySchema(
result = ResourceDirectorySchema(
path=display_path,
name=os.path.basename(safe_path),
items=items,
@@ -261,6 +272,8 @@ class ResourceService:
total_dirs=total_dirs,
total_size=total_size,
)
await _backend.set(cache_key, json.dumps(result.model_dump()).encode(), expire=_RESOURCE_DIR_TTL)
return result
except CustomException:
raise