2021-07-15 04:26:51 +02:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
2021-10-08 03:28:04 +02:00
|
|
|
"github.com/TwiN/gatus/v3/core"
|
|
|
|
"github.com/TwiN/gatus/v3/storage/store/common"
|
|
|
|
"github.com/TwiN/gatus/v3/storage/store/common/paging"
|
2021-07-15 04:26:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ShallowCopyServiceStatus returns a shallow copy of a ServiceStatus with only the results
|
|
|
|
// within the range defined by the page and pageSize parameters
|
|
|
|
func ShallowCopyServiceStatus(ss *core.ServiceStatus, params *paging.ServiceStatusParams) *core.ServiceStatus {
|
|
|
|
shallowCopy := &core.ServiceStatus{
|
|
|
|
Name: ss.Name,
|
|
|
|
Group: ss.Group,
|
|
|
|
Key: ss.Key,
|
|
|
|
Uptime: core.NewUptime(),
|
|
|
|
}
|
|
|
|
numberOfResults := len(ss.Results)
|
|
|
|
resultsStart, resultsEnd := getStartAndEndIndex(numberOfResults, params.ResultsPage, params.ResultsPageSize)
|
|
|
|
if resultsStart < 0 || resultsEnd < 0 {
|
|
|
|
shallowCopy.Results = []*core.Result{}
|
|
|
|
} else {
|
|
|
|
shallowCopy.Results = ss.Results[resultsStart:resultsEnd]
|
|
|
|
}
|
|
|
|
numberOfEvents := len(ss.Events)
|
|
|
|
eventsStart, eventsEnd := getStartAndEndIndex(numberOfEvents, params.EventsPage, params.EventsPageSize)
|
|
|
|
if eventsStart < 0 || eventsEnd < 0 {
|
|
|
|
shallowCopy.Events = []*core.Event{}
|
|
|
|
} else {
|
|
|
|
shallowCopy.Events = ss.Events[eventsStart:eventsEnd]
|
|
|
|
}
|
|
|
|
return shallowCopy
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStartAndEndIndex(numberOfResults int, page, pageSize int) (int, int) {
|
2021-07-16 04:07:30 +02:00
|
|
|
if page < 1 || pageSize < 0 {
|
|
|
|
return -1, -1
|
|
|
|
}
|
2021-07-15 04:26:51 +02:00
|
|
|
start := numberOfResults - (page * pageSize)
|
|
|
|
end := numberOfResults - ((page - 1) * pageSize)
|
|
|
|
if start > numberOfResults {
|
|
|
|
start = -1
|
|
|
|
} else if start < 0 {
|
|
|
|
start = 0
|
|
|
|
}
|
|
|
|
if end > numberOfResults {
|
|
|
|
end = numberOfResults
|
|
|
|
}
|
|
|
|
return start, end
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddResult adds a Result to ServiceStatus.Results and makes sure that there are
|
|
|
|
// no more than MaximumNumberOfResults results in the Results slice
|
|
|
|
func AddResult(ss *core.ServiceStatus, result *core.Result) {
|
|
|
|
if ss == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(ss.Results) > 0 {
|
|
|
|
// Check if there's any change since the last result
|
2021-07-17 01:12:14 +02:00
|
|
|
if ss.Results[len(ss.Results)-1].Success != result.Success {
|
2021-07-17 01:51:09 +02:00
|
|
|
ss.Events = append(ss.Events, core.NewEventFromResult(result))
|
2021-08-13 03:54:23 +02:00
|
|
|
if len(ss.Events) > common.MaximumNumberOfEvents {
|
2021-07-15 04:26:51 +02:00
|
|
|
// Doing ss.Events[1:] would usually be sufficient, but in the case where for some reason, the slice has
|
|
|
|
// more than one extra element, we can get rid of all of them at once and thus returning the slice to a
|
|
|
|
// length of MaximumNumberOfEvents by using ss.Events[len(ss.Events)-MaximumNumberOfEvents:] instead
|
2021-08-13 03:54:23 +02:00
|
|
|
ss.Events = ss.Events[len(ss.Events)-common.MaximumNumberOfEvents:]
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-17 01:12:14 +02:00
|
|
|
} else {
|
|
|
|
// This is the first result, so we need to add the first healthy/unhealthy event
|
2021-07-17 01:51:09 +02:00
|
|
|
ss.Events = append(ss.Events, core.NewEventFromResult(result))
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
|
|
|
ss.Results = append(ss.Results, result)
|
2021-08-13 03:54:23 +02:00
|
|
|
if len(ss.Results) > common.MaximumNumberOfResults {
|
2021-07-15 04:26:51 +02:00
|
|
|
// Doing ss.Results[1:] would usually be sufficient, but in the case where for some reason, the slice has more
|
|
|
|
// than one extra element, we can get rid of all of them at once and thus returning the slice to a length of
|
|
|
|
// MaximumNumberOfResults by using ss.Results[len(ss.Results)-MaximumNumberOfResults:] instead
|
2021-08-13 03:54:23 +02:00
|
|
|
ss.Results = ss.Results[len(ss.Results)-common.MaximumNumberOfResults:]
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
|
|
|
processUptimeAfterResult(ss.Uptime, result)
|
|
|
|
}
|