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 { type ActionExecutionResult struct {
LastExecutionTime int `json:"last_execution_time"` ExecutionTime int `json:"execution_time"`
Error string `json:"error"` Error string `json:"error"`
Result string `json:"result"` Result string `json:"result"`
Message string `json:"message"` Message string `json:"message"`
ChannelName string `json:"channel_name"`
ChannelType string `json:"channel_type"`
} }
const ( const (

View File

@ -78,6 +78,7 @@ func (tr *TimeRange) Include( t time.Time) bool {
type FilterParam struct { type FilterParam struct {
Start interface{} `json:"start"` Start interface{} `json:"start"`
End interface{} `json:"end"` End interface{} `json:"end"`
BucketSize string `json:"bucket_size"`
} }
//ctx //ctx
//rule expression, rule_id, resource_id, resource_name, event_id, condition_name, preset_value,[group_tags, check_values], //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.GET, "/alerting/stats", alert.getAlertStats)
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.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.GET, "/alerting/channel/:channel_id", alert.getChannel)
api.HandleAPIMethod(api.POST, "/alerting/channel", alert.createChannel) api.HandleAPIMethod(api.POST, "/alerting/channel", alert.createChannel)

View File

@ -16,6 +16,7 @@ import (
"infini.sh/framework/core/orm" "infini.sh/framework/core/orm"
"infini.sh/framework/core/task" "infini.sh/framework/core/task"
"infini.sh/framework/core/util" "infini.sh/framework/core/util"
"infini.sh/framework/modules/elastic/api"
"net/http" "net/http"
"time" "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) { func (alertAPI *AlertAPI) getMetricData(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
rule := alerting.Rule{} rule := &alerting.Rule{
err := alertAPI.DecodeJSON(req, &rule) ID: ps.ByName("rule_id"),
if err != nil { }
log.Error(err) exists, err := orm.Get(rule)
if !exists || err != nil {
alertAPI.WriteJSON(w, util.MapStr{ alertAPI.WriteJSON(w, util.MapStr{
"error": err.Error(), "_id": rule.ID,
}, http.StatusInternalServerError) "found": false,
}, http.StatusNotFound)
return 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) eng := alerting2.GetEngine(rule.Resource.Type)
metricData, err := eng.GetTargetMetricData(&rule, true, nil) metricData, err := eng.GetTargetMetricData(rule, true, filterParam)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
alertAPI.WriteJSON(w, util.MapStr{ alertAPI.WriteJSON(w, util.MapStr{
@ -502,7 +515,7 @@ func (alertAPI *AlertAPI) getMetricData(w http.ResponseWriter, req *http.Request
filteredMetricData = append(filteredMetricData, md) filteredMetricData = append(filteredMetricData, md)
} }
alertAPI.WriteJSON(w, util.MapStr{ alertAPI.WriteJSON(w, util.MapStr{
"metric": filteredMetricData, "metrics": filteredMetricData,
}, http.StatusOK) }, http.StatusOK)
} }

View File

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