grafana/pkg/services/ngalert/schedule/loaded_metrics_reader.go
Alexander Weaver 6c5e94095d
Alerting: Scheduler and registry handle rules by an interface (#84044)
* export Evaluation

* Export Evaluation

* Export RuleVersionAndPauseStatus

* export Eval, create interface

* Export update and add to interface

* Export Stop and Run and add to interface

* Registry and scheduler use rule by interface and not concrete type

* Update factory to use interface, update tests to work over public API rather than writing to channels directly

* Rename map in registry

* Rename getOrCreateInfo to not reference a specific implementation

* Genericize alertRuleInfoRegistry into ruleRegistry

* Rename alertRuleInfo to alertRule

* Comments on interface

* Update pkg/services/ngalert/schedule/schedule.go

Co-authored-by: Jean-Philippe Quéméner <JohnnyQQQQ@users.noreply.github.com>

---------

Co-authored-by: Jean-Philippe Quéméner <JohnnyQQQQ@users.noreply.github.com>
2024-03-11 22:57:38 +02:00

45 lines
1.3 KiB
Go

package schedule
import (
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/ngalert/state"
)
var _ eval.AlertingResultsReader = AlertingResultsFromRuleState{}
func (a *alertRule) newLoadedMetricsReader(rule *ngmodels.AlertRule) eval.AlertingResultsReader {
return &AlertingResultsFromRuleState{
Manager: a.stateManager,
Rule: rule,
}
}
type RuleStateProvider interface {
GetStatesForRuleUID(orgID int64, alertRuleUID string) []*state.State
}
// AlertingResultsFromRuleState implements eval.AlertingResultsReader that gets the data from state manager.
// It returns results fingerprints only for Alerting and Pending states that have empty StateReason.
type AlertingResultsFromRuleState struct {
Manager RuleStateProvider
Rule *ngmodels.AlertRule
}
func (n AlertingResultsFromRuleState) Read() map[data.Fingerprint]struct{} {
states := n.Manager.GetStatesForRuleUID(n.Rule.OrgID, n.Rule.UID)
active := map[data.Fingerprint]struct{}{}
for _, st := range states {
if st.StateReason != "" {
continue
}
if st.State == eval.Alerting || st.State == eval.Pending {
active[st.ResultFingerprint] = struct{}{}
}
}
return active
}