diff --git a/model/alerting/alert.go b/model/alerting/alert.go index 30ee0e26..3952467f 100644 --- a/model/alerting/alert.go +++ b/model/alerting/alert.go @@ -39,7 +39,7 @@ type ActionExecutionResult struct { const ( AlertStateActive string = "active" - AlertStateAcknowledge = "acknowledge" + AlertStateAcknowledge = "acknowledged" AlertStateNormal = "normal" AlertStateError = "error" ) diff --git a/model/alerting/condition.go b/model/alerting/condition.go index 657d2649..2d53dbd6 100644 --- a/model/alerting/condition.go +++ b/model/alerting/condition.go @@ -58,9 +58,7 @@ type ConditionResultItem struct { type Severity string var SeverityWeights = map[string]int{ - "verbose": 1, - "info": 2, - "warning": 3, - "error": 4, - "critical": 5, + "warning": 1, + "error": 2, + "critical": 3, } \ No newline at end of file diff --git a/plugin/api/alerting/alert.go b/plugin/api/alerting/alert.go index c8325e1a..3c3edb2c 100644 --- a/plugin/api/alerting/alert.go +++ b/plugin/api/alerting/alert.go @@ -8,6 +8,7 @@ import ( "fmt" "infini.sh/console/model/alerting" httprouter "infini.sh/framework/core/api/router" + "infini.sh/framework/core/elastic" "infini.sh/framework/core/orm" "infini.sh/framework/core/util" "net/http" @@ -88,10 +89,28 @@ func (h *AlertAPI) searchAlert(w http.ResponseWriter, req *http.Request, ps http queryDSL = `{"sort":[{"created":{ "order": "desc"}}],"query":{"bool":{"must":[%s]}}, "size": %d, "from": %d}` strSize = h.GetParameterOrDefault(req, "size", "20") strFrom = h.GetParameterOrDefault(req, "from", "0") + state = h.GetParameterOrDefault(req, "state", "") + severity = h.GetParameterOrDefault(req, "severity", "") mustBuilder = &strings.Builder{} ) + hasFilter := false if keyword != "" { mustBuilder.WriteString(fmt.Sprintf(`{"query_string":{"default_field":"*","query": "%s"}}`, keyword)) + hasFilter = true + } + if state != "" { + if hasFilter { + mustBuilder.WriteString(",") + } + mustBuilder.WriteString(fmt.Sprintf(`{"term":{"state":{"value":"%s"}}}`, state)) + hasFilter = true + } + if severity != "" { + if hasFilter { + mustBuilder.WriteString(",") + } + mustBuilder.WriteString(fmt.Sprintf(`{"term":{"severity":{"value":"%s"}}}`, severity)) + hasFilter = true } size, _ := strconv.Atoi(strSize) if size <= 0 { @@ -114,3 +133,59 @@ func (h *AlertAPI) searchAlert(w http.ResponseWriter, req *http.Request, ps http } h.Write(w, res.Raw) } + +func (h *AlertAPI) getAlertStats(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + esClient := elastic.GetClient(h.Config.Elasticsearch) + queryDsl := util.MapStr{ + "size": 0, + "query": util.MapStr{ + "bool": util.MapStr{ + "must_not": []util.MapStr{ + { + "terms": util.MapStr{ + "state": []string{ + "acknowledged", + "normal", + "", + }, + }, + }, + }, + }, + }, + "aggs": util.MapStr{ + "terms_by_state": util.MapStr{ + "terms": util.MapStr{ + "field": "severity", + "size": 5, + }, + }, + }, + } + + searchRes, err := esClient.SearchWithRawQueryDSL(orm.GetWildcardIndexName(alerting.Alert{}), util.MustToJSONBytes(queryDsl) ) + if err != nil { + h.WriteJSON(w, util.MapStr{ + "error": err.Error(), + }, http.StatusInternalServerError) + return + } + severityAlerts := map[string]interface{}{} + if termsAgg, ok := searchRes.Aggregations["terms_by_state"]; ok { + for _, bk := range termsAgg.Buckets { + if severity, ok := bk["key"].(string); ok { + severityAlerts[severity] = bk["doc_count"] + } + } + } + for severity, _ := range alerting.SeverityWeights { + if _, ok := severityAlerts[severity]; !ok { + severityAlerts[severity] = 0 + } + } + h.WriteJSON(w, util.MapStr{ + "alert": util.MapStr{ + "current": severityAlerts, + }, + }, http.StatusOK) +} \ No newline at end of file diff --git a/plugin/api/alerting/api.go b/plugin/api/alerting/api.go index 90cda661..ca27be12 100644 --- a/plugin/api/alerting/api.go +++ b/plugin/api/alerting/api.go @@ -21,6 +21,7 @@ func (alert *AlertAPI) Init() { api.HandleAPIMethod(api.DELETE, "/alerting/rule/:rule_id", alert.deleteRule) api.HandleAPIMethod(api.PUT, "/alerting/rule/:rule_id", alert.updateRule) api.HandleAPIMethod(api.GET, "/alerting/rule/_search", alert.searchRule) + api.HandleAPIMethod(api.GET, "/alerting/stats", alert.getAlertStats) api.HandleAPIMethod(api.POST, "/alerting/rule/info", alert.fetchAlertInfos) api.HandleAPIMethod(api.POST, "/alerting/rule/:rule_id/_enable", alert.enableRule)