Add monitor configuration, history, and daily rollup models for the operations channel monitoring module.
67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package model
|
|
|
|
import "time"
|
|
|
|
type ChannelMonitor struct {
|
|
Id int `json:"id" gorm:"primaryKey;autoIncrement"`
|
|
Name string `json:"name" gorm:"size:128;not null"`
|
|
ChannelId int `json:"channel_id" gorm:"index;default:0"`
|
|
TargetUrl string `json:"target_url" gorm:"size:512;not null"`
|
|
PrimaryModel string `json:"primary_model" gorm:"size:128;not null"`
|
|
ExtraModels string `json:"extra_models" gorm:"type:text"`
|
|
CheckInterval int `json:"check_interval" gorm:"not null;default:300"`
|
|
Timeout int `json:"timeout" gorm:"not null;default:30"`
|
|
RequestTemplates string `json:"request_templates" gorm:"type:text"`
|
|
CustomHeaders string `json:"custom_headers" gorm:"type:text"`
|
|
CustomBody string `json:"custom_body" gorm:"type:text"`
|
|
Status int `json:"status" gorm:"not null;default:1;index"`
|
|
LastCheckedAt *time.Time `json:"last_checked_at"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (ChannelMonitor) TableName() string {
|
|
return "channel_monitors"
|
|
}
|
|
|
|
func (m *ChannelMonitor) Insert() error {
|
|
return DB.Create(m).Error
|
|
}
|
|
|
|
func (m *ChannelMonitor) Update() error {
|
|
return DB.Save(m).Error
|
|
}
|
|
|
|
func GetChannelMonitorByID(id int) (*ChannelMonitor, error) {
|
|
var monitor ChannelMonitor
|
|
err := DB.First(&monitor, "id = ?", id).Error
|
|
return &monitor, err
|
|
}
|
|
|
|
func GetEnabledChannelMonitors() ([]ChannelMonitor, error) {
|
|
var monitors []ChannelMonitor
|
|
err := DB.Where("status = ?", 1).Order("id DESC").Find(&monitors).Error
|
|
return monitors, err
|
|
}
|
|
|
|
func GetChannelMonitors(page int, pageSize int) ([]ChannelMonitor, int64, error) {
|
|
var monitors []ChannelMonitor
|
|
var total int64
|
|
query := DB.Model(&ChannelMonitor{})
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 {
|
|
pageSize = 20
|
|
}
|
|
err := query.Order("id DESC").Offset((page - 1) * pageSize).Limit(pageSize).Find(&monitors).Error
|
|
return monitors, total, err
|
|
}
|
|
|
|
func DeleteChannelMonitorByID(id int) error {
|
|
return DB.Delete(&ChannelMonitor{}, id).Error
|
|
}
|