Appearance
仪表盘模块
仪表盘模块(dashboard)提供管理后台的统计数据概览,包括租户、用户、角色计数及 30 天增长率、今日活跃用户数。
架构特点
该模块采用轻量级设计,仅有 interfaces 层,直接通过 *gorm.DB 查询数据库,不经过 domain/application/infrastructure 分层。
modules/dashboard/
└── interfaces/ # Handler(直接查询 DB)、路由注册统计数据
StatsResponse
| 字段 | 类型 | 说明 |
|---|---|---|
| TenantCount | int64 | 租户总数 |
| UserCount | int64 | 用户总数 |
| RoleCount | int64 | 角色总数 |
| OnlineCount | int64 | 在线人数 |
| TenantGrowth | float64 | 租户 30 天增长率(%) |
| UserGrowth | float64 | 用户 30 天增长率(%) |
| RoleGrowth | float64 | 角色 30 天增长率(%) |
| TodayActiveUsers | int64 | 今日活跃用户数 |
API 接口
| 方法 | 路径 | 权限 | 说明 |
|---|---|---|---|
| GET | /api/v1/dashboard/stats | dashboard.read | 获取统计数据 |
响应示例
json
// GET /api/v1/dashboard/stats
// Response 200
{
"tenantCount": 0,
"userCount": 156,
"roleCount": 12,
"onlineCount": 0,
"tenantGrowth": 0,
"userGrowth": 15.5,
"roleGrowth": 0,
"todayActiveUsers": 23
}查询逻辑
- UserCount:
SELECT COUNT(*) FROM users WHERE tenant_id = ? - RoleCount:
SELECT COUNT(*) FROM roles WHERE tenant_id = ? - UserGrowth / RoleGrowth: 对比当前数量与 30 天前的数量,计算增长率百分比
- TodayActiveUsers:
SELECT COUNT(*) FROM users WHERE tenant_id = ? AND last_login >= 今日零点 - TenantCount / OnlineCount / TenantGrowth: 当前硬编码为 0(预留扩展)
增长率计算
go
func calcGrowth(current, previous int64) float64previous == 0 && current > 0→ 返回 100.0previous == 0 && current == 0→ 返回 0- 其他 →
(current - previous) / previous * 100
依赖注入
go
fx.Module("dashboard",
fx.Provide(
func(db *gorm.DB) *Handler {
return NewHandler(db)
},
),
)直接注入 *gorm.DB,无中间层。

