and enable rule api

This commit is contained in:
liugq 2022-04-27 10:55:16 +08:00
parent 0b88aba906
commit 24855bcd54
2 changed files with 45 additions and 0 deletions

View File

@ -22,6 +22,7 @@ func (alert *AlertAPI) Init() {
api.HandleAPIMethod(api.PUT, "/alerting/rule/:rule_id", alert.updateRule) api.HandleAPIMethod(api.PUT, "/alerting/rule/:rule_id", alert.updateRule)
api.HandleAPIMethod(api.GET, "/alerting/rule/_search", alert.searchRule) api.HandleAPIMethod(api.GET, "/alerting/rule/_search", alert.searchRule)
api.HandleAPIMethod(api.POST, "/alerting/rule/info", alert.fetchAlertInfos) api.HandleAPIMethod(api.POST, "/alerting/rule/info", alert.fetchAlertInfos)
api.HandleAPIMethod(api.POST, "/alerting/rule/:rule_id/_enable", alert.enableRule)
api.HandleAPIMethod(api.GET, "/alerting/channel/:channel_id", alert.getChannel) api.HandleAPIMethod(api.GET, "/alerting/channel/:channel_id", alert.getChannel)
api.HandleAPIMethod(api.POST, "/alerting/channel", alert.createChannel) api.HandleAPIMethod(api.POST, "/alerting/channel", alert.createChannel)
@ -33,6 +34,7 @@ func (alert *AlertAPI) Init() {
api.HandleAPIMethod(api.GET, "/alerting/alert/:alert_id", alert.getAlert) api.HandleAPIMethod(api.GET, "/alerting/alert/:alert_id", alert.getAlert)
api.HandleAPIMethod(api.POST, "/alerting/alert/_acknowledge", alert.acknowledgeAlert) api.HandleAPIMethod(api.POST, "/alerting/alert/_acknowledge", alert.acknowledgeAlert)
//just for test //just for test
//api.HandleAPIMethod(api.GET, "/alerting/rule/test", alert.testRule) //api.HandleAPIMethod(api.GET, "/alerting/rule/test", alert.testRule)

View File

@ -355,6 +355,49 @@ func (alertAPI *AlertAPI) fetchAlertInfos(w http.ResponseWriter, req *http.Reque
alertAPI.WriteJSON(w, latestAlertInfos, http.StatusOK) alertAPI.WriteJSON(w, latestAlertInfos, http.StatusOK)
} }
func (alertAPI *AlertAPI) enableRule(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
reqObj := alerting.Rule{}
err := alertAPI.DecodeJSON(req, &reqObj)
if err != nil {
alertAPI.WriteError(w, fmt.Sprintf("request format error:%v", err), http.StatusInternalServerError)
return
}
id := ps.MustGetParameter("rule_id")
obj := alerting.Rule{}
obj.ID = id
exists, err := orm.Get(&obj)
if !exists || err != nil {
alertAPI.WriteJSON(w, util.MapStr{
"_id": id,
"result": "not_found",
}, http.StatusNotFound)
return
}
if reqObj.Enabled {
eng := alerting2.GetEngine(obj.Resource.Type)
ruleTask := task.ScheduleTask{
ID: obj.ID,
Interval: obj.Schedule.Interval,
Description: obj.Metrics.Expression,
Task: eng.GenerateTask(&obj),
}
task.RegisterScheduleTask(ruleTask)
task.StartTask(ruleTask.ID)
}else{
task.DeleteTask(id)
}
obj.Enabled = reqObj.Enabled
err = orm.Save(obj)
if err != nil {
alertAPI.WriteError(w, fmt.Sprintf("save rule error:%v", err), http.StatusInternalServerError)
return
}
alertAPI.WriteJSON(w, util.MapStr{
"result": "updated",
"_id": id,
}, http.StatusOK)
}
func checkResourceExists(rule *alerting.Rule) (bool, error) { func checkResourceExists(rule *alerting.Rule) (bool, error) {
if rule.Resource.ID == "" { if rule.Resource.ID == "" {
return false, fmt.Errorf("resource id can not be empty") return false, fmt.Errorf("resource id can not be empty")