diff --git a/config/config.go b/config/config.go index 101ccb95..58f59606 100644 --- a/config/config.go +++ b/config/config.go @@ -14,7 +14,6 @@ import ( "github.com/TwinProduction/gatus/k8s" "github.com/TwinProduction/gatus/security" "github.com/TwinProduction/gatus/storage" - "github.com/TwinProduction/gatus/util" "gopkg.in/yaml.v2" ) @@ -186,7 +185,7 @@ func validateStorageConfig(config *Config) error { // Remove all ServiceStatus that represent services which no longer exist in the configuration var keys []string for _, service := range config.Services { - keys = append(keys, util.ConvertGroupAndServiceToKey(service.Group, service.Name)) + keys = append(keys, service.Key()) } numberOfServiceStatusesDeleted := storage.Get().DeleteAllServiceStatusesNotInKeys(keys) if numberOfServiceStatusesDeleted > 0 { diff --git a/core/service.go b/core/service.go index 067ea26c..9ff23311 100644 --- a/core/service.go +++ b/core/service.go @@ -14,6 +14,7 @@ import ( "github.com/TwinProduction/gatus/alerting/alert" "github.com/TwinProduction/gatus/client" + "github.com/TwinProduction/gatus/util" ) const ( @@ -135,6 +136,11 @@ func (service *Service) ValidateAndSetDefaults() error { return nil } +// Key returns the unique key for the Service +func (service Service) Key() string { + return util.ConvertGroupAndServiceToKey(service.Group, service.Name) +} + // EvaluateHealth sends a request to the service's URL and evaluates the conditions of the service. func (service *Service) EvaluateHealth() *Result { result := &Result{Success: true, Errors: []string{}} diff --git a/core/service_status.go b/core/service_status.go index d1e103c5..58dfe12c 100644 --- a/core/service_status.go +++ b/core/service_status.go @@ -2,8 +2,6 @@ package core import ( "time" - - "github.com/TwinProduction/gatus/util" ) const ( @@ -48,7 +46,7 @@ func NewServiceStatus(service *Service) *ServiceStatus { return &ServiceStatus{ Name: service.Name, Group: service.Group, - Key: util.ConvertGroupAndServiceToKey(service.Group, service.Name), + Key: service.Key(), Results: make([]*Result, 0), Events: []*Event{{ Type: EventStart, diff --git a/storage/store/memory/memory.go b/storage/store/memory/memory.go index 0b23d3f9..d859fc3a 100644 --- a/storage/store/memory/memory.go +++ b/storage/store/memory/memory.go @@ -63,7 +63,7 @@ func (s *Store) GetServiceStatusByKey(key string) *core.ServiceStatus { // Insert adds the observed result for the specified service into the store func (s *Store) Insert(service *core.Service, result *core.Result) { - key := util.ConvertGroupAndServiceToKey(service.Group, service.Name) + key := service.Key() serviceStatus, exists := s.cache.Get(key) if !exists { serviceStatus = core.NewServiceStatus(service) diff --git a/storage/store/memory/memory_test.go b/storage/store/memory/memory_test.go index ecdb51e8..584884b9 100644 --- a/storage/store/memory/memory_test.go +++ b/storage/store/memory/memory_test.go @@ -6,7 +6,6 @@ import ( "time" "github.com/TwinProduction/gatus/core" - "github.com/TwinProduction/gatus/util" ) var ( @@ -181,7 +180,7 @@ func TestStore_GetServiceStatusByKey(t *testing.T) { store.Insert(&testService, &testSuccessfulResult) store.Insert(&testService, &testUnsuccessfulResult) - serviceStatus := store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(testService.Group, testService.Name)) + serviceStatus := store.GetServiceStatusByKey(testService.Key()) if serviceStatus == nil { t.Fatalf("serviceStatus shouldn't have been nil") } @@ -212,7 +211,7 @@ func TestStore_GetAllServiceStatusesWithResultPagination(t *testing.T) { if len(serviceStatuses) != 1 { t.Fatal("expected 1 service status") } - actual, exists := serviceStatuses[util.ConvertGroupAndServiceToKey(testService.Group, testService.Name)] + actual, exists := serviceStatuses[testService.Key()] if !exists { t.Fatal("expected service status to exist") } @@ -234,20 +233,20 @@ func TestStore_DeleteAllServiceStatusesNotInKeys(t *testing.T) { if store.cache.Count() != 2 { t.Errorf("expected cache to have 2 keys, got %d", store.cache.Count()) } - if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(firstService.Group, firstService.Name)) == nil { + if store.GetServiceStatusByKey(firstService.Key()) == nil { t.Fatal("firstService should exist") } - if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(secondService.Group, secondService.Name)) == nil { + if store.GetServiceStatusByKey(secondService.Key()) == nil { t.Fatal("secondService should exist") } - store.DeleteAllServiceStatusesNotInKeys([]string{util.ConvertGroupAndServiceToKey(firstService.Group, firstService.Name)}) + store.DeleteAllServiceStatusesNotInKeys([]string{firstService.Key()}) if store.cache.Count() != 1 { t.Fatalf("expected cache to have 1 keys, got %d", store.cache.Count()) } - if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(firstService.Group, firstService.Name)) == nil { + if store.GetServiceStatusByKey(firstService.Key()) == nil { t.Error("secondService should've been deleted") } - if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(secondService.Group, secondService.Name)) != nil { + if store.GetServiceStatusByKey(secondService.Key()) != nil { t.Error("firstService should still exist") } }