Add Service.Key() method to generate the unique service key

This commit is contained in:
TwinProduction 2021-07-11 22:22:21 -04:00 committed by Chris
parent 7aed826d65
commit 1498b6d8a2
5 changed files with 16 additions and 14 deletions

View File

@ -14,7 +14,6 @@ import (
"github.com/TwinProduction/gatus/k8s" "github.com/TwinProduction/gatus/k8s"
"github.com/TwinProduction/gatus/security" "github.com/TwinProduction/gatus/security"
"github.com/TwinProduction/gatus/storage" "github.com/TwinProduction/gatus/storage"
"github.com/TwinProduction/gatus/util"
"gopkg.in/yaml.v2" "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 // Remove all ServiceStatus that represent services which no longer exist in the configuration
var keys []string var keys []string
for _, service := range config.Services { for _, service := range config.Services {
keys = append(keys, util.ConvertGroupAndServiceToKey(service.Group, service.Name)) keys = append(keys, service.Key())
} }
numberOfServiceStatusesDeleted := storage.Get().DeleteAllServiceStatusesNotInKeys(keys) numberOfServiceStatusesDeleted := storage.Get().DeleteAllServiceStatusesNotInKeys(keys)
if numberOfServiceStatusesDeleted > 0 { if numberOfServiceStatusesDeleted > 0 {

View File

@ -14,6 +14,7 @@ import (
"github.com/TwinProduction/gatus/alerting/alert" "github.com/TwinProduction/gatus/alerting/alert"
"github.com/TwinProduction/gatus/client" "github.com/TwinProduction/gatus/client"
"github.com/TwinProduction/gatus/util"
) )
const ( const (
@ -135,6 +136,11 @@ func (service *Service) ValidateAndSetDefaults() error {
return nil 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. // EvaluateHealth sends a request to the service's URL and evaluates the conditions of the service.
func (service *Service) EvaluateHealth() *Result { func (service *Service) EvaluateHealth() *Result {
result := &Result{Success: true, Errors: []string{}} result := &Result{Success: true, Errors: []string{}}

View File

@ -2,8 +2,6 @@ package core
import ( import (
"time" "time"
"github.com/TwinProduction/gatus/util"
) )
const ( const (
@ -48,7 +46,7 @@ func NewServiceStatus(service *Service) *ServiceStatus {
return &ServiceStatus{ return &ServiceStatus{
Name: service.Name, Name: service.Name,
Group: service.Group, Group: service.Group,
Key: util.ConvertGroupAndServiceToKey(service.Group, service.Name), Key: service.Key(),
Results: make([]*Result, 0), Results: make([]*Result, 0),
Events: []*Event{{ Events: []*Event{{
Type: EventStart, Type: EventStart,

View File

@ -63,7 +63,7 @@ func (s *Store) GetServiceStatusByKey(key string) *core.ServiceStatus {
// Insert adds the observed result for the specified service into the store // Insert adds the observed result for the specified service into the store
func (s *Store) Insert(service *core.Service, result *core.Result) { 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) serviceStatus, exists := s.cache.Get(key)
if !exists { if !exists {
serviceStatus = core.NewServiceStatus(service) serviceStatus = core.NewServiceStatus(service)

View File

@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/TwinProduction/gatus/core" "github.com/TwinProduction/gatus/core"
"github.com/TwinProduction/gatus/util"
) )
var ( var (
@ -181,7 +180,7 @@ func TestStore_GetServiceStatusByKey(t *testing.T) {
store.Insert(&testService, &testSuccessfulResult) store.Insert(&testService, &testSuccessfulResult)
store.Insert(&testService, &testUnsuccessfulResult) store.Insert(&testService, &testUnsuccessfulResult)
serviceStatus := store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(testService.Group, testService.Name)) serviceStatus := store.GetServiceStatusByKey(testService.Key())
if serviceStatus == nil { if serviceStatus == nil {
t.Fatalf("serviceStatus shouldn't have been nil") t.Fatalf("serviceStatus shouldn't have been nil")
} }
@ -212,7 +211,7 @@ func TestStore_GetAllServiceStatusesWithResultPagination(t *testing.T) {
if len(serviceStatuses) != 1 { if len(serviceStatuses) != 1 {
t.Fatal("expected 1 service status") t.Fatal("expected 1 service status")
} }
actual, exists := serviceStatuses[util.ConvertGroupAndServiceToKey(testService.Group, testService.Name)] actual, exists := serviceStatuses[testService.Key()]
if !exists { if !exists {
t.Fatal("expected service status to exist") t.Fatal("expected service status to exist")
} }
@ -234,20 +233,20 @@ func TestStore_DeleteAllServiceStatusesNotInKeys(t *testing.T) {
if store.cache.Count() != 2 { if store.cache.Count() != 2 {
t.Errorf("expected cache to have 2 keys, got %d", store.cache.Count()) 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") 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") t.Fatal("secondService should exist")
} }
store.DeleteAllServiceStatusesNotInKeys([]string{util.ConvertGroupAndServiceToKey(firstService.Group, firstService.Name)}) store.DeleteAllServiceStatusesNotInKeys([]string{firstService.Key()})
if store.cache.Count() != 1 { if store.cache.Count() != 1 {
t.Fatalf("expected cache to have 1 keys, got %d", store.cache.Count()) 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") 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") t.Error("firstService should still exist")
} }
} }