2020-03-04 10:29:00 +00:00
package services
import (
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/checkins"
"github.com/statping/statping/types/failures"
"github.com/statping/statping/types/null"
2020-04-17 12:52:53 +00:00
"github.com/statping/statping/utils"
2020-03-04 10:29:00 +00:00
"time"
)
var (
allServices map [ int64 ] * Service
)
func init ( ) {
allServices = make ( map [ int64 ] * Service )
}
func Services ( ) map [ int64 ] * Service {
return allServices
}
// Service is the main struct for Services
type Service struct {
Id int64 ` gorm:"primary_key;column:id" json:"id" `
Name string ` gorm:"column:name" json:"name" `
Domain string ` gorm:"column:domain" json:"domain" private:"true" scope:"user,admin" `
Expected null . NullString ` gorm:"column:expected" json:"expected" scope:"user,admin" `
ExpectedStatus int ` gorm:"default:200;column:expected_status" json:"expected_status" scope:"user,admin" `
2020-04-13 11:17:09 +00:00
Interval int ` gorm:"default:30;column:check_interval" json:"check_interval" `
2020-03-04 10:29:00 +00:00
Type string ` gorm:"column:check_type" json:"type" scope:"user,admin" `
Method string ` gorm:"column:method" json:"method" scope:"user,admin" `
PostData null . NullString ` gorm:"column:post_data" json:"post_data" scope:"user,admin" `
Port int ` gorm:"not null;column:port" json:"port" scope:"user,admin" `
Timeout int ` gorm:"default:30;column:timeout" json:"timeout" scope:"user,admin" `
Order int ` gorm:"default:0;column:order_id" json:"order_id" `
VerifySSL null . NullBool ` gorm:"default:false;column:verify_ssl" json:"verify_ssl" scope:"user,admin" `
Public null . NullBool ` gorm:"default:true;column:public" json:"public" `
GroupId int ` gorm:"default:0;column:group_id" json:"group_id" `
Headers null . NullString ` gorm:"column:headers" json:"headers" scope:"user,admin" `
Permalink null . NullString ` gorm:"column:permalink" json:"permalink" `
CreatedAt time . Time ` gorm:"column:created_at" json:"created_at" `
UpdatedAt time . Time ` gorm:"column:updated_at" json:"updated_at" `
Online bool ` gorm:"-" json:"online" `
2020-03-10 05:24:35 +00:00
Latency int64 ` gorm:"-" json:"latency" `
PingTime int64 ` gorm:"-" json:"ping_time" `
2020-03-04 10:29:00 +00:00
Online24Hours float32 ` gorm:"-" json:"online_24_hours" `
Online7Days float32 ` gorm:"-" json:"online_7_days" `
2020-03-13 04:06:06 +00:00
AvgResponse int64 ` gorm:"-" json:"avg_response" `
2020-03-04 10:29:00 +00:00
FailuresLast24Hours int ` gorm:"-" json:"failures_24_hours" `
Running chan bool ` gorm:"-" json:"-" `
Checkpoint time . Time ` gorm:"-" json:"-" `
SleepDuration time . Duration ` gorm:"-" json:"-" `
LastResponse string ` gorm:"-" json:"-" `
2020-03-23 02:50:30 +00:00
NotifyAfter int64 ` gorm:"column:notify_after" json:"notify_after" scope:"user,admin" `
notifyAfterCount int64 ` gorm:"-" json:"-" `
2020-03-04 10:29:00 +00:00
AllowNotifications null . NullBool ` gorm:"default:true;column:allow_notifications" json:"allow_notifications" scope:"user,admin" `
UserNotified bool ` gorm:"-" json:"-" ` // True if the User was already notified about a Downtime
UpdateNotify null . NullBool ` gorm:"default:true;column:notify_all_changes" json:"notify_all_changes" scope:"user,admin" ` // This Variable is a simple copy of `core.CoreApp.UpdateNotify.Bool`
DownText string ` gorm:"-" json:"-" ` // Contains the current generated Downtime Text
SuccessNotified bool ` gorm:"-" json:"-" ` // Is 'true' if the user has already be informed that the Services now again available
LastStatusCode int ` gorm:"-" json:"status_code" `
Failures [ ] * failures . Failure ` gorm:"-" json:"failures,omitempty" scope:"user,admin" `
AllCheckins [ ] * checkins . Checkin ` gorm:"-" json:"checkins,omitempty" scope:"user,admin" `
2020-03-05 08:27:51 +00:00
LastLookupTime int64 ` gorm:"-" json:"-" `
LastLatency int64 ` gorm:"-" json:"-" `
LastCheck time . Time ` gorm:"-" json:"-" `
2020-03-06 22:18:06 +00:00
LastOnline time . Time ` gorm:"-" json:"last_success" `
LastOffline time . Time ` gorm:"-" json:"last_error" `
Stats * Stats ` gorm:"-" json:"stats,omitempty" `
2020-03-05 08:27:51 +00:00
SecondsOnline int64 ` gorm:"-" json:"-" `
SecondsOffline int64 ` gorm:"-" json:"-" `
2020-03-04 10:29:00 +00:00
}
type Stats struct {
2020-03-13 04:06:06 +00:00
Failures int ` gorm:"-" json:"failures" `
Hits int ` gorm:"-" json:"hits" `
FirstHit time . Time ` gorm:"-" json:"first_hit" `
2020-03-04 10:29:00 +00:00
}
// BeforeCreate for Service will set CreatedAt to UTC
func ( s * Service ) BeforeCreate ( ) ( err error ) {
if s . CreatedAt . IsZero ( ) {
2020-04-17 12:52:53 +00:00
s . CreatedAt = utils . Now ( )
s . UpdatedAt = utils . Now ( )
2020-03-04 10:29:00 +00:00
}
return
}
// ServiceOrder will reorder the services based on 'order_id' (Order)
2020-03-06 03:03:18 +00:00
type ServiceOrder [ ] Service
2020-03-04 10:29:00 +00:00
// Sort interface for resroting the Services in order
2020-03-06 03:03:18 +00:00
func ( c ServiceOrder ) Len ( ) int { return len ( c ) }
func ( c ServiceOrder ) Swap ( i , j int ) { c [ int64 ( i ) ] , c [ int64 ( j ) ] = c [ int64 ( j ) ] , c [ int64 ( i ) ] }
func ( c ServiceOrder ) Less ( i , j int ) bool { return c [ i ] . Order < c [ j ] . Order }