grafana/pkg/services/ngalert/state/historian/annotation_store.go
Alexander Weaver 18b910e654
Alerting: Refactor annotation historian to isolate dashboard service dependency (#71689)
* Refactor annotation historian to isolate dashboard service dependency

* Export PanelKey

* Don't export parsePanelKey

* Remove commented out code
2023-07-18 08:18:55 -05:00

63 lines
2.1 KiB
Go

package historian
import (
"context"
"fmt"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/ngalert/metrics"
)
type AnnotationService interface {
Find(ctx context.Context, query *annotations.ItemQuery) ([]*annotations.ItemDTO, error)
SaveMany(ctx context.Context, items []annotations.Item) error
}
type AnnotationServiceStore struct {
svc AnnotationService
dashboards *dashboardResolver
metrics *metrics.Historian
}
func NewAnnotationStore(svc AnnotationService, dashboards dashboards.DashboardService, metrics *metrics.Historian) *AnnotationServiceStore {
return &AnnotationServiceStore{
svc: svc,
dashboards: newDashboardResolver(dashboards, defaultDashboardCacheExpiry),
metrics: metrics,
}
}
func (s *AnnotationServiceStore) Save(ctx context.Context, panel *PanelKey, annotations []annotations.Item, orgID int64, logger log.Logger) error {
if panel != nil {
dashID, err := s.dashboards.getID(ctx, panel.orgID, panel.dashUID)
if err != nil {
logger.Error("Error getting dashboard for alert annotation", "dashboardUID", panel.dashUID, "error", err)
dashID = 0
}
for i := range annotations {
annotations[i].DashboardID = dashID
annotations[i].PanelID = panel.panelID
}
}
org := fmt.Sprint(orgID)
s.metrics.WritesTotal.WithLabelValues(org, "annotations").Inc()
s.metrics.TransitionsTotal.WithLabelValues(org).Add(float64(len(annotations)))
if err := s.svc.SaveMany(ctx, annotations); err != nil {
logger.Error("Error saving alert annotation batch", "error", err)
s.metrics.WritesFailed.WithLabelValues(org, "annotations").Inc()
s.metrics.TransitionsFailed.WithLabelValues(org).Add(float64(len(annotations)))
return fmt.Errorf("error saving alert annotation batch: %w", err)
}
logger.Debug("Done saving alert annotation batch")
return nil
}
func (s *AnnotationServiceStore) Find(ctx context.Context, query *annotations.ItemQuery) ([]*annotations.ItemDTO, error) {
return s.svc.Find(ctx, query)
}