nezha/model/monitor.go

48 lines
802 B
Go
Raw Normal View History

2019-12-07 05:14:40 -05:00
package model
2020-11-06 07:56:46 -05:00
import (
"encoding/json"
2020-11-10 21:07:45 -05:00
pb "github.com/naiba/nezha/proto"
"gorm.io/gorm"
2020-11-06 07:56:46 -05:00
)
2019-12-07 05:14:40 -05:00
2019-12-09 03:02:49 -05:00
const (
_ = iota
TaskTypeHTTPGET
TaskTypeICMPPing
TaskTypeTCPPing
TaskTypeCommand
2019-12-09 03:02:49 -05:00
)
type Monitor struct {
Common
Name string
Type uint8
Target string
SkipServersRaw string
Notify bool
SkipServers map[uint64]bool `gorm:"-" json:"-"`
2019-12-07 05:14:40 -05:00
}
func (m *Monitor) PB() *pb.Task {
return &pb.Task{
Id: m.ID,
Type: uint64(m.Type),
Data: m.Target,
2019-12-09 05:14:31 -05:00
}
}
func (m *Monitor) AfterFind(tx *gorm.DB) error {
var skipServers []uint64
if err := json.Unmarshal([]byte(m.SkipServersRaw), &skipServers); err != nil {
return err
}
m.SkipServers = make(map[uint64]bool)
for i := 0; i < len(skipServers); i++ {
m.SkipServers[skipServers[i]] = true
}
return nil
}