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/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 {

View File

@ -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{}}

View File

@ -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,

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
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)

View File

@ -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")
}
}