Files
status/model/service.go
ostiwe fc8b723114 refactor: Fix linter errors by auto-apply
- Apply automated linter fixes
- Manually reformat fields in model/service.go
- Resolve code complexity issues
2025-11-04 19:59:17 +03:00

83 lines
2.4 KiB
Go

package model
import "math"
type HTTPConfig struct {
Method string `json:"method"`
Headers map[string]string `json:"headers"`
}
// ServiceTypeCheckConfig
// MaxFails - max "ping" fails, after with the service marked as unavailable
// Interval - interval between "ping" in seconds
// Timeout - interval after which the task will be canceled
type ServiceTypeCheckConfig struct {
Version string `json:"version"`
HTTPConfig *HTTPConfig `json:"httpConfig"`
MaxFails uint8 `json:"maxFails"`
Interval uint64 `json:"interval"`
Timeout uint64 `json:"timeout"`
}
// Service
// ID Unique ID for entity
// Name Human-readable service name
// Description Human-readable service description
// Host to check, for example 192.168.1.44 or https://google.com
// Type for check, for now is TCP or HTTP or something else
type Service struct {
ID uint64 `gorm:"primary_key;auto_increment" json:"id"`
Name string `gorm:"size:255;not null" json:"name"`
Description string `gorm:"size:255" json:"description"`
PublicDescription string `gorm:"size:255" json:"publicDescription"`
Public *bool `gorm:"default:false" json:"public"`
Host string `gorm:"size:255;not null" json:"host"`
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
}