更新uniapp和platform
This commit is contained in:
1 parent
31ae07175d
commit
7f9a66da0f
47 files changed
+5207
-1974
No files matched your search
@@ -0,0 +1,212 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"server/models"
|
||||
"server/pkg/identity"
|
||||
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// PlatformAnalyticsController 平台端数据统计(真实数据,不做任何模拟)
|
||||
type PlatformAnalyticsController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *PlatformAnalyticsController) analyticsFail(httpStatus, bizCode int, msg string) {
|
||||
c.Ctx.Output.SetStatus(httpStatus)
|
||||
c.Data["json"] = map[string]any{"code": bizCode, "msg": msg}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// Users GET /platform/analytics/users
|
||||
//
|
||||
// 统计租户端(backend)真实的用户与租户数据:
|
||||
// - 用户:yz_system_tenant_user(不含已删除)
|
||||
// - 租户:yz_system_tenant(不含已删除)
|
||||
// - 活跃:yz_system_login_log 近 30 天成功登录的去重用户 / 今日登录次数
|
||||
//
|
||||
// 返回:总量、今日/本月新增、近 14 天新增趋势、各租户用户数分布。
|
||||
func (c *PlatformAnalyticsController) Users() {
|
||||
if _, err := requirePlatform(&c.Controller); err != nil {
|
||||
code := identity.Status(err)
|
||||
c.analyticsFail(code, code, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
||||
lastMonthStart := monthStart.AddDate(0, -1, 0)
|
||||
trendStart := today.AddDate(0, 0, -13) // 含今天共 14 天
|
||||
activeSince := now.AddDate(0, 0, -30)
|
||||
|
||||
userQuery := func() orm.QuerySeter {
|
||||
return models.Orm.QueryTable(new(models.SystemTenantUser)).
|
||||
Filter("delete_time__isnull", true)
|
||||
}
|
||||
tenantQuery := func() orm.QuerySeter {
|
||||
return models.Orm.QueryTable(new(models.SystemTenant)).
|
||||
Filter("delete_time__isnull", true)
|
||||
}
|
||||
|
||||
// ===== 用户 =====
|
||||
totalUsers, _ := userQuery().Count()
|
||||
activeUsers, _ := userQuery().Filter("status", 1).Count()
|
||||
todayNewUsers, _ := userQuery().Filter("create_time__gte", today).Count()
|
||||
monthNewUsers, _ := userQuery().Filter("create_time__gte", monthStart).Count()
|
||||
lastMonthNewUsers, _ := userQuery().
|
||||
Filter("create_time__gte", lastMonthStart).
|
||||
Filter("create_time__lt", monthStart).
|
||||
Count()
|
||||
|
||||
// ===== 租户 =====
|
||||
totalTenants, _ := tenantQuery().Count()
|
||||
activeTenants, _ := tenantQuery().Filter("status", 1).Count()
|
||||
todayNewTenants, _ := tenantQuery().Filter("create_time__gte", today).Count()
|
||||
monthNewTenants, _ := tenantQuery().Filter("create_time__gte", monthStart).Count()
|
||||
|
||||
// ===== 活跃用户(近 30 天成功登录)=====
|
||||
var loginLogs []models.SystemLoginLog
|
||||
_, _ = models.Orm.QueryTable(new(models.SystemLoginLog)).
|
||||
Filter("create_time__gte", activeSince).
|
||||
Filter("status", 1).
|
||||
All(&loginLogs)
|
||||
|
||||
activeUserSet := make(map[uint64]struct{}, len(loginLogs))
|
||||
todayLogins := 0
|
||||
for _, log := range loginLogs {
|
||||
if log.UserID > 0 {
|
||||
activeUserSet[log.UserID] = struct{}{}
|
||||
}
|
||||
if !log.CreateTime.Before(today) {
|
||||
todayLogins++
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 近 14 天新增趋势 =====
|
||||
const trendDays = 14
|
||||
days := make([]string, trendDays)
|
||||
dayUsers := make([]int, trendDays)
|
||||
dayTenants := make([]int, trendDays)
|
||||
dayIndex := make(map[string]int, trendDays)
|
||||
for i := 0; i < trendDays; i++ {
|
||||
d := trendStart.AddDate(0, 0, i)
|
||||
key := d.Format("2006-01-02")
|
||||
dayIndex[key] = i
|
||||
days[i] = d.Format("01-02")
|
||||
}
|
||||
|
||||
var recentUsers []models.SystemTenantUser
|
||||
_, _ = userQuery().Filter("create_time__gte", trendStart).All(&recentUsers)
|
||||
for _, u := range recentUsers {
|
||||
if i, ok := dayIndex[u.CreateTime.Format("2006-01-02")]; ok {
|
||||
dayUsers[i]++
|
||||
}
|
||||
}
|
||||
|
||||
var recentTenants []models.SystemTenant
|
||||
_, _ = tenantQuery().Filter("create_time__gte", trendStart).All(&recentTenants)
|
||||
for _, t := range recentTenants {
|
||||
if i, ok := dayIndex[t.CreateTime.Format("2006-01-02")]; ok {
|
||||
dayTenants[i]++
|
||||
}
|
||||
}
|
||||
|
||||
// ===== 各租户用户数分布(Top 8 + 其他)=====
|
||||
var allUsers []models.SystemTenantUser
|
||||
_, _ = userQuery().All(&allUsers)
|
||||
countByTid := make(map[uint64]int, len(allUsers))
|
||||
for _, u := range allUsers {
|
||||
countByTid[u.Tid]++
|
||||
}
|
||||
|
||||
var allTenants []models.SystemTenant
|
||||
_, _ = tenantQuery().All(&allTenants)
|
||||
tenantName := make(map[uint64]string, len(allTenants))
|
||||
for _, t := range allTenants {
|
||||
tenantName[t.ID] = t.TenantName
|
||||
}
|
||||
|
||||
type tenantUserCount struct {
|
||||
ID uint64
|
||||
Name string
|
||||
Value int
|
||||
}
|
||||
dist := make([]tenantUserCount, 0, len(countByTid))
|
||||
for tid, count := range countByTid {
|
||||
name := tenantName[tid]
|
||||
if name == "" {
|
||||
name = "未归属租户"
|
||||
}
|
||||
dist = append(dist, tenantUserCount{ID: tid, Name: name, Value: count})
|
||||
}
|
||||
sort.Slice(dist, func(i, j int) bool {
|
||||
if dist[i].Value != dist[j].Value {
|
||||
return dist[i].Value > dist[j].Value
|
||||
}
|
||||
return dist[i].ID < dist[j].ID
|
||||
})
|
||||
|
||||
const topN = 8
|
||||
tenantUserTop := make([]map[string]any, 0, topN+1)
|
||||
otherCount := 0
|
||||
for i, item := range dist {
|
||||
if i < topN {
|
||||
tenantUserTop = append(tenantUserTop, map[string]any{
|
||||
"name": item.Name,
|
||||
"value": item.Value,
|
||||
})
|
||||
} else {
|
||||
otherCount += item.Value
|
||||
}
|
||||
}
|
||||
if otherCount > 0 {
|
||||
tenantUserTop = append(tenantUserTop, map[string]any{
|
||||
"name": "其他",
|
||||
"value": otherCount,
|
||||
})
|
||||
}
|
||||
|
||||
// ===== 环比(本月新增 vs 上月新增)=====
|
||||
growthPct := 0.0
|
||||
growthUp := true
|
||||
if lastMonthNewUsers > 0 {
|
||||
growthPct = float64(monthNewUsers-lastMonthNewUsers) / float64(lastMonthNewUsers) * 100
|
||||
growthUp = monthNewUsers >= lastMonthNewUsers
|
||||
} else if monthNewUsers > 0 {
|
||||
growthPct = 100
|
||||
}
|
||||
growthPct = math.Round(math.Abs(growthPct)*10) / 10
|
||||
|
||||
c.Data["json"] = map[string]any{
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": map[string]any{
|
||||
"totalUsers": totalUsers,
|
||||
"activeUsers": activeUsers,
|
||||
"todayNewUsers": todayNewUsers,
|
||||
"monthNewUsers": monthNewUsers,
|
||||
"lastMonthNewUsers": lastMonthNewUsers,
|
||||
"totalTenants": totalTenants,
|
||||
"activeTenants": activeTenants,
|
||||
"todayNewTenants": todayNewTenants,
|
||||
"monthNewTenants": monthNewTenants,
|
||||
"activeUsers30d": len(activeUserSet),
|
||||
"todayLogins": todayLogins,
|
||||
"userGrowthPct": growthPct,
|
||||
"userGrowthUp": growthUp,
|
||||
"tenantUserTop": tenantUserTop,
|
||||
"trend": map[string]any{
|
||||
"days": days,
|
||||
"users": dayUsers,
|
||||
"tenants": dayTenants,
|
||||
},
|
||||
},
|
||||
}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
Reference in new issue
Block a user