2021-02-03 05:06:34 +01:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
2021-09-03 05:09:29 +02:00
|
|
|
"sort"
|
2021-07-13 04:53:35 +02:00
|
|
|
"sync"
|
2021-07-14 07:53:14 +02:00
|
|
|
"time"
|
2021-02-03 05:06:34 +01:00
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
"github.com/TwiN/gatus/v5/config/endpoint"
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/storage/store/common"
|
|
|
|
"github.com/TwiN/gatus/v5/storage/store/common/paging"
|
2022-06-15 05:36:18 +02:00
|
|
|
"github.com/TwiN/gocache/v2"
|
2021-02-03 05:06:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Store that leverages gocache
|
|
|
|
type Store struct {
|
2021-07-13 04:53:35 +02:00
|
|
|
sync.RWMutex
|
2022-06-15 05:36:18 +02:00
|
|
|
|
2021-02-03 05:06:34 +01:00
|
|
|
cache *gocache.Cache
|
|
|
|
}
|
|
|
|
|
2021-07-18 02:18:27 +02:00
|
|
|
// NewStore creates a new store using gocache.Cache
|
|
|
|
//
|
|
|
|
// This store holds everything in memory, and if the file parameter is not blank,
|
|
|
|
// supports eventual persistence.
|
2022-08-12 02:42:56 +02:00
|
|
|
func NewStore() (*Store, error) {
|
2021-02-03 05:06:34 +01:00
|
|
|
store := &Store{
|
|
|
|
cache: gocache.NewCache().WithMaxSize(gocache.NoMaxSize),
|
|
|
|
}
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
// GetAllEndpointStatuses returns all monitored endpoint.Status
|
|
|
|
// with a subset of endpoint.Result defined by the page and pageSize parameters
|
|
|
|
func (s *Store) GetAllEndpointStatuses(params *paging.EndpointStatusParams) ([]*endpoint.Status, error) {
|
2021-10-23 22:47:12 +02:00
|
|
|
endpointStatuses := s.cache.GetAll()
|
2024-05-10 04:56:16 +02:00
|
|
|
pagedEndpointStatuses := make([]*endpoint.Status, 0, len(endpointStatuses))
|
2021-10-23 22:47:12 +02:00
|
|
|
for _, v := range endpointStatuses {
|
2024-05-10 04:56:16 +02:00
|
|
|
pagedEndpointStatuses = append(pagedEndpointStatuses, ShallowCopyEndpointStatus(v.(*endpoint.Status), params))
|
2021-10-23 22:47:12 +02:00
|
|
|
}
|
|
|
|
sort.Slice(pagedEndpointStatuses, func(i, j int) bool {
|
|
|
|
return pagedEndpointStatuses[i].Key < pagedEndpointStatuses[j].Key
|
2021-09-03 05:09:29 +02:00
|
|
|
})
|
2021-10-23 22:47:12 +02:00
|
|
|
return pagedEndpointStatuses, nil
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
// GetEndpointStatus returns the endpoint status for a given endpoint name in the given group
|
2024-05-10 04:56:16 +02:00
|
|
|
func (s *Store) GetEndpointStatus(groupName, endpointName string, params *paging.EndpointStatusParams) (*endpoint.Status, error) {
|
|
|
|
return s.GetEndpointStatusByKey(endpoint.ConvertGroupAndEndpointNameToKey(groupName, endpointName), params)
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
// GetEndpointStatusByKey returns the endpoint status for a given key
|
2024-05-10 04:56:16 +02:00
|
|
|
func (s *Store) GetEndpointStatusByKey(key string, params *paging.EndpointStatusParams) (*endpoint.Status, error) {
|
2021-10-23 22:47:12 +02:00
|
|
|
endpointStatus := s.cache.GetValue(key)
|
|
|
|
if endpointStatus == nil {
|
|
|
|
return nil, common.ErrEndpointNotFound
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
return ShallowCopyEndpointStatus(endpointStatus.(*endpoint.Status), params), nil
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
|
|
|
|
2021-08-13 03:54:23 +02:00
|
|
|
// GetUptimeByKey returns the uptime percentage during a time range
|
|
|
|
func (s *Store) GetUptimeByKey(key string, from, to time.Time) (float64, error) {
|
|
|
|
if from.After(to) {
|
|
|
|
return 0, common.ErrInvalidTimeRange
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
endpointStatus := s.cache.GetValue(key)
|
2024-05-10 04:56:16 +02:00
|
|
|
if endpointStatus == nil || endpointStatus.(*endpoint.Status).Uptime == nil {
|
2021-10-23 22:47:12 +02:00
|
|
|
return 0, common.ErrEndpointNotFound
|
2021-08-13 03:54:23 +02:00
|
|
|
}
|
|
|
|
successfulExecutions := uint64(0)
|
|
|
|
totalExecutions := uint64(0)
|
|
|
|
current := from
|
|
|
|
for to.Sub(current) >= 0 {
|
|
|
|
hourlyUnixTimestamp := current.Truncate(time.Hour).Unix()
|
2024-05-10 04:56:16 +02:00
|
|
|
hourlyStats := endpointStatus.(*endpoint.Status).Uptime.HourlyStatistics[hourlyUnixTimestamp]
|
2021-08-13 03:54:23 +02:00
|
|
|
if hourlyStats == nil || hourlyStats.TotalExecutions == 0 {
|
|
|
|
current = current.Add(time.Hour)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
successfulExecutions += hourlyStats.SuccessfulExecutions
|
|
|
|
totalExecutions += hourlyStats.TotalExecutions
|
|
|
|
current = current.Add(time.Hour)
|
|
|
|
}
|
|
|
|
if totalExecutions == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return float64(successfulExecutions) / float64(totalExecutions), nil
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:59:09 +02:00
|
|
|
// GetAverageResponseTimeByKey returns the average response time in milliseconds (value) during a time range
|
|
|
|
func (s *Store) GetAverageResponseTimeByKey(key string, from, to time.Time) (int, error) {
|
|
|
|
if from.After(to) {
|
|
|
|
return 0, common.ErrInvalidTimeRange
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
endpointStatus := s.cache.GetValue(key)
|
2024-05-10 04:56:16 +02:00
|
|
|
if endpointStatus == nil || endpointStatus.(*endpoint.Status).Uptime == nil {
|
2021-10-23 22:47:12 +02:00
|
|
|
return 0, common.ErrEndpointNotFound
|
2021-08-21 16:59:09 +02:00
|
|
|
}
|
|
|
|
current := from
|
|
|
|
var totalExecutions, totalResponseTime uint64
|
|
|
|
for to.Sub(current) >= 0 {
|
|
|
|
hourlyUnixTimestamp := current.Truncate(time.Hour).Unix()
|
2024-05-10 04:56:16 +02:00
|
|
|
hourlyStats := endpointStatus.(*endpoint.Status).Uptime.HourlyStatistics[hourlyUnixTimestamp]
|
2021-08-21 16:59:09 +02:00
|
|
|
if hourlyStats == nil || hourlyStats.TotalExecutions == 0 {
|
|
|
|
current = current.Add(time.Hour)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
totalExecutions += hourlyStats.TotalExecutions
|
|
|
|
totalResponseTime += hourlyStats.TotalExecutionsResponseTime
|
|
|
|
current = current.Add(time.Hour)
|
|
|
|
}
|
|
|
|
if totalExecutions == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return int(float64(totalResponseTime) / float64(totalExecutions)), nil
|
|
|
|
}
|
|
|
|
|
2021-08-20 05:07:21 +02:00
|
|
|
// GetHourlyAverageResponseTimeByKey returns a map of hourly (key) average response time in milliseconds (value) during a time range
|
|
|
|
func (s *Store) GetHourlyAverageResponseTimeByKey(key string, from, to time.Time) (map[int64]int, error) {
|
|
|
|
if from.After(to) {
|
|
|
|
return nil, common.ErrInvalidTimeRange
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
endpointStatus := s.cache.GetValue(key)
|
2024-05-10 04:56:16 +02:00
|
|
|
if endpointStatus == nil || endpointStatus.(*endpoint.Status).Uptime == nil {
|
2021-10-23 22:47:12 +02:00
|
|
|
return nil, common.ErrEndpointNotFound
|
2021-08-20 05:07:21 +02:00
|
|
|
}
|
|
|
|
hourlyAverageResponseTimes := make(map[int64]int)
|
|
|
|
current := from
|
|
|
|
for to.Sub(current) >= 0 {
|
|
|
|
hourlyUnixTimestamp := current.Truncate(time.Hour).Unix()
|
2024-05-10 04:56:16 +02:00
|
|
|
hourlyStats := endpointStatus.(*endpoint.Status).Uptime.HourlyStatistics[hourlyUnixTimestamp]
|
2021-08-20 05:07:21 +02:00
|
|
|
if hourlyStats == nil || hourlyStats.TotalExecutions == 0 {
|
|
|
|
current = current.Add(time.Hour)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hourlyAverageResponseTimes[hourlyUnixTimestamp] = int(float64(hourlyStats.TotalExecutionsResponseTime) / float64(hourlyStats.TotalExecutions))
|
|
|
|
current = current.Add(time.Hour)
|
|
|
|
}
|
|
|
|
return hourlyAverageResponseTimes, nil
|
|
|
|
}
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
// Insert adds the observed result for the specified endpoint into the store
|
2024-05-10 04:56:16 +02:00
|
|
|
func (s *Store) Insert(ep *endpoint.Endpoint, result *endpoint.Result) error {
|
|
|
|
key := ep.Key()
|
2021-07-13 04:53:35 +02:00
|
|
|
s.Lock()
|
2021-10-23 22:47:12 +02:00
|
|
|
status, exists := s.cache.Get(key)
|
2021-02-03 05:06:34 +01:00
|
|
|
if !exists {
|
2024-05-10 04:56:16 +02:00
|
|
|
status = endpoint.NewStatus(ep.Group, ep.Name)
|
|
|
|
status.(*endpoint.Status).Events = append(status.(*endpoint.Status).Events, &endpoint.Event{
|
|
|
|
Type: endpoint.EventStart,
|
2021-07-14 07:53:14 +02:00
|
|
|
Timestamp: time.Now(),
|
|
|
|
})
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
2024-05-10 04:56:16 +02:00
|
|
|
AddResult(status.(*endpoint.Status), result)
|
2021-10-23 22:47:12 +02:00
|
|
|
s.cache.Set(key, status)
|
2021-07-13 04:53:35 +02:00
|
|
|
s.Unlock()
|
2021-09-11 00:00:04 +02:00
|
|
|
return nil
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
|
|
|
|
2024-05-10 04:56:16 +02:00
|
|
|
// DeleteAllEndpointStatusesNotInKeys removes all Status that are not within the keys provided
|
2021-10-23 22:47:12 +02:00
|
|
|
func (s *Store) DeleteAllEndpointStatusesNotInKeys(keys []string) int {
|
2021-02-03 05:06:34 +01:00
|
|
|
var keysToDelete []string
|
|
|
|
for _, existingKey := range s.cache.GetKeysByPattern("*", 0) {
|
|
|
|
shouldDelete := true
|
|
|
|
for _, key := range keys {
|
|
|
|
if existingKey == key {
|
|
|
|
shouldDelete = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if shouldDelete {
|
|
|
|
keysToDelete = append(keysToDelete, existingKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.cache.DeleteAll(keysToDelete)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear deletes everything from the store
|
|
|
|
func (s *Store) Clear() {
|
|
|
|
s.cache.Clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save persists the cache to the store file
|
|
|
|
func (s *Store) Save() error {
|
2021-02-06 02:45:28 +01:00
|
|
|
return nil
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
2021-07-16 04:07:30 +02:00
|
|
|
|
|
|
|
// Close does nothing, because there's nothing to close
|
|
|
|
func (s *Store) Close() {
|
|
|
|
return
|
|
|
|
}
|