2022-09-26 13:55:05 -05:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-14 09:45:39 -06:00
|
|
|
"slices"
|
2022-09-26 13:55:05 -05:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
2023-01-25 11:29:57 -06:00
|
|
|
history_model "github.com/grafana/grafana/pkg/services/ngalert/state/historian/model"
|
2022-11-09 15:06:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/screenshot"
|
2022-09-26 13:55:05 -05:00
|
|
|
)
|
|
|
|
|
2022-10-06 01:22:58 -05:00
|
|
|
var _ InstanceStore = &FakeInstanceStore{}
|
|
|
|
|
2022-09-26 13:55:05 -05:00
|
|
|
type FakeInstanceStore struct {
|
|
|
|
mtx sync.Mutex
|
2024-02-14 09:45:39 -06:00
|
|
|
recordedOps []any
|
2022-09-26 13:55:05 -05:00
|
|
|
}
|
|
|
|
|
2022-12-06 12:07:39 -06:00
|
|
|
type FakeInstanceStoreOp struct {
|
|
|
|
Name string
|
2023-08-30 10:46:47 -05:00
|
|
|
Args []any
|
2022-12-06 12:07:39 -06:00
|
|
|
}
|
|
|
|
|
2024-02-14 09:45:39 -06:00
|
|
|
func (f *FakeInstanceStore) RecordedOps() []any {
|
|
|
|
f.mtx.Lock()
|
|
|
|
defer f.mtx.Unlock()
|
|
|
|
return slices.Clone(f.recordedOps)
|
|
|
|
}
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
func (f *FakeInstanceStore) ListAlertInstances(_ context.Context, q *models.ListAlertInstancesQuery) ([]*models.AlertInstance, error) {
|
2022-09-26 13:55:05 -05:00
|
|
|
f.mtx.Lock()
|
|
|
|
defer f.mtx.Unlock()
|
2024-02-14 09:45:39 -06:00
|
|
|
f.recordedOps = append(f.recordedOps, *q)
|
2023-03-28 03:34:35 -05:00
|
|
|
return nil, nil
|
2022-09-26 13:55:05 -05:00
|
|
|
}
|
|
|
|
|
2023-04-06 11:06:25 -05:00
|
|
|
func (f *FakeInstanceStore) SaveAlertInstance(_ context.Context, q models.AlertInstance) error {
|
2022-09-26 13:55:05 -05:00
|
|
|
f.mtx.Lock()
|
|
|
|
defer f.mtx.Unlock()
|
2024-02-14 09:45:39 -06:00
|
|
|
f.recordedOps = append(f.recordedOps, q)
|
2022-09-26 13:55:05 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FakeInstanceStore) FetchOrgIds(_ context.Context) ([]int64, error) { return []int64{}, nil }
|
|
|
|
|
2022-12-06 12:07:39 -06:00
|
|
|
func (f *FakeInstanceStore) DeleteAlertInstances(ctx context.Context, q ...models.AlertInstanceKey) error {
|
|
|
|
f.mtx.Lock()
|
|
|
|
defer f.mtx.Unlock()
|
2024-02-14 09:45:39 -06:00
|
|
|
f.recordedOps = append(f.recordedOps, FakeInstanceStoreOp{
|
2023-08-30 10:46:47 -05:00
|
|
|
Name: "DeleteAlertInstances", Args: []any{
|
2022-12-06 12:07:39 -06:00
|
|
|
ctx,
|
|
|
|
q,
|
|
|
|
},
|
|
|
|
})
|
2022-09-26 13:55:05 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FakeInstanceStore) DeleteAlertInstancesByRule(ctx context.Context, key models.AlertRuleKey) error {
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-27 10:33:32 -05:00
|
|
|
|
2024-01-22 06:07:11 -06:00
|
|
|
func (f *FakeInstanceStore) FullSync(ctx context.Context, instances []models.AlertInstance) error {
|
|
|
|
f.mtx.Lock()
|
|
|
|
defer f.mtx.Unlock()
|
2024-02-14 09:45:39 -06:00
|
|
|
f.recordedOps = []any{}
|
2024-01-22 06:07:11 -06:00
|
|
|
for _, instance := range instances {
|
2024-02-14 09:45:39 -06:00
|
|
|
f.recordedOps = append(f.recordedOps, instance)
|
2024-01-22 06:07:11 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-27 10:33:32 -05:00
|
|
|
type FakeRuleReader struct{}
|
|
|
|
|
2023-03-28 03:34:35 -05:00
|
|
|
func (f *FakeRuleReader) ListAlertRules(_ context.Context, q *models.ListAlertRulesQuery) (models.RulesGroup, error) {
|
|
|
|
return nil, nil
|
2022-09-27 10:33:32 -05:00
|
|
|
}
|
2022-10-05 15:32:20 -05:00
|
|
|
|
2023-01-26 11:29:10 -06:00
|
|
|
type FakeHistorian struct {
|
|
|
|
StateTransitions []StateTransition
|
|
|
|
}
|
2022-10-05 15:32:20 -05:00
|
|
|
|
2023-03-17 12:41:18 -05:00
|
|
|
func (f *FakeHistorian) Record(ctx context.Context, rule history_model.RuleMeta, states []StateTransition) <-chan error {
|
2023-01-26 11:29:10 -06:00
|
|
|
f.StateTransitions = append(f.StateTransitions, states...)
|
2023-01-24 09:41:38 -06:00
|
|
|
errCh := make(chan error)
|
|
|
|
close(errCh)
|
|
|
|
return errCh
|
2022-10-05 15:32:20 -05:00
|
|
|
}
|
2022-11-09 15:06:49 -06:00
|
|
|
|
|
|
|
// NotAvailableImageService is a service that returns ErrScreenshotsUnavailable.
|
|
|
|
type NotAvailableImageService struct{}
|
|
|
|
|
|
|
|
func (s *NotAvailableImageService) NewImage(_ context.Context, _ *models.AlertRule) (*models.Image, error) {
|
|
|
|
return nil, screenshot.ErrScreenshotsUnavailable
|
|
|
|
}
|
|
|
|
|
|
|
|
// NoopImageService is a no-op image service.
|
|
|
|
type NoopImageService struct{}
|
|
|
|
|
|
|
|
func (s *NoopImageService) NewImage(_ context.Context, _ *models.AlertRule) (*models.Image, error) {
|
|
|
|
return &models.Image{}, nil
|
|
|
|
}
|