From 07275a6b9bd5ebc30efb3b69b02ab45283f87fec Mon Sep 17 00:00:00 2001 From: wklken Date: Fri, 8 Mar 2019 17:39:48 +0800 Subject: [PATCH 1/3] add rule api /api/v1/alerts/{firing,pending} --- pkg/rule/api/v1.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/rule/api/v1.go b/pkg/rule/api/v1.go index be2dc5abf4..89d80c8ac4 100644 --- a/pkg/rule/api/v1.go +++ b/pkg/rule/api/v1.go @@ -50,6 +50,7 @@ func (api *API) Register(r *route.Router, tracer opentracing.Tracer, logger log. } r.Get("/alerts", instr("alerts", api.alerts)) + r.Get("/alerts/:state", instr("alerts", api.stateAlerts)) r.Get("/rules", instr("rules", api.rules)) } @@ -113,6 +114,7 @@ func (api *API) rules(r *http.Request) (interface{}, []error, *qapi.ApiError) { } func (api *API) alerts(r *http.Request) (interface{}, []error, *qapi.ApiError) { + alertingRules := api.rulesRetriever.AlertingRules() alerts := []*Alert{} @@ -128,6 +130,27 @@ func (api *API) alerts(r *http.Request) (interface{}, []error, *qapi.ApiError) { return res, nil, nil } +func (api *API) stateAlerts(r *http.Request) (interface{}, []error, *qapi.ApiError) { + ctx := r.Context() + state := route.Param(ctx, "state") + + alertingRules := api.rulesRetriever.AlertingRules() + alerts := []*Alert{} + + for _, alertingRule := range alertingRules { + stateAlerts := rulesAlertsToAPIAlerts(alertingRule.ActiveAlerts()) + for _, a := range stateAlerts { + if a.State == state { + alerts = append(alerts, a) + } + } + } + + res := &AlertDiscovery{Alerts: alerts} + + return res, nil, nil +} + type AlertDiscovery struct { Alerts []*Alert `json:"alerts"` } From d4c7148ac4814c1b2a7612dd1b6832c47bd11c7b Mon Sep 17 00:00:00 2001 From: wklken Date: Sat, 9 Mar 2019 10:19:27 +0800 Subject: [PATCH 2/3] try to add unittest to rule/api/v1_test.go --- pkg/rule/api/v1_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/rule/api/v1_test.go b/pkg/rule/api/v1_test.go index af4034bd8d..0a0194c95a 100644 --- a/pkg/rule/api/v1_test.go +++ b/pkg/rule/api/v1_test.go @@ -178,6 +178,15 @@ func testEndpoints(t *testing.T, api *API) { }, }, }, + { + endpoint: api.alerts, + response: &AlertDiscovery{Alerts: []*Alert{}}, + }, + { + endpoint: api.stateAlerts, + params: map[string]string{"state": "firing"}, + response: &AlertDiscovery{Alerts: []*Alert{}}, + }, } methods := func(f qapi.ApiFunc) []string { From d93e0092f427a7456df0da3611537f091e88ecda Mon Sep 17 00:00:00 2001 From: wklken Date: Mon, 6 May 2019 17:24:39 +0800 Subject: [PATCH 3/3] Update v1.go change to 0.4.0 new func call --- pkg/rule/api/v1.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/rule/api/v1.go b/pkg/rule/api/v1.go index 26e257ffae..e5889c02a5 100644 --- a/pkg/rule/api/v1.go +++ b/pkg/rule/api/v1.go @@ -51,7 +51,7 @@ func (api *API) Register(r *route.Router, tracer opentracing.Tracer, logger log. } r.Get("/alerts", instr("alerts", api.alerts)) - r.Get("/alerts/:state", instr("alerts", api.stateAlerts)) + r.Get("/alerts/:state", instr("alerts_state", api.stateAlerts)) r.Get("/rules", instr("rules", api.rules)) } @@ -133,9 +133,9 @@ func (api *API) stateAlerts(r *http.Request) (interface{}, []error, *qapi.ApiErr ctx := r.Context() state := route.Param(ctx, "state") - var alerts []*Alert + var alerts []*Alert for _, alertingRule := range api.ruleRetriever.AlertingRules() { - stateAlerts := rulesAlertsToAPIAlerts(alertingRule.ActiveAlerts()) + stateAlerts := rulesAlertsToAPIAlerts(alertingRule.PartialResponseStrategy, alertingRule.ActiveAlerts()) for _, a := range stateAlerts { if a.State == state { alerts = append(alerts, a)