File size: 2,542 Bytes
ffb6330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package database

import (
	"time"

	"gorm.io/datatypes"
	"gorm.io/gorm"
)

// Agent 表示一个注册的agent(GORM实现)
type Agent struct {
	ID           string         `gorm:"primaryKey;column:id" json:"agent_id"`
	Hostname     string         `gorm:"column:hostname;not null" json:"hostname"`
	IP           string         `gorm:"column:ip;not null" json:"ip"`
	Version      string         `gorm:"column:version" json:"version"`
	Capabilities datatypes.JSON `gorm:"column:capabilities" json:"capabilities"` // 存储为JSON
	Meta         string         `gorm:"column:meta;not null" json:"meta"`
	LastSeen     int64          `gorm:"column:last_seen;not null" json:"last_seen"`   // 时间戳毫秒级
	CreatedAt    int64          `gorm:"column:created_at;not null" json:"created_at"` // 时间戳毫秒级
	UpdatedAt    int64          `gorm:"column:updated_at;not null" json:"updated_at"` // 时间戳毫秒级
	Online       bool           `gorm:"column:online;not null;default:false" json:"online"`
}

type AgentStore struct {
	db *gorm.DB
}

// NewAgentStore 创建一个新的AgentStore实例
func NewAgentStore(db *gorm.DB) *AgentStore {
	return &AgentStore{db: db}
}

// Init 自动迁移agent表结构
func (s *AgentStore) Init() error {
	return s.db.AutoMigrate(&Agent{})
}

// Register 注册或更新agent信息
func (s *AgentStore) Register(agent *Agent) error {
	now := time.Now().UnixMilli()
	agent.LastSeen = now
	if agent.CreatedAt == 0 {
		agent.CreatedAt = now
	}
	agent.UpdatedAt = now
	return s.db.Save(agent).Error
}

// GetAgent 获取指定agent的信息
func (s *AgentStore) GetAgent(id string) (*Agent, error) {
	var agent Agent
	err := s.db.First(&agent, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &agent, nil
}

// ListAgents 获取所有agent列表
func (s *AgentStore) ListAgents() ([]*Agent, error) {
	var agents []*Agent
	err := s.db.Find(&agents).Error
	if err != nil {
		return nil, err
	}
	return agents, nil
}

// UpdateLastSeen 更新agent的最后在线时间
func (s *AgentStore) UpdateLastSeen(id string) error {
	now := time.Now().UnixMilli()
	return s.db.Model(&Agent{}).Where("id = ?", id).Updates(map[string]interface{}{
		"last_seen":  now,
		"updated_at": now,
	}).Error
}

// DeleteAgent 删除指定的agent
func (s *AgentStore) DeleteAgent(id string) error {
	return s.db.Delete(&Agent{}, "id = ?", id).Error
}

func (s *AgentStore) UpdateOnlineStatus(id string, online bool) error {
	return s.db.Model(&Agent{}).Where("id = ?", id).Update("online", online).Error
}