add batch enable channel api
This commit is contained in:
parent
f767ffa39a
commit
6bbce21258
|
@ -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))
|
||||
|
|
|
@ -316,3 +316,64 @@ func (h *AlertAPI) testChannel(w http.ResponseWriter, req *http.Request, ps http
|
|||
}
|
||||
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
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue