76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package model
|
|
|
|
import "math"
|
|
|
|
type HTTPConfig struct {
|
|
Method string `json:"method"`
|
|
Headers map[string]string `json:"headers"`
|
|
}
|
|
|
|
type ServiceTypeCheckConfig struct {
|
|
Version string `json:"version"`
|
|
HTTPConfig *HTTPConfig `json:"http_config"`
|
|
}
|
|
|
|
type Service struct {
|
|
// Unique ID for entity
|
|
ID int `gorm:"primary_key;auto_increment" json:"id"`
|
|
// Human-readable service name
|
|
Name string `gorm:"size:255;not null" json:"name"`
|
|
// Human-readable service description
|
|
Description string `gorm:"size:255" json:"description"`
|
|
PublicDescription string `gorm:"size:255" json:"public_description"`
|
|
Public *bool `gorm:"default:false" json:"public"`
|
|
// Host to check, for example 192.168.1.44
|
|
Host string `gorm:"size:255;not null" json:"host"`
|
|
// Type for check, for now is TCP or HTTP
|
|
Type string `gorm:"size:255;not null" json:"type"`
|
|
TypeConfig *ServiceTypeCheckConfig `gorm:"serializer:json" json:"type_config"`
|
|
|
|
Statuses []Status `gorm:"foreignkey:ServiceID" json:"statuses"`
|
|
}
|
|
|
|
func (Service) TableName() string {
|
|
return "services"
|
|
}
|
|
|
|
func (s Service) CountLastStatuses(status StatusCode) uint {
|
|
var count uint = 0
|
|
|
|
for i := range s.Statuses {
|
|
if s.Statuses[i].Status != status {
|
|
break
|
|
}
|
|
|
|
count++
|
|
}
|
|
|
|
return count
|
|
}
|
|
|
|
func (s Service) CalculateUptimePercent() float64 {
|
|
var uptime float64
|
|
|
|
if len(s.Statuses) == 0 {
|
|
return uptime
|
|
}
|
|
|
|
var countOfOkStatus int
|
|
|
|
for i := range s.Statuses {
|
|
if s.Statuses[i].Status == StatusOK {
|
|
countOfOkStatus++
|
|
}
|
|
}
|
|
|
|
if countOfOkStatus == 0 {
|
|
return uptime
|
|
}
|
|
|
|
sla := 100 - (float64(len(s.Statuses)) / float64(countOfOkStatus))
|
|
ratio := math.Pow(10, float64(2))
|
|
uptime = math.Round(sla*ratio) / ratio
|
|
|
|
return uptime
|
|
}
|