copy channel name and type to execution result

This commit is contained in:
liugq 2022-05-13 16:03:59 +08:00
parent 1b66f07c9a
commit d63147a2ab
5 changed files with 38 additions and 17 deletions

View File

@ -32,10 +32,12 @@ type Alert struct {
}
type ActionExecutionResult struct {
LastExecutionTime int `json:"last_execution_time"`
Error string `json:"error"`
ExecutionTime int `json:"execution_time"`
Error string `json:"error"`
Result string `json:"result"`
Message string `json:"message"`
ChannelName string `json:"channel_name"`
ChannelType string `json:"channel_type"`
}
const (

View File

@ -78,6 +78,7 @@ func (tr *TimeRange) Include( t time.Time) bool {
type FilterParam struct {
Start interface{} `json:"start"`
End interface{} `json:"end"`
BucketSize string `json:"bucket_size"`
}
//ctx
//rule expression, rule_id, resource_id, resource_name, event_id, condition_name, preset_value,[group_tags, check_values],

View File

@ -25,7 +25,7 @@ func (alert *AlertAPI) Init() {
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)
api.HandleAPIMethod(api.POST, "/alerting/rule/metric", alert.getMetricData)
api.HandleAPIMethod(api.GET, "/alerting/rule/:rule_id/metric", alert.getMetricData)
api.HandleAPIMethod(api.GET, "/alerting/channel/:channel_id", alert.getChannel)
api.HandleAPIMethod(api.POST, "/alerting/channel", alert.createChannel)

View File

@ -16,6 +16,7 @@ import (
"infini.sh/framework/core/orm"
"infini.sh/framework/core/task"
"infini.sh/framework/core/util"
"infini.sh/framework/modules/elastic/api"
"net/http"
"time"
)
@ -476,17 +477,29 @@ func (alertAPI *AlertAPI) getTemplateParams(w http.ResponseWriter, req *http.Req
}
func (alertAPI *AlertAPI) getMetricData(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
rule := alerting.Rule{}
err := alertAPI.DecodeJSON(req, &rule)
if err != nil {
log.Error(err)
rule := &alerting.Rule{
ID: ps.ByName("rule_id"),
}
exists, err := orm.Get(rule)
if !exists || err != nil {
alertAPI.WriteJSON(w, util.MapStr{
"error": err.Error(),
}, http.StatusInternalServerError)
"_id": rule.ID,
"found": false,
}, http.StatusNotFound)
return
}
var (
minStr = alertAPI.Get(req, "min", "")
maxStr = alertAPI.Get(req, "max", "")
)
bucketSize, min, max, err := api.GetMetricRangeAndBucketSize(minStr, maxStr, 60, 15)
filterParam := &alerting.FilterParam{
Start: min,
End: max,
BucketSize: fmt.Sprintf("%ds", bucketSize),
}
eng := alerting2.GetEngine(rule.Resource.Type)
metricData, err := eng.GetTargetMetricData(&rule, true, nil)
metricData, err := eng.GetTargetMetricData(rule, true, filterParam)
if err != nil {
log.Error(err)
alertAPI.WriteJSON(w, util.MapStr{
@ -502,7 +515,7 @@ func (alertAPI *AlertAPI) getMetricData(w http.ResponseWriter, req *http.Request
filteredMetricData = append(filteredMetricData, md)
}
alertAPI.WriteJSON(w, util.MapStr{
"metric": filteredMetricData,
"metrics": filteredMetricData,
}, http.StatusOK)
}

View File

@ -56,10 +56,14 @@ func (engine *Engine) GenerateQuery(rule *alerting.Rule, filterParam *alerting.F
if err != nil {
return nil, fmt.Errorf("get interval field error: %w", err)
}
var periodInterval = rule.Metrics.PeriodInterval
if filterParam != nil && filterParam.BucketSize != "" {
periodInterval = filterParam.BucketSize
}
timeAggs := util.MapStr{
"date_histogram": util.MapStr{
"field": rule.Resource.TimeField,
intervalField: rule.Metrics.PeriodInterval,
intervalField: periodInterval,
},
"aggs": basicAggs,
}
@ -263,7 +267,6 @@ func (engine *Engine) GenerateRawFilter(rule *alerting.Rule, filterParam *alerti
var err error
if rule.Resource.RawFilter != nil {
query = util.DeepCopy(rule.Resource.RawFilter).(map[string]interface{})
}else{
if !rule.Resource.Filter.IsEmpty(){
query, err = engine.ConvertFilterQueryToDsl(&rule.Resource.Filter)
@ -742,10 +745,12 @@ func performChannels(channels []alerting.Channel, ctx map[string]interface{}) ([
errStr = err.Error()
}
actionResults = append(actionResults, alerting.ActionExecutionResult{
Result: string(resBytes),
Error: errStr,
Message: string(messageBytes),
LastExecutionTime: int(time.Now().UnixNano()/1e6),
Result: string(resBytes),
Error: errStr,
Message: string(messageBytes),
ExecutionTime: int(time.Now().UnixNano()/1e6),
ChannelType: channel.Type,
ChannelName: channel.Name,
})
}
return actionResults, errCount