2021-02-03 05:06:34 +01:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2021-10-29 01:35:46 +02:00
|
|
|
"context"
|
|
|
|
"log"
|
2021-08-20 05:07:21 +02:00
|
|
|
"time"
|
|
|
|
|
2022-12-06 07:41:09 +01:00
|
|
|
"github.com/TwiN/gatus/v5/core"
|
|
|
|
"github.com/TwiN/gatus/v5/storage"
|
|
|
|
"github.com/TwiN/gatus/v5/storage/store/common/paging"
|
|
|
|
"github.com/TwiN/gatus/v5/storage/store/memory"
|
|
|
|
"github.com/TwiN/gatus/v5/storage/store/sql"
|
2021-02-03 05:06:34 +01:00
|
|
|
)
|
|
|
|
|
2022-08-12 02:47:29 +02:00
|
|
|
// Store is the interface that each store should implement
|
2021-02-03 05:06:34 +01:00
|
|
|
type Store interface {
|
2021-10-23 22:47:12 +02:00
|
|
|
// GetAllEndpointStatuses returns the JSON encoding of all monitored core.EndpointStatus
|
2021-02-25 04:41:36 +01:00
|
|
|
// with a subset of core.Result defined by the page and pageSize parameters
|
2021-10-23 22:47:12 +02:00
|
|
|
GetAllEndpointStatuses(params *paging.EndpointStatusParams) ([]*core.EndpointStatus, error)
|
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
|
|
|
|
GetEndpointStatus(groupName, endpointName string, params *paging.EndpointStatusParams) (*core.EndpointStatus, error)
|
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
|
|
|
|
GetEndpointStatusByKey(key string, params *paging.EndpointStatusParams) (*core.EndpointStatus, error)
|
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
|
|
|
|
GetUptimeByKey(key string, from, to time.Time) (float64, error)
|
|
|
|
|
2021-08-21 16:59:09 +02:00
|
|
|
// GetAverageResponseTimeByKey returns the average response time in milliseconds (value) during a time range
|
|
|
|
GetAverageResponseTimeByKey(key string, from, to time.Time) (int, error)
|
|
|
|
|
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
|
|
|
|
GetHourlyAverageResponseTimeByKey(key string, from, to time.Time) (map[int64]int, error)
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
// Insert adds the observed result for the specified endpoint into the store
|
|
|
|
Insert(endpoint *core.Endpoint, result *core.Result) error
|
2021-02-03 05:06:34 +01:00
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
// DeleteAllEndpointStatusesNotInKeys removes all EndpointStatus that are not within the keys provided
|
2021-02-03 05:06:34 +01:00
|
|
|
//
|
2021-10-23 22:47:12 +02:00
|
|
|
// Used to delete endpoints that have been persisted but are no longer part of the configured endpoints
|
|
|
|
DeleteAllEndpointStatusesNotInKeys(keys []string) int
|
2021-02-03 05:06:34 +01:00
|
|
|
|
|
|
|
// Clear deletes everything from the store
|
|
|
|
Clear()
|
2021-02-06 02:45:28 +01:00
|
|
|
|
|
|
|
// Save persists the data if and where it needs to be persisted
|
|
|
|
Save() error
|
2021-07-16 04:07:30 +02:00
|
|
|
|
2021-09-11 00:00:04 +02:00
|
|
|
// Close terminates every connection and closes the store, if applicable.
|
2021-07-16 04:07:30 +02:00
|
|
|
// Should only be used before stopping the application.
|
|
|
|
Close()
|
2021-02-03 05:06:34 +01:00
|
|
|
}
|
|
|
|
|
2021-07-13 04:53:14 +02:00
|
|
|
// TODO: add method to check state of store (by keeping track of silent errors)
|
|
|
|
|
2021-02-03 05:06:34 +01:00
|
|
|
var (
|
|
|
|
// Validate interface implementation on compile
|
|
|
|
_ Store = (*memory.Store)(nil)
|
2021-09-11 00:00:04 +02:00
|
|
|
_ Store = (*sql.Store)(nil)
|
2021-02-03 05:06:34 +01:00
|
|
|
)
|
2021-10-29 01:35:46 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
store Store
|
|
|
|
|
|
|
|
// initialized keeps track of whether the storage provider was initialized
|
|
|
|
// Because store.Store is an interface, a nil check wouldn't be sufficient, so instead of doing reflection
|
|
|
|
// every single time Get is called, we'll just lazily keep track of its existence through this variable
|
|
|
|
initialized bool
|
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
cancelFunc context.CancelFunc
|
|
|
|
)
|
|
|
|
|
|
|
|
func Get() Store {
|
|
|
|
if !initialized {
|
2021-11-09 06:06:41 +01:00
|
|
|
// This only happens in tests
|
2024-04-02 03:47:14 +02:00
|
|
|
log.Println("[store.Get] Provider requested before it was initialized, automatically initializing")
|
2021-10-29 01:35:46 +02:00
|
|
|
err := Initialize(nil)
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to automatically initialize store: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return store
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize instantiates the storage provider based on the Config provider
|
|
|
|
func Initialize(cfg *storage.Config) error {
|
|
|
|
initialized = true
|
|
|
|
var err error
|
|
|
|
if cancelFunc != nil {
|
|
|
|
// Stop the active autoSave task, if there's already one
|
|
|
|
cancelFunc()
|
|
|
|
}
|
|
|
|
if cfg == nil {
|
2021-11-09 06:06:41 +01:00
|
|
|
// This only happens in tests
|
2024-04-02 03:47:14 +02:00
|
|
|
log.Println("[store.Initialize] nil storage config passed as parameter. This should only happen in tests. Defaulting to an empty config.")
|
2021-10-29 01:35:46 +02:00
|
|
|
cfg = &storage.Config{}
|
|
|
|
}
|
2021-11-05 02:33:13 +01:00
|
|
|
if len(cfg.Path) == 0 && cfg.Type != storage.TypePostgres {
|
2024-04-02 03:47:14 +02:00
|
|
|
log.Printf("[store.Initialize] Creating storage provider of type=%s", cfg.Type)
|
2021-10-29 01:35:46 +02:00
|
|
|
}
|
|
|
|
ctx, cancelFunc = context.WithCancel(context.Background())
|
|
|
|
switch cfg.Type {
|
|
|
|
case storage.TypeSQLite, storage.TypePostgres:
|
2022-08-12 02:47:29 +02:00
|
|
|
store, err = sql.NewStore(string(cfg.Type), cfg.Path, cfg.Caching)
|
2021-10-29 01:35:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case storage.TypeMemory:
|
|
|
|
fallthrough
|
|
|
|
default:
|
2022-08-12 02:42:56 +02:00
|
|
|
store, _ = memory.NewStore()
|
2021-10-29 01:35:46 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// autoSave automatically calls the Save function of the provider at every interval
|
|
|
|
func autoSave(ctx context.Context, store Store, interval time.Duration) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2024-04-02 03:47:14 +02:00
|
|
|
log.Printf("[store.autoSave] Stopping active job")
|
2021-10-29 01:35:46 +02:00
|
|
|
return
|
|
|
|
case <-time.After(interval):
|
2024-04-02 03:47:14 +02:00
|
|
|
log.Printf("[store.autoSave] Saving")
|
2021-10-29 01:35:46 +02:00
|
|
|
err := store.Save()
|
|
|
|
if err != nil {
|
2024-04-02 03:47:14 +02:00
|
|
|
log.Println("[store.autoSave] Save failed:", err.Error())
|
2021-10-29 01:35:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|