Compare commits

...

4 Commits

Author SHA1 Message Date
Jayice
85eccb0603 fix cubic review 2026-06-26 03:32:51 +08:00
Jayice
29a3e728c3 fix cubic review 2026-06-25 16:55:42 +08:00
Jayice
901f1e9fb2 fix cubic review 2026-06-25 14:47:27 +08:00
Jayice
76f2752fd6 extend rule state to count state number 2026-06-25 14:27:38 +08:00
2 changed files with 21 additions and 15 deletions

View File

@@ -236,11 +236,11 @@ func NewAlertAPI(ar *AlertingRule, a *notifier.Alert) *ApiAlert {
return aa
}
func (r *ApiRule) ExtendState() {
if len(r.Alerts) > 0 {
func (r *ApiRule) ExtendState(normalizeAlertState bool) {
if !normalizeAlertState && r.Type == "alerting" {
return
}
if r.State == "" {
if r.State == "" || r.State == "inactive" || r.State == "pending" || r.State == "firing" {
r.State = "ok"
}
if r.Health != "ok" {

View File

@@ -122,6 +122,9 @@ func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool {
httpserver.Errorf(w, r, "%s", err)
return true
}
if r.URL.Path == "/vmalert/groups" {
rf.normalizeAlertState = true
}
// only support filtering by a single state
state := ""
if len(rf.states) > 0 {
@@ -371,16 +374,17 @@ func newAlertsFilter(r *http.Request) (*alertsFilter, *httpserver.ErrorWithStatu
// see https://prometheus.io/docs/prometheus/latest/querying/api/#rules
type rulesFilter struct {
gf *groupsFilter
ruleNames []string
ruleType string
excludeAlerts bool
states []string
maxGroups int
pageNum int
search string
match [][]metricsql.LabelFilter
extendedStates bool
gf *groupsFilter
ruleNames []string
ruleType string
excludeAlerts bool
states []string
maxGroups int
pageNum int
search string
match [][]metricsql.LabelFilter
extendedStates bool
normalizeAlertState bool
}
func newRulesFilter(r *http.Request) (*rulesFilter, *httpserver.ErrorWithStatusCode) {
@@ -543,8 +547,10 @@ func (rh *requestHandler) groups(rf *rulesFilter) *listGroupsResponse {
if !groupFound && !strings.Contains(strings.ToLower(rule.Name), rf.search) {
continue
}
ruleWithExtendedState := rule
ruleWithExtendedState.ExtendState(rf.normalizeAlertState)
if rf.extendedStates {
rule.ExtendState()
rule = ruleWithExtendedState
}
if !rf.matchesRule(&rule) {
continue
@@ -552,7 +558,7 @@ func (rh *requestHandler) groups(rf *rulesFilter) *listGroupsResponse {
if rf.excludeAlerts {
rule.Alerts = nil
}
g.States[rule.State]++
g.States[ruleWithExtendedState.State]++
filteredRules = append(filteredRules, rule)
}
if len(g.Rules) == 0 || len(filteredRules) > 0 {