82 lines
2.1 KiB
Go
82 lines
2.1 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:"httpConfig"`
|
|
// MaxFails - max "ping" fails, after with the service marked as unavailable
|
|
MaxFails uint8 `json:"maxFails"`
|
|
// Interval - interval between "ping" in seconds
|
|
Interval uint64 `json:"interval"`
|
|
// Timeout - interval after which the task will be canceled
|
|
Timeout uint64 `json:"timeout"`
|
|
}
|
|
|
|
type Service struct {
|
|
// Unique ID for entity
|
|
ID uint64 `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:"publicDescription"`
|
|
Public *bool `gorm:"default:false" json:"public"`
|
|
// Host to check, for example 192.168.1.44 or https://google.com
|
|
Host string `gorm:"size:255;not null" json:"host"`
|
|
// Type for check, for now is TCP or HTTP or something else
|
|
Type string `gorm:"size:255;not null" json:"type"`
|
|
Config *ServiceTypeCheckConfig `gorm:"serializer:json;column:type_config" json:"typeConfig"`
|
|
|
|
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
|
|
}
|