up(notification): 添加修改删除通知、报警规则

This commit is contained in:
naiba
2020-12-19 23:11:16 +08:00
parent d19de0edc2
commit fcbd564bff
14 changed files with 156 additions and 85 deletions

35
model/alertrule.go Normal file
View File

@@ -0,0 +1,35 @@
package model
import (
"encoding/json"
"gorm.io/gorm"
)
type Rule struct {
Type string // 指标类型cpu、memory、swap、disk、net_in、net_out、net_all、transfer_in、transfer_out、transfer_all、offline
Min uint64 // 最小阈值 (百分比、字节 kb ÷ 1024)
Max uint64 // 最大阈值 (百分比、字节 kb ÷ 1024)
Duration uint64 // 持续时间 (秒)
}
type AlertRule struct {
Common
Name string
Rules []Rule `gorm:"-" json:"-"`
RulesRaw string
Enable *bool
}
func (r *AlertRule) BeforeSave(tx *gorm.DB) error {
data, err := json.Marshal(r.Rules)
if err != nil {
return err
}
r.RulesRaw = string(data)
return nil
}
func (r *AlertRule) AfterFind(tx *gorm.DB) error {
return json.Unmarshal([]byte(r.RulesRaw), r.Rules)
}

View File

@@ -1,10 +0,0 @@
package model
type Rule struct {
Common
Name string
Type string // 指标类型cpu、memory、swap、disk、net_in、net_out、net_all、transfer_in、transfer_out、transfer_all、offline
Min uint64 // 最小阈值
Max uint64 // 最大阈值
Duration uint64 // 持续时间
}

View File

@@ -14,8 +14,8 @@ type Server struct {
Name string
Secret string `json:"-"`
Host *Host
State *State
Host *Host `gorm:"-"`
State *State `gorm:"-"`
LastActive time.Time
Stream pb.NezhaService_HeartbeatServer `gorm:"-" json:"-"`

View File

@@ -10,7 +10,7 @@ import (
// User ...
type User struct {
Common `json:"common,omitempty"`
Common
Login string `gorm:"UNIQUE_INDEX" json:"login,omitempty"` // 登录名
AvatarURL string `json:"avatar_url,omitempty"` // 头像地址
Name string `json:"name,omitempty"` // 昵称