Некоторые проверки не удались
CI / test (push) Successful in 2m5s
Docker / Build and publish worker image (push) Failing after 31s
84 строки
1.9 KiB
Go
84 строки
1.9 KiB
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/lib/pq"
|
|
"gorm.io/gorm"
|
|
|
|
"rocketgit.ru/rsmon/worker/app/models/concerns"
|
|
"rocketgit.ru/rsmon/worker/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,
|
|
)
|
|
}
|