Refactor code

This commit is contained in:
TwinProduction 2021-07-14 01:53:14 -04:00 committed by Chris
parent a4c69d6fc3
commit 8dedcf7c74
6 changed files with 19 additions and 26 deletions

View File

@ -1,9 +1,5 @@
package core package core
import (
"time"
)
const ( const (
// MaximumNumberOfResults is the maximum number of results that ServiceStatus.Results can have // MaximumNumberOfResults is the maximum number of results that ServiceStatus.Results can have
MaximumNumberOfResults = 100 MaximumNumberOfResults = 100
@ -42,17 +38,14 @@ type ServiceStatus struct {
} }
// NewServiceStatus creates a new ServiceStatus // NewServiceStatus creates a new ServiceStatus
func NewServiceStatus(service *Service) *ServiceStatus { func NewServiceStatus(serviceKey, serviceGroup, serviceName string) *ServiceStatus {
return &ServiceStatus{ return &ServiceStatus{
Name: service.Name, Name: serviceName,
Group: service.Group, Group: serviceGroup,
Key: service.Key(), Key: serviceKey,
Results: make([]*Result, 0), Results: make([]*Result, 0),
Events: []*Event{{ Events: make([]*Event, 0),
Type: EventStart, Uptime: NewUptime(),
Timestamp: time.Now(),
}},
Uptime: NewUptime(),
} }
} }

View File

@ -81,7 +81,7 @@ var (
func BenchmarkServiceStatus_WithResultPagination(b *testing.B) { func BenchmarkServiceStatus_WithResultPagination(b *testing.B) {
service := &testService service := &testService
serviceStatus := NewServiceStatus(service) serviceStatus := NewServiceStatus(service.Key(), service.Group, service.Name)
for i := 0; i < MaximumNumberOfResults; i++ { for i := 0; i < MaximumNumberOfResults; i++ {
serviceStatus.AddResult(&testSuccessfulResult) serviceStatus.AddResult(&testSuccessfulResult)
} }

View File

@ -7,7 +7,7 @@ import (
func TestNewServiceStatus(t *testing.T) { func TestNewServiceStatus(t *testing.T) {
service := &Service{Name: "name", Group: "group"} service := &Service{Name: "name", Group: "group"}
serviceStatus := NewServiceStatus(service) serviceStatus := NewServiceStatus(service.Key(), service.Group, service.Name)
if serviceStatus.Name != service.Name { if serviceStatus.Name != service.Name {
t.Errorf("expected %s, got %s", service.Name, serviceStatus.Name) t.Errorf("expected %s, got %s", service.Name, serviceStatus.Name)
} }
@ -21,7 +21,7 @@ func TestNewServiceStatus(t *testing.T) {
func TestServiceStatus_AddResult(t *testing.T) { func TestServiceStatus_AddResult(t *testing.T) {
service := &Service{Name: "name", Group: "group"} service := &Service{Name: "name", Group: "group"}
serviceStatus := NewServiceStatus(service) serviceStatus := NewServiceStatus(service.Key(), service.Group, service.Name)
for i := 0; i < MaximumNumberOfResults+10; i++ { for i := 0; i < MaximumNumberOfResults+10; i++ {
serviceStatus.AddResult(&Result{Timestamp: time.Now()}) serviceStatus.AddResult(&Result{Timestamp: time.Now()})
} }
@ -32,7 +32,7 @@ func TestServiceStatus_AddResult(t *testing.T) {
func TestServiceStatus_WithResultPagination(t *testing.T) { func TestServiceStatus_WithResultPagination(t *testing.T) {
service := &Service{Name: "name", Group: "group"} service := &Service{Name: "name", Group: "group"}
serviceStatus := NewServiceStatus(service) serviceStatus := NewServiceStatus(service.Key(), service.Group, service.Name)
for i := 0; i < 25; i++ { for i := 0; i < 25; i++ {
serviceStatus.AddResult(&Result{Timestamp: time.Now()}) serviceStatus.AddResult(&Result{Timestamp: time.Now()})
} }

View File

@ -7,7 +7,7 @@ import (
func TestUptime_ProcessResult(t *testing.T) { func TestUptime_ProcessResult(t *testing.T) {
service := &Service{Name: "name", Group: "group"} service := &Service{Name: "name", Group: "group"}
serviceStatus := NewServiceStatus(service) serviceStatus := NewServiceStatus(service.Key(), service.Group, service.Name)
uptime := serviceStatus.Uptime uptime := serviceStatus.Uptime
checkUptimes(t, serviceStatus, 0.00, 0.00, 0.00) checkUptimes(t, serviceStatus, 0.00, 0.00, 0.00)
@ -50,7 +50,7 @@ func TestUptime_ProcessResult(t *testing.T) {
func TestServiceStatus_AddResultUptimeIsCleaningUpAfterItself(t *testing.T) { func TestServiceStatus_AddResultUptimeIsCleaningUpAfterItself(t *testing.T) {
service := &Service{Name: "name", Group: "group"} service := &Service{Name: "name", Group: "group"}
serviceStatus := NewServiceStatus(service) serviceStatus := NewServiceStatus(service.Key(), service.Group, service.Name)
now := time.Now() now := time.Now()
now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()) now = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
// Start 12 days ago // Start 12 days ago

View File

@ -444,12 +444,7 @@ func (s *Store) getServiceStatusByKey(tx *sql.Tx, key string, eventsPage, events
if err != nil { if err != nil {
return nil, err return nil, err
} }
serviceStatus := &core.ServiceStatus{ serviceStatus := core.NewServiceStatus(key, serviceGroup, serviceName)
Name: serviceName,
Group: serviceGroup,
Key: key,
Uptime: &core.Uptime{},
}
if eventsPageSize > 0 { if eventsPageSize > 0 {
if serviceStatus.Events, err = s.getEventsByServiceID(tx, serviceID, eventsPage, eventsPageSize); err != nil { if serviceStatus.Events, err = s.getEventsByServiceID(tx, serviceID, eventsPage, eventsPageSize); err != nil {
log.Printf("[database][getServiceStatusByKey] Failed to retrieve events for key=%s: %s", key, err.Error()) log.Printf("[database][getServiceStatusByKey] Failed to retrieve events for key=%s: %s", key, err.Error())

View File

@ -3,6 +3,7 @@ package memory
import ( import (
"encoding/gob" "encoding/gob"
"sync" "sync"
"time"
"github.com/TwinProduction/gatus/core" "github.com/TwinProduction/gatus/core"
"github.com/TwinProduction/gatus/util" "github.com/TwinProduction/gatus/util"
@ -69,7 +70,11 @@ func (s *Store) Insert(service *core.Service, result *core.Result) {
s.Lock() s.Lock()
serviceStatus, exists := s.cache.Get(key) serviceStatus, exists := s.cache.Get(key)
if !exists { if !exists {
serviceStatus = core.NewServiceStatus(service) serviceStatus = core.NewServiceStatus(key, service.Group, service.Name)
serviceStatus.(*core.ServiceStatus).Events = append(serviceStatus.(*core.ServiceStatus).Events, &core.Event{
Type: core.EventStart,
Timestamp: time.Now(),
})
} }
serviceStatus.(*core.ServiceStatus).AddResult(result) serviceStatus.(*core.ServiceStatus).AddResult(result)
s.cache.Set(key, serviceStatus) s.cache.Set(key, serviceStatus)