stillbox/pkg/alerting/rules/alertrules.go

41 lines
714 B
Go
Raw Permalink Normal View History

package rules
import (
"time"
"dynatron.me/x/stillbox/internal/ruletime"
)
type AlertRules []AlertRule
2024-11-12 09:57:38 -05:00
func (ars AlertRules) Apply(t time.Time, coversOpts ...ruletime.CoversOption) float64 {
if ars == nil || len(ars) < 1 {
return 1.0
}
final := 1.0
2024-11-12 09:57:38 -05:00
for _, ar := range ars {
if ar.MatchTime(t, coversOpts...) {
final *= float64(ar.ScoreMultiplier)
}
}
return final
}
type AlertRule struct {
Times []ruletime.RuleTime `json:"times"`
ScoreMultiplier float32 `json:"mult"`
}
func (ar *AlertRule) MatchTime(t time.Time, coversOpts ...ruletime.CoversOption) bool {
for _, at := range ar.Times {
if at.Covers(t, coversOpts...) {
return true
}
}
return false
}