feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
Этот коммит содержится в:
83
app/models/event.go
Обычный файл
83
app/models/event.go
Обычный файл
@@ -0,0 +1,83 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"rsgit.ru/rsmon/rsmon/app/models/concerns"
|
||||
"rsgit.ru/rsmon/rsmon/internal/util"
|
||||
)
|
||||
|
||||
// Event provides functionality.
|
||||
type Event struct {
|
||||
concerns.Model
|
||||
|
||||
MonitorID int64 `gorm:"type:bigint REFERENCES monitors(id)" json:"monitor_id"`
|
||||
Monitor *Monitor `json:"monitor,omitempty"`
|
||||
|
||||
StartTime *time.Time `json:"start_time"`
|
||||
EndTime *time.Time `json:"end_time"`
|
||||
Duration int `json:"duration"`
|
||||
Errors int `json:"errors"`
|
||||
Oks int `json:"oks"`
|
||||
State string `gorm:"index" json:"state"`
|
||||
Reason string `json:"reason"`
|
||||
|
||||
Messages []Message `json:"messages" gorm:"many2many:event_messages;"`
|
||||
|
||||
ChecksDown pq.StringArray `gorm:"type:varchar(255)[]" json:"checks_down"`
|
||||
Checks []Check `json:"-" gorm:"many2many:event_checks;"`
|
||||
|
||||
ExpiresAt *time.Time `json:"-"`
|
||||
|
||||
Audited
|
||||
}
|
||||
|
||||
// EventScope provides functionality.
|
||||
func EventScope(q *gorm.DB) *gorm.DB {
|
||||
return q.Where("state IN ('current', 'ended')").
|
||||
Preload("Checks").
|
||||
Preload("Monitor").
|
||||
Preload("Monitor.Group").
|
||||
Preload("Monitor.Group.Notifications").
|
||||
Preload("Monitor.Group.Notifications.Contacts")
|
||||
}
|
||||
|
||||
// GetDuration provides functionality.
|
||||
func (e *Event) GetDuration(tn time.Time) int64 {
|
||||
endTime := e.EndTime
|
||||
if endTime == nil {
|
||||
endTime = &tn
|
||||
}
|
||||
return int64(endTime.Sub(*e.StartTime) / time.Second)
|
||||
}
|
||||
|
||||
// FormatDuration provides functionality.
|
||||
func (e *Event) FormatDuration() string {
|
||||
d := e.GetDuration(time.Now())
|
||||
return util.FormatDuration(d)
|
||||
}
|
||||
|
||||
// Inspect provides functionality.
|
||||
func (e *Event) Inspect() string {
|
||||
var st, et string
|
||||
if e.StartTime != nil {
|
||||
st = e.StartTime.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
if e.EndTime != nil {
|
||||
et = e.EndTime.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
return fmt.Sprintf(
|
||||
"Event<id: %d, monitor_id: %d, start_time: %s, end_time: %s, reason: %s, duration: %d>",
|
||||
e.ID,
|
||||
e.MonitorID,
|
||||
st,
|
||||
et,
|
||||
e.Reason,
|
||||
e.Duration,
|
||||
)
|
||||
}
|
||||
Ссылка в новой задаче
Block a user