Files
meshtastic_mqtt_server/internal/store/active_store.go
T
kevinandClaude Fable 5 cd5dcb29f5 新增活跃度查询工具
功能:
- 查询指定时间范围内的活跃节点数和活跃人数
- 活跃节点:统计 nodeinfo 表 updated_at 字段
- 活跃人数:统计 text_message 表按 from_id 去重的用户数

使用场景:
- 用户问'现在有多少人活跃'时 AI 调用此工具
- 用户问'当前有多少节点在线'时 AI 调用此工具
- 支持附带时间条件,默认1小时,最大24小时

参数:
- hours: 查询最近N小时,默认1小时,最大24小时
- query_type: both/nodes/users,默认 both

实现:
- internal/agents/active/active.go - 工具主逻辑
- internal/store/active_store.go - 数据库查询方法
- 完整的单元测试,所有测试通过
- 在 ai/service.go 中注册工具

测试:
-  默认查询(1小时,both)
-  指定时间查询(6小时、24小时)
-  仅查询节点/人数
-  时间限制验证
-  项目编译成功

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-23 21:26:14 +08:00

25 lines
635 B
Go

package store
import (
"time"
)
// CountActiveNodes 统计指定时间后有更新记录的节点数量
func (s *Store) CountActiveNodes(since time.Time) (int64, error) {
var count int64
err := s.db.Model(&NodeInfoRecord{}).
Where("updated_at >= ?", since).
Count(&count).Error
return count, err
}
// CountActiveUsers 统计指定时间后发送过消息的唯一用户数(按 from_id 去重)
func (s *Store) CountActiveUsers(since time.Time) (int64, error) {
var count int64
err := s.db.Model(&TextMessageRecord{}).
Where("created_at >= ?", since).
Distinct("from_id").
Count(&count).Error
return count, err
}