增加登录器客户端心跳
This commit is contained in:
@@ -178,11 +178,19 @@ func (c *PlatformCursorEquipmentController) rowToMap(row *models.PlatformCursorE
|
||||
}
|
||||
}
|
||||
|
||||
// 在线状态计算:若最后心跳时间在 5 分钟内,则认为在线
|
||||
isOnline := false
|
||||
if row.LastHeartbeatAt != nil && time.Since(*row.LastHeartbeatAt) < 5*time.Minute {
|
||||
isOnline = true
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"id": row.ID,
|
||||
"deviceInfo": row.DeviceInfo,
|
||||
"machineCode": row.MachineCode,
|
||||
"status": row.Status,
|
||||
"isOnline": isOnline,
|
||||
"lastHeartbeatAt": row.LastHeartbeatAt,
|
||||
"system": row.System,
|
||||
"os": row.System,
|
||||
"version": row.Version,
|
||||
@@ -208,6 +216,7 @@ func (c *PlatformCursorEquipmentController) rowToMap(row *models.PlatformCursorE
|
||||
}
|
||||
|
||||
// List GET /platform/cursor/equipment/list
|
||||
// 优化:用 3 条批量 SQL 替代 N+1,列表只返回必要字段,详情由 Detail 接口按需加载完整数据。
|
||||
func (c *PlatformCursorEquipmentController) List() {
|
||||
if _, err := c.platformClaims(); err != nil {
|
||||
c.jsonErr(401, 401, err.Error())
|
||||
@@ -265,9 +274,242 @@ func (c *PlatformCursorEquipmentController) List() {
|
||||
return
|
||||
}
|
||||
|
||||
if len(rows) == 0 {
|
||||
c.ok(map[string]interface{}{
|
||||
"list": []interface{}{},
|
||||
"total": total,
|
||||
"page": page,
|
||||
"pageSize": pageSize,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// ── 收集本页所有设备的 ID 和机器码 ──────────────────────────────────
|
||||
deviceIDs := make([]uint64, 0, len(rows))
|
||||
machineCodes := make([]string, 0, len(rows))
|
||||
codeToID := make(map[string]uint64, len(rows)) // machineCode -> equipmentID
|
||||
for _, r := range rows {
|
||||
deviceIDs = append(deviceIDs, r.ID)
|
||||
machineCodes = append(machineCodes, r.MachineCode)
|
||||
codeToID[r.MachineCode] = r.ID
|
||||
}
|
||||
|
||||
// ── 批量查激活码(1 条 SQL)──────────────────────────────────────────
|
||||
// 只取本页设备相关的所有激活码,按 id 降序,在内存里取每个设备的 latest
|
||||
type actSummary struct {
|
||||
code string
|
||||
codeID uint64
|
||||
count int64
|
||||
lastActivatedAt interface{}
|
||||
expiredAt interface{}
|
||||
}
|
||||
actMap := make(map[uint64]*actSummary, len(rows))
|
||||
|
||||
var allActs []models.PlatformCursorActivationCode
|
||||
if len(deviceIDs) > 0 {
|
||||
// Beego ORM 的 __in 过滤
|
||||
ids := make([]interface{}, len(deviceIDs))
|
||||
for i, id := range deviceIDs {
|
||||
ids[i] = id
|
||||
}
|
||||
codes := make([]interface{}, len(machineCodes))
|
||||
for i, mc := range machineCodes {
|
||||
codes[i] = mc
|
||||
}
|
||||
actCond := orm.NewCondition().
|
||||
And("delete_time__isnull", true).
|
||||
AndCond(orm.NewCondition().
|
||||
Or("bind_device_id__in", ids).
|
||||
Or("machine_code__in", codes))
|
||||
models.Orm.QueryTable(new(models.PlatformCursorActivationCode)).
|
||||
SetCond(actCond).
|
||||
OrderBy("-activated_at", "-id").
|
||||
All(&allActs)
|
||||
}
|
||||
for i := range allActs {
|
||||
a := &allActs[i]
|
||||
// 确定归属设备 ID
|
||||
var devID uint64
|
||||
if a.BindDeviceID != nil && *a.BindDeviceID != 0 {
|
||||
devID = *a.BindDeviceID
|
||||
} else if a.MachineCode != nil {
|
||||
devID = codeToID[*a.MachineCode]
|
||||
}
|
||||
if devID == 0 {
|
||||
continue
|
||||
}
|
||||
s, exists := actMap[devID]
|
||||
if !exists {
|
||||
s = &actSummary{}
|
||||
actMap[devID] = s
|
||||
}
|
||||
s.count++
|
||||
// 第一条就是 latest(已按 -activated_at,-id 排序)
|
||||
if s.codeID == 0 {
|
||||
s.code = a.Code
|
||||
s.codeID = a.ID
|
||||
if a.ActivatedAt != nil {
|
||||
s.lastActivatedAt = a.ActivatedAt
|
||||
}
|
||||
if a.ExpiredAt != nil {
|
||||
s.expiredAt = a.ExpiredAt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── 批量查提取记录(1 条 SQL)──────────────────────────────────────────
|
||||
type extractSummary struct {
|
||||
count int64
|
||||
lastExtracted interface{}
|
||||
}
|
||||
extractMap := make(map[uint64]*extractSummary, len(rows))
|
||||
|
||||
type extractRow struct {
|
||||
MachineCode string
|
||||
Cnt int64
|
||||
LastExtracted *string
|
||||
}
|
||||
var extractRows []extractRow
|
||||
if len(machineCodes) > 0 {
|
||||
inPlaceholders := make([]string, len(machineCodes))
|
||||
inArgs := make([]interface{}, len(machineCodes))
|
||||
for i, mc := range machineCodes {
|
||||
inPlaceholders[i] = "?"
|
||||
inArgs[i] = mc
|
||||
}
|
||||
sql := "SELECT machine_code, COUNT(*) AS cnt, MAX(extracted_time) AS last_extracted " +
|
||||
"FROM yz_platform_account_pool_cursor " +
|
||||
"WHERE is_extracted > 0 AND delete_time IS NULL " +
|
||||
"AND machine_code IN (" + strings.Join(inPlaceholders, ",") + ") " +
|
||||
"GROUP BY machine_code"
|
||||
models.Orm.Raw(sql, inArgs...).QueryRows(&extractRows)
|
||||
}
|
||||
for _, er := range extractRows {
|
||||
devID := codeToID[er.MachineCode]
|
||||
if devID == 0 {
|
||||
continue
|
||||
}
|
||||
es := &extractSummary{count: er.Cnt}
|
||||
if er.LastExtracted != nil {
|
||||
es.lastExtracted = *er.LastExtracted
|
||||
}
|
||||
extractMap[devID] = es
|
||||
}
|
||||
|
||||
// ── 批量查最后登录 IP(1 条 SQL)────────────────────────────────────────
|
||||
// 每个设备取 id 最大的一条
|
||||
type ipRow struct {
|
||||
EquipmentID uint64
|
||||
MachineCode string
|
||||
Query string
|
||||
}
|
||||
ipMap := make(map[uint64]string, len(rows))
|
||||
if len(deviceIDs) > 0 {
|
||||
inPlaceholders := make([]string, len(deviceIDs))
|
||||
inArgs := make([]interface{}, len(deviceIDs))
|
||||
for i, id := range deviceIDs {
|
||||
inPlaceholders[i] = "?"
|
||||
inArgs[i] = id
|
||||
}
|
||||
codePlaceholders := make([]string, len(machineCodes))
|
||||
codeArgs := make([]interface{}, len(machineCodes))
|
||||
for i, mc := range machineCodes {
|
||||
codePlaceholders[i] = "?"
|
||||
codeArgs[i] = mc
|
||||
}
|
||||
allArgs := append(inArgs, codeArgs...)
|
||||
ipSQL := "SELECT equipment_id, machine_code, query FROM yz_platform_cursor_equipment_ip_log " +
|
||||
"WHERE id IN (" +
|
||||
" SELECT MAX(id) FROM yz_platform_cursor_equipment_ip_log " +
|
||||
" WHERE equipment_id IN (" + strings.Join(inPlaceholders, ",") + ")" +
|
||||
" OR machine_code IN (" + strings.Join(codePlaceholders, ",") + ")" +
|
||||
" GROUP BY COALESCE(NULLIF(equipment_id,0), machine_code)" +
|
||||
")"
|
||||
var ipRows []ipRow
|
||||
models.Orm.Raw(ipSQL, allArgs...).QueryRows(&ipRows)
|
||||
for _, ir := range ipRows {
|
||||
devID := ir.EquipmentID
|
||||
if devID == 0 {
|
||||
devID = codeToID[ir.MachineCode]
|
||||
}
|
||||
if devID != 0 {
|
||||
ipMap[devID] = ir.Query
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── 组装列表(纯内存操作)────────────────────────────────────────────
|
||||
list := make([]map[string]interface{}, 0, len(rows))
|
||||
for i := range rows {
|
||||
list = append(list, c.rowToMap(&rows[i]))
|
||||
r := &rows[i]
|
||||
|
||||
// 激活码信息
|
||||
var bindActivationCode interface{}
|
||||
var activationCodeId interface{}
|
||||
var lastActivatedAt interface{} = r.ActivationTime
|
||||
var expiredAt interface{} = r.ExpireTime
|
||||
var activationCount int64
|
||||
if s, ok := actMap[r.ID]; ok {
|
||||
activationCount = s.count
|
||||
bindActivationCode = s.code
|
||||
activationCodeId = s.codeID
|
||||
if s.lastActivatedAt != nil {
|
||||
lastActivatedAt = s.lastActivatedAt
|
||||
}
|
||||
if s.expiredAt != nil {
|
||||
expiredAt = s.expiredAt
|
||||
}
|
||||
}
|
||||
|
||||
// 提取记录
|
||||
var extractCount int64
|
||||
var lastExtractedAt interface{}
|
||||
if es, ok := extractMap[r.ID]; ok {
|
||||
extractCount = es.count
|
||||
lastExtractedAt = es.lastExtracted
|
||||
}
|
||||
|
||||
// 最后登录 IP
|
||||
var lastLoginIp interface{}
|
||||
if ip, ok := ipMap[r.ID]; ok && ip != "" {
|
||||
lastLoginIp = ip
|
||||
}
|
||||
|
||||
// 在线状态计算:若最后心跳时间在 5 分钟内,则认为在线
|
||||
isOnline := false
|
||||
if r.LastHeartbeatAt != nil && time.Since(*r.LastHeartbeatAt) < 5*time.Minute {
|
||||
isOnline = true
|
||||
}
|
||||
|
||||
list = append(list, map[string]interface{}{
|
||||
"id": r.ID,
|
||||
"deviceInfo": r.DeviceInfo,
|
||||
"machineCode": r.MachineCode,
|
||||
"status": r.Status,
|
||||
"isOnline": isOnline,
|
||||
"lastHeartbeatAt": r.LastHeartbeatAt,
|
||||
"system": r.System,
|
||||
"os": r.System,
|
||||
"version": r.Version,
|
||||
"bindAccount": r.BindAccount,
|
||||
"bindActivationCode": bindActivationCode,
|
||||
"activationCode": bindActivationCode,
|
||||
"activationCodeId": activationCodeId,
|
||||
"ownerUserId": r.OwnerUserID,
|
||||
"ownerUserName": r.OwnerUserName,
|
||||
"activationTime": lastActivatedAt,
|
||||
"lastActivatedAt": lastActivatedAt,
|
||||
"expireTime": expiredAt,
|
||||
"expiredAt": expiredAt,
|
||||
"activationCount": activationCount,
|
||||
"extractCount": extractCount,
|
||||
"lastExtractedAt": lastExtractedAt,
|
||||
"lastLoginIp": lastLoginIp,
|
||||
// 列表不返回 lastLoginIpInfo,点详情时由 Detail 接口加载完整 IP 信息
|
||||
"remark": r.Remark,
|
||||
"createTime": r.CreateTime,
|
||||
"updateTime": r.UpdateTime,
|
||||
})
|
||||
}
|
||||
|
||||
c.ok(map[string]interface{}{
|
||||
|
||||
Reference in New Issue
Block a user