feat: 实现客户端实例上报功能
- 新增 ClientInstance 模型,记录客户端 IP 与配置项的关联 - 配置拉取接口(fetchAll/fetchChanged)自动记录客户端信息 - 配置列表页面展示每个配置的在线客户端数量 - 点击可查看具体 IP 地址、拉取方式、最后拉取时间 - 超过 5 分钟未拉取的记录自动清理 - 注释掉 /api/ping 接口
This commit is contained in:
@@ -80,9 +80,31 @@ func (h *ConfigHandler) List(c *gin.Context) {
|
||||
total, _ := dao.Engine.Where(whereClause).Count(&model.AppConfig{})
|
||||
dao.Engine.Where(whereClause).Limit(req.PageSize, (req.PageNo-1)*req.PageSize).Find(&configs)
|
||||
|
||||
// build app names
|
||||
// build app names and client counts
|
||||
configIds := make([]int, 0, len(configs))
|
||||
for i := range configs {
|
||||
configs[i].AppNames = h.buildAppNames(&configs[i])
|
||||
configIds = append(configIds, configs[i].Id)
|
||||
}
|
||||
// batch query client counts
|
||||
if len(configIds) > 0 {
|
||||
type countResult struct {
|
||||
AppConfigId int `xorm:"app_config_id"`
|
||||
Cnt int `xorm:"cnt"`
|
||||
}
|
||||
var results []countResult
|
||||
dao.Engine.Table("client_instance").
|
||||
Select("app_config_id, COUNT(DISTINCT ip) as cnt").
|
||||
In("app_config_id", configIds).
|
||||
GroupBy("app_config_id").
|
||||
Find(&results)
|
||||
countMap := make(map[int]int, len(results))
|
||||
for _, r := range results {
|
||||
countMap[r.AppConfigId] = r.Cnt
|
||||
}
|
||||
for i := range configs {
|
||||
configs[i].ClientCount = countMap[configs[i].Id]
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, model.PageResult{PageNo: req.PageNo, PageSize: req.PageSize, Total: total, Data: configs})
|
||||
|
||||
Reference in New Issue
Block a user