From 6bbce21258239709d3698d25a4f398a02e296af9 Mon Sep 17 00:00:00 2001 From: liugq Date: Mon, 28 Aug 2023 16:20:42 +0800 Subject: [PATCH] add batch enable channel api --- plugin/api/alerting/api.go | 3 ++ plugin/api/alerting/channel.go | 61 ++++++++++++++++++++++++++++++++++ plugin/api/alerting/rule.go | 56 ++++++++++++++++++++++++++++++- 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/plugin/api/alerting/api.go b/plugin/api/alerting/api.go index f09660d3..87d80eda 100644 --- a/plugin/api/alerting/api.go +++ b/plugin/api/alerting/api.go @@ -30,6 +30,7 @@ func (alert *AlertAPI) Init() { api.HandleAPIMethod(api.GET, "/alerting/rule/:rule_id/info", alert.RequirePermission(alert.getRuleDetail, enum.PermissionAlertRuleRead, enum.PermissionAlertMessageRead)) api.HandleAPIMethod(api.POST, "/alerting/rule/_enable", alert.RequirePermission(alert.batchEnableRule, enum.PermissionAlertRuleWrite)) api.HandleAPIMethod(api.POST, "/alerting/rule/_disable", alert.RequirePermission(alert.batchDisableRule, enum.PermissionAlertRuleWrite)) + api.HandleAPIMethod(api.GET, "/alerting/rule/_search_values", alert.RequirePermission(alert.searchFieldValues, enum.PermissionAlertRuleRead)) api.HandleAPIMethod(api.GET, "/alerting/channel/:channel_id", alert.RequirePermission(alert.getChannel, enum.PermissionAlertChannelRead)) api.HandleAPIMethod(api.POST, "/alerting/channel", alert.RequirePermission(alert.createChannel, enum.PermissionAlertChannelWrite)) @@ -37,6 +38,8 @@ func (alert *AlertAPI) Init() { api.HandleAPIMethod(api.PUT, "/alerting/channel/:channel_id", alert.RequirePermission(alert.updateChannel, enum.PermissionAlertChannelWrite)) api.HandleAPIMethod(api.GET, "/alerting/channel/_search", alert.RequirePermission(alert.searchChannel, enum.PermissionAlertChannelRead)) api.HandleAPIMethod(api.POST, "/alerting/channel/test", alert.RequirePermission(alert.testChannel, enum.PermissionAlertChannelWrite)) + api.HandleAPIMethod(api.POST, "/alerting/channel/_enable", alert.RequirePermission(alert.batchEnableChannel, enum.PermissionAlertChannelWrite)) + api.HandleAPIMethod(api.POST, "/alerting/channel/_disable", alert.RequirePermission(alert.batchDisableChannel, enum.PermissionAlertChannelWrite)) api.HandleAPIMethod(api.GET, "/alerting/alert/_search", alert.RequirePermission(alert.searchAlert, enum.PermissionAlertHistoryRead)) api.HandleAPIMethod(api.GET, "/alerting/alert/:alert_id", alert.RequirePermission(alert.getAlert, enum.PermissionAlertHistoryRead)) diff --git a/plugin/api/alerting/channel.go b/plugin/api/alerting/channel.go index 6999098b..fb1de867 100644 --- a/plugin/api/alerting/channel.go +++ b/plugin/api/alerting/channel.go @@ -315,4 +315,65 @@ func (h *AlertAPI) testChannel(w http.ResponseWriter, req *http.Request, ps http return } h.WriteAckOKJSON(w) +} + +func (alertAPI *AlertAPI) batchEnableChannel(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + var channelIDs = []string{} + err := alertAPI.DecodeJSON(req, &channelIDs) + if err != nil { + log.Error(err) + alertAPI.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + if len(channelIDs) == 0 { + alertAPI.WriteJSON(w, util.MapStr{}, http.StatusOK) + return + } + if len(channelIDs) > 0 { + err = setChannelEnabled(true, channelIDs) + if err != nil { + log.Error(err) + alertAPI.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + } + alertAPI.WriteAckOKJSON(w) +} + +func (alertAPI *AlertAPI) batchDisableChannel(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + var channelIDs = []string{} + err := alertAPI.DecodeJSON(req, &channelIDs) + if err != nil { + log.Error(err) + alertAPI.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + if len(channelIDs) == 0 { + alertAPI.WriteJSON(w, util.MapStr{}, http.StatusOK) + return + } + if len(channelIDs) > 0 { + err = setChannelEnabled(false, channelIDs) + if err != nil { + log.Error(err) + alertAPI.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + } + alertAPI.WriteAckOKJSON(w) +} + +func setChannelEnabled(enabled bool, channelIDs []string) error { + q := util.MapStr{ + "query": util.MapStr{ + "terms": util.MapStr{ + "id": channelIDs, + }, + }, + "script": util.MapStr{ + "source": fmt.Sprintf("ctx._source['enabled'] = %v", enabled), + }, + } + err := orm.UpdateBy(alerting.Channel{}, util.MustToJSONBytes(q)) + return err } \ No newline at end of file diff --git a/plugin/api/alerting/rule.go b/plugin/api/alerting/rule.go index 0382f2c0..ba206702 100644 --- a/plugin/api/alerting/rule.go +++ b/plugin/api/alerting/rule.go @@ -1177,7 +1177,6 @@ func (alertAPI *AlertAPI) batchDisableRule(w http.ResponseWriter, req *http.Requ alertAPI.WriteError(w, err.Error(), http.StatusInternalServerError) return } - log.Info(rules) var newIDs []string for _, rule := range rules { if rule.Enabled { @@ -1207,6 +1206,61 @@ func (alertAPI *AlertAPI) batchDisableRule(w http.ResponseWriter, req *http.Requ alertAPI.WriteAckOKJSON(w) } +func (alertAPI *AlertAPI) searchFieldValues(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + var keyword = alertAPI.GetParameterOrDefault(req, "keyword", "") + var field = alertAPI.GetParameterOrDefault(req, "field", "category") + items , err := searchListItems(field, keyword, 20) + if err != nil { + log.Error(err) + alertAPI.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + alertAPI.WriteJSON(w, items, http.StatusOK) +} + +func searchListItems(field, keyword string, size int) ([]string, error){ + query := util.MapStr{ + "size": 0, + "aggs": util.MapStr{ + "items": util.MapStr{ + "terms": util.MapStr{ + "field": field, + "size": size, + }, + }, + }, + } + if v := strings.TrimSpace(keyword); v != ""{ + query["query"]= util.MapStr{ + "query_string": util.MapStr{ + "default_field": field, + "query": fmt.Sprintf("*%s*", v), + }, + } + } + q := orm.Query{ + RawQuery: util.MustToJSONBytes(query), + } + err, result := orm.Search(alerting.Rule{}, &q) + if err != nil { + return nil, err + } + searchRes := elastic.SearchResponse{} + err = util.FromJSONBytes(result.Raw, &searchRes) + if err != nil { + return nil, err + } + items := []string{} + for _, bk := range searchRes.Aggregations["items"].Buckets { + if v, ok := bk["key"].(string); ok { + if strings.Contains(v, keyword){ + items = append(items, v) + } + } + } + return items, nil +} + func getRulesByID(ruleIDs []string) ([]alerting.Rule, error){ if len(ruleIDs) == 0 { return nil, nil