mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 12:40:07 +00:00
refactor: rename monitor -> service
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/naiba/nezha/pkg/utils"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -12,17 +10,6 @@ const (
|
||||
ModeOnetimeTrigger = 1
|
||||
)
|
||||
|
||||
type CycleTransferStats struct {
|
||||
Name string
|
||||
From time.Time
|
||||
To time.Time
|
||||
Max uint64
|
||||
Min uint64
|
||||
ServerName map[uint64]string
|
||||
Transfer map[uint64]uint64
|
||||
NextUpdate map[uint64]time.Time
|
||||
}
|
||||
|
||||
type AlertRule struct {
|
||||
Common
|
||||
Name string
|
||||
|
||||
18
model/api.go
18
model/api.go
@@ -4,24 +4,6 @@ const (
|
||||
ApiErrorUnauthorized = 10001
|
||||
)
|
||||
|
||||
type ServiceItemResponse struct {
|
||||
Monitor *Monitor
|
||||
CurrentUp uint64
|
||||
CurrentDown uint64
|
||||
TotalUp uint64
|
||||
TotalDown uint64
|
||||
Delay *[30]float32
|
||||
Up *[30]int
|
||||
Down *[30]int
|
||||
}
|
||||
|
||||
func (r ServiceItemResponse) TotalUptime() float32 {
|
||||
if r.TotalUp+r.TotalDown == 0 {
|
||||
return 0
|
||||
}
|
||||
return float32(r.TotalUp) / (float32(r.TotalUp + r.TotalDown)) * 100
|
||||
}
|
||||
|
||||
type LoginRequest struct {
|
||||
Username string `json:"username,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// MonitorHistory 历史监控记录
|
||||
type MonitorHistory struct {
|
||||
ID uint64 `gorm:"primaryKey"`
|
||||
CreatedAt time.Time `gorm:"index;<-:create;index:idx_server_id_created_at_monitor_id_avg_delay"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
MonitorID uint64 `gorm:"index:idx_server_id_created_at_monitor_id_avg_delay"`
|
||||
ServerID uint64 `gorm:"index:idx_server_id_created_at_monitor_id_avg_delay"`
|
||||
AvgDelay float32 `gorm:"index:idx_server_id_created_at_monitor_id_avg_delay"` // 平均延迟,毫秒
|
||||
Up uint64 // 检查状态良好计数
|
||||
Down uint64 // 检查状态异常计数
|
||||
Data string
|
||||
}
|
||||
@@ -40,11 +40,11 @@ type TaskFM struct {
|
||||
}
|
||||
|
||||
const (
|
||||
MonitorCoverAll = iota
|
||||
MonitorCoverIgnoreAll
|
||||
ServiceCoverAll = iota
|
||||
ServiceCoverIgnoreAll
|
||||
)
|
||||
|
||||
type Monitor struct {
|
||||
type Service struct {
|
||||
Common
|
||||
Name string `json:"name,omitempty"`
|
||||
Type uint8 `json:"type,omitempty"`
|
||||
@@ -71,7 +71,7 @@ type Monitor struct {
|
||||
CronJobID cron.EntryID `gorm:"-" json:"-"`
|
||||
}
|
||||
|
||||
func (m *Monitor) PB() *pb.Task {
|
||||
func (m *Service) PB() *pb.Task {
|
||||
return &pb.Task{
|
||||
Id: m.ID,
|
||||
Type: uint64(m.Type),
|
||||
@@ -80,7 +80,7 @@ func (m *Monitor) PB() *pb.Task {
|
||||
}
|
||||
|
||||
// CronSpec 返回服务监控请求间隔对应的 cron 表达式
|
||||
func (m *Monitor) CronSpec() string {
|
||||
func (m *Service) CronSpec() string {
|
||||
if m.Duration == 0 {
|
||||
// 默认间隔 30 秒
|
||||
m.Duration = 30
|
||||
@@ -88,7 +88,7 @@ func (m *Monitor) CronSpec() string {
|
||||
return fmt.Sprintf("@every %ds", m.Duration)
|
||||
}
|
||||
|
||||
func (m *Monitor) BeforeSave(tx *gorm.DB) error {
|
||||
func (m *Service) BeforeSave(tx *gorm.DB) error {
|
||||
if data, err := utils.Json.Marshal(m.SkipServers); err != nil {
|
||||
return err
|
||||
} else {
|
||||
@@ -107,10 +107,10 @@ func (m *Monitor) BeforeSave(tx *gorm.DB) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Monitor) AfterFind(tx *gorm.DB) error {
|
||||
func (m *Service) AfterFind(tx *gorm.DB) error {
|
||||
m.SkipServers = make(map[uint64]bool)
|
||||
if err := utils.Json.Unmarshal([]byte(m.SkipServersRaw), &m.SkipServers); err != nil {
|
||||
log.Println("NEZHA>> Monitor.AfterFind:", err)
|
||||
log.Println("NEZHA>> Service.AfterFind:", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package model
|
||||
|
||||
type MonitorForm struct {
|
||||
import "time"
|
||||
|
||||
type ServiceForm struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Target string `json:"target,omitempty"`
|
||||
Type uint8 `json:"type,omitempty"`
|
||||
@@ -17,3 +19,37 @@ type MonitorForm struct {
|
||||
SkipServers map[uint64]bool `json:"skip_servers,omitempty"`
|
||||
NotificationGroupID uint64 `json:"notification_group_id,omitempty"`
|
||||
}
|
||||
|
||||
type ServiceResponseItem struct {
|
||||
Service *Service
|
||||
CurrentUp uint64
|
||||
CurrentDown uint64
|
||||
TotalUp uint64
|
||||
TotalDown uint64
|
||||
Delay *[30]float32
|
||||
Up *[30]int
|
||||
Down *[30]int
|
||||
}
|
||||
|
||||
func (r ServiceResponseItem) TotalUptime() float32 {
|
||||
if r.TotalUp+r.TotalDown == 0 {
|
||||
return 0
|
||||
}
|
||||
return float32(r.TotalUp) / (float32(r.TotalUp + r.TotalDown)) * 100
|
||||
}
|
||||
|
||||
type CycleTransferStats struct {
|
||||
Name string
|
||||
From time.Time
|
||||
To time.Time
|
||||
Max uint64
|
||||
Min uint64
|
||||
ServerName map[uint64]string
|
||||
Transfer map[uint64]uint64
|
||||
NextUpdate map[uint64]time.Time
|
||||
}
|
||||
|
||||
type ServiceResponse struct {
|
||||
Services map[uint64]ServiceResponseItem
|
||||
CycleTransferStats map[uint64]CycleTransferStats
|
||||
}
|
||||
20
model/service_history.go
Normal file
20
model/service_history.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ServiceHistory struct {
|
||||
ID uint64 `gorm:"primaryKey" json:"id,omitempty"`
|
||||
CreatedAt time.Time `gorm:"index;<-:create;index:idx_server_id_created_at_service_id_avg_delay" json:"created_at,omitempty"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at,omitempty"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
|
||||
ServiceID uint64 `gorm:"index:idx_server_id_created_at_service_id_avg_delay" json:"service_id,omitempty"`
|
||||
ServerID uint64 `gorm:"index:idx_server_id_created_at_service_id_avg_delay" json:"server_id,omitempty"`
|
||||
AvgDelay float32 `gorm:"index:idx_server_id_created_at_service_id_avg_delay" json:"avg_delay,omitempty"` // 平均延迟,毫秒
|
||||
Up uint64 `json:"up,omitempty"` // 检查状态良好计数
|
||||
Down uint64 `json:"down,omitempty"` // 检查状态异常计数
|
||||
Data string `json:"data,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user