mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-10-13 20:51:09 +00:00
refactor: 小程序端代码瘦身与功能补齐
- 清理冗余代码与配置: - 删除废弃的 mock 适配器、通用 mock 模块与示例API - 移除冗余的 ts 类型文件与 patch 目录 - 简化 tsconfig 组件路径配置 - 新增功能与优化: - 新增全局水印组件与水印配置 - 接入微信小程序登录/手机号登录 - 新增系统参数配置 store 与国际化支持 - 新增 markdown 渲染插件与样式配置 - 优化 pinia 持久化逻辑修复 - 新增微信小程序 access_token 缓存配置 - 修复登录趋势数据结构定义 - 优化全局请求防抖重定向逻辑 - 页面与路由优化: - 补全引导页首次启动逻辑 - 统一页面底部安全区适配 - 更新页面路由类型声明 - 代码质量优化: - 移除无用 console.log 调试代码 - 修复全局弹窗重复重登录问题 - 优化 mp-html 组件配置
This commit is contained in:
@@ -26,6 +26,14 @@ class RecentLoginItem(BaseModel):
|
||||
login_location: str | None = None
|
||||
|
||||
|
||||
class LoginTrendItem(BaseModel):
|
||||
"""登录趋势(按天聚合)"""
|
||||
day: str # 日期 YYYY-MM-DD
|
||||
logins: int = 0 # 登录次数
|
||||
unique_users: int = 0 # 独立用户数
|
||||
new_users: int = 0 # 当日新增用户数
|
||||
|
||||
|
||||
class DashboardStatsSchema(BaseModel):
|
||||
"""仪表盘统计数据"""
|
||||
online_users: int = 0
|
||||
@@ -33,4 +41,5 @@ class DashboardStatsSchema(BaseModel):
|
||||
today_login_count: int = 0
|
||||
today_unique_users: int = 0
|
||||
week_user_created: int = 0
|
||||
login_trend: list[LoginTrendItem] = []
|
||||
recent_logins: list[RecentLoginItem] = []
|
||||
|
||||
@@ -12,7 +12,7 @@ from app.core.logger import logger
|
||||
from app.core.redis_crud import RedisCURD
|
||||
from app.core.security import decode_access_token
|
||||
|
||||
from .schema import DashboardStatsSchema, OnlineQueryParam, RecentLoginItem
|
||||
from .schema import DashboardStatsSchema, LoginTrendItem, OnlineQueryParam, RecentLoginItem
|
||||
|
||||
|
||||
class OnlineService:
|
||||
@@ -129,12 +129,76 @@ class OnlineService:
|
||||
for r in recent_rows
|
||||
]
|
||||
|
||||
# 近 7 天登录趋势:按天聚合登录次数 / 独立用户 / 新增用户(含今天,共 7 天)
|
||||
login_trend = await OnlineService._build_login_trend(db, today_start=today_start, week_start=week_start)
|
||||
|
||||
result = DashboardStatsSchema(
|
||||
online_users=online_count,
|
||||
total_users=user_count,
|
||||
today_login_count=today_login_count,
|
||||
today_unique_users=today_unique_count,
|
||||
week_user_created=user_week_count,
|
||||
login_trend=login_trend,
|
||||
recent_logins=recent_logins,
|
||||
)
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
async def _build_login_trend(db: AsyncSession, *, today_start: datetime, week_start: datetime) -> list[LoginTrendItem]:
|
||||
"""构建近 7 天登录趋势(含今天,共 7 天,按日期倒序填充缺口为 0)。
|
||||
|
||||
参数:
|
||||
- db (AsyncSession): 数据库会话
|
||||
- today_start (datetime): 今天零点
|
||||
- week_start (datetime): 7 天前零点
|
||||
|
||||
返回:
|
||||
- list[LoginTrendItem]: 按日期升序的 7 天趋势
|
||||
"""
|
||||
# 查询 7 天内每天登录次数与独立用户数
|
||||
login_sql = (
|
||||
select(
|
||||
func.date(LoginLogModel.created_time).label("day"),
|
||||
func.count().label("logins"),
|
||||
func.count(func.distinct(LoginLogModel.username)).label("unique_users"),
|
||||
)
|
||||
.where(
|
||||
LoginLogModel.is_deleted.is_(False),
|
||||
LoginLogModel.status == 1, # 仅统计成功登录
|
||||
LoginLogModel.created_time >= week_start,
|
||||
)
|
||||
.group_by(func.date(LoginLogModel.created_time))
|
||||
)
|
||||
login_rows = (await db.execute(login_sql)).all()
|
||||
login_map = {str(r.day): (r.logins, r.unique_users) for r in login_rows}
|
||||
|
||||
# 查询 7 天内每天新增用户数
|
||||
new_user_sql = (
|
||||
select(
|
||||
func.date(UserModel.created_time).label("day"),
|
||||
func.count().label("new_users"),
|
||||
)
|
||||
.where(
|
||||
UserModel.is_deleted.is_(False),
|
||||
UserModel.created_time >= week_start,
|
||||
)
|
||||
.group_by(func.date(UserModel.created_time))
|
||||
)
|
||||
new_rows = (await db.execute(new_user_sql)).all()
|
||||
new_map = {str(r.day): r.new_users for r in new_rows}
|
||||
|
||||
# 生成连续 7 天日期序列(升序),缺口补 0
|
||||
trend: list[LoginTrendItem] = []
|
||||
for offset in range(7):
|
||||
d = week_start.date() + timedelta(days=offset)
|
||||
day_str = d.isoformat()
|
||||
logins, unique_users = login_map.get(day_str, (0, 0))
|
||||
trend.append(
|
||||
LoginTrendItem(
|
||||
day=day_str,
|
||||
logins=int(logins or 0),
|
||||
unique_users=int(unique_users or 0),
|
||||
new_users=int(new_map.get(day_str, 0) or 0),
|
||||
)
|
||||
)
|
||||
return trend
|
||||
|
||||
Reference in New Issue
Block a user