Return array instead of map on /api/v1/services/statuses

This commit is contained in:
TwinProduction 2021-09-02 14:57:50 -04:00 committed by Chris
parent 44c36a8a5e
commit c61b406483
6 changed files with 23 additions and 23 deletions

View File

@ -388,33 +388,33 @@ func TestServiceStatusesHandler(t *testing.T) {
scenarios := []Scenario{ scenarios := []Scenario{
{ {
Name: "no-pagination", Name: "no-pagination",
Path: "/api/v1/statuses", Path: "/api/v1/services/statuses",
ExpectedCode: http.StatusOK, ExpectedCode: http.StatusOK,
ExpectedBody: `{"group_name":{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"errors":null,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"},{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}}`, ExpectedBody: `[{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"errors":null,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"},{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}]`,
}, },
{ {
Name: "pagination-first-result", Name: "pagination-first-result",
Path: "/api/v1/statuses?page=1&pageSize=1", Path: "/api/v1/services/statuses?page=1&pageSize=1",
ExpectedCode: http.StatusOK, ExpectedCode: http.StatusOK,
ExpectedBody: `{"group_name":{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}}`, ExpectedBody: `[{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}]`,
}, },
{ {
Name: "pagination-second-result", Name: "pagination-second-result",
Path: "/api/v1/statuses?page=2&pageSize=1", Path: "/api/v1/services/statuses?page=2&pageSize=1",
ExpectedCode: http.StatusOK, ExpectedCode: http.StatusOK,
ExpectedBody: `{"group_name":{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"errors":null,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"}]}}`, ExpectedBody: `[{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"errors":null,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"}]}]`,
}, },
{ {
Name: "pagination-no-results", Name: "pagination-no-results",
Path: "/api/v1/statuses?page=5&pageSize=20", Path: "/api/v1/services/statuses?page=5&pageSize=20",
ExpectedCode: http.StatusOK, ExpectedCode: http.StatusOK,
ExpectedBody: `{"group_name":{"name":"name","group":"group","key":"group_name","results":[]}}`, ExpectedBody: `[{"name":"name","group":"group","key":"group_name","results":[]}]`,
}, },
{ {
Name: "invalid-pagination-should-fall-back-to-default", Name: "invalid-pagination-should-fall-back-to-default",
Path: "/api/v1/statuses?page=INVALID&pageSize=INVALID", Path: "/api/v1/services/statuses?page=INVALID&pageSize=INVALID",
ExpectedCode: http.StatusOK, ExpectedCode: http.StatusOK,
ExpectedBody: `{"group_name":{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"errors":null,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"},{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}}`, ExpectedBody: `[{"name":"name","group":"group","key":"group_name","results":[{"status":200,"hostname":"example.org","duration":150000000,"errors":null,"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":true},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":true}],"success":true,"timestamp":"0001-01-01T00:00:00Z"},{"status":200,"hostname":"example.org","duration":750000000,"errors":["error-1","error-2"],"conditionResults":[{"condition":"[STATUS] == 200","success":true},{"condition":"[RESPONSE_TIME] \u003c 500","success":false},{"condition":"[CERTIFICATE_EXPIRATION] \u003c 72h","success":false}],"success":false,"timestamp":"0001-01-01T00:00:00Z"}]}]`,
}, },
} }

View File

@ -47,11 +47,11 @@ func NewStore(file string) (*Store, error) {
// GetAllServiceStatuses returns all monitored core.ServiceStatus // GetAllServiceStatuses returns all monitored core.ServiceStatus
// with a subset of core.Result defined by the page and pageSize parameters // with a subset of core.Result defined by the page and pageSize parameters
func (s *Store) GetAllServiceStatuses(params *paging.ServiceStatusParams) map[string]*core.ServiceStatus { func (s *Store) GetAllServiceStatuses(params *paging.ServiceStatusParams) []*core.ServiceStatus {
serviceStatuses := s.cache.GetAll() serviceStatuses := s.cache.GetAll()
pagedServiceStatuses := make(map[string]*core.ServiceStatus, len(serviceStatuses)) pagedServiceStatuses := make([]*core.ServiceStatus, 0, len(serviceStatuses))
for k, v := range serviceStatuses { for _, v := range serviceStatuses {
pagedServiceStatuses[k] = ShallowCopyServiceStatus(v.(*core.ServiceStatus), params) pagedServiceStatuses = append(pagedServiceStatuses, ShallowCopyServiceStatus(v.(*core.ServiceStatus), params))
} }
return pagedServiceStatuses return pagedServiceStatuses
} }

View File

@ -148,7 +148,7 @@ func (s *Store) createSchema() error {
// GetAllServiceStatuses returns all monitored core.ServiceStatus // GetAllServiceStatuses returns all monitored core.ServiceStatus
// with a subset of core.Result defined by the page and pageSize parameters // with a subset of core.Result defined by the page and pageSize parameters
func (s *Store) GetAllServiceStatuses(params *paging.ServiceStatusParams) map[string]*core.ServiceStatus { func (s *Store) GetAllServiceStatuses(params *paging.ServiceStatusParams) []*core.ServiceStatus {
tx, err := s.db.Begin() tx, err := s.db.Begin()
if err != nil { if err != nil {
return nil return nil
@ -158,13 +158,13 @@ func (s *Store) GetAllServiceStatuses(params *paging.ServiceStatusParams) map[st
_ = tx.Rollback() _ = tx.Rollback()
return nil return nil
} }
serviceStatuses := make(map[string]*core.ServiceStatus, len(keys)) serviceStatuses := make([]*core.ServiceStatus, 0, len(keys))
for _, key := range keys { for _, key := range keys {
serviceStatus, err := s.getServiceStatusByKey(tx, key, params) serviceStatus, err := s.getServiceStatusByKey(tx, key, params)
if err != nil { if err != nil {
continue continue
} }
serviceStatuses[key] = serviceStatus serviceStatuses = append(serviceStatuses, serviceStatus)
} }
if err = tx.Commit(); err != nil { if err = tx.Commit(); err != nil {
_ = tx.Rollback() _ = tx.Rollback()

View File

@ -13,7 +13,7 @@ import (
type Store interface { type Store interface {
// GetAllServiceStatuses returns the JSON encoding of all monitored core.ServiceStatus // GetAllServiceStatuses returns the JSON encoding of all monitored core.ServiceStatus
// with a subset of core.Result defined by the page and pageSize parameters // with a subset of core.Result defined by the page and pageSize parameters
GetAllServiceStatuses(params *paging.ServiceStatusParams) map[string]*core.ServiceStatus GetAllServiceStatuses(params *paging.ServiceStatusParams) []*core.ServiceStatus
// GetServiceStatus returns the service status for a given service name in the given group // GetServiceStatus returns the service status for a given service name in the given group
GetServiceStatus(groupName, serviceName string, params *paging.ServiceStatusParams) *core.ServiceStatus GetServiceStatus(groupName, serviceName string, params *paging.ServiceStatusParams) *core.ServiceStatus

View File

@ -184,8 +184,8 @@ func TestStore_GetAllServiceStatuses(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[testService.Key()] actual := serviceStatuses[0]
if !exists { if actual == nil {
t.Fatal("expected service status to exist") t.Fatal("expected service status to exist")
} }
if len(actual.Results) != 2 { if len(actual.Results) != 2 {
@ -213,8 +213,8 @@ func TestStore_GetAllServiceStatusesWithResultsAndEvents(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[testService.Key()] actual := serviceStatuses[0]
if !exists { if actual == nil {
t.Fatal("expected service status to exist") t.Fatal("expected service status to exist")
} }
if len(actual.Results) != 2 { if len(actual.Results) != 2 {

View File

@ -47,7 +47,7 @@ export default {
}, },
data() { data() {
return { return {
serviceStatuses: {}, serviceStatuses: [],
currentPage: 1, currentPage: 1,
showAverageResponseTime: true showAverageResponseTime: true
} }