From 49e6e27d3dde2143da3fcea793103d6035bacc4f Mon Sep 17 00:00:00 2001 From: liugq Date: Thu, 7 Jul 2022 21:25:45 +0800 Subject: [PATCH] update group data struct --- model/alerting/metric.go | 8 ++++++-- service/alerting/elasticsearch/engine.go | 14 +++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/model/alerting/metric.go b/model/alerting/metric.go index 80f082ab..40d17839 100644 --- a/model/alerting/metric.go +++ b/model/alerting/metric.go @@ -17,6 +17,12 @@ type Metric struct { Title string `json:"title"` //text template Message string `json:"message"` // text template FormatType string `json:"format_type,omitempty"` + Groups []MetricGroupItem `json:"groups"` //bucket group +} + +type MetricGroupItem struct { + Field string `json:"field"` + Limit int `json:"limit"` } func (m *Metric) GenerateExpression() (string, error){ if len(m.Items) == 1 { @@ -45,8 +51,6 @@ type MetricItem struct { Name string `json:"name"` Field string `json:"field"` Statistic string `json:"statistic"` - Group []string `json:"group"` //bucket group - Limit int `json:"limit"` } type QueryResult struct { diff --git a/service/alerting/elasticsearch/engine.go b/service/alerting/elasticsearch/engine.go index 9c5ead50..8d7bd17c 100644 --- a/service/alerting/elasticsearch/engine.go +++ b/service/alerting/elasticsearch/engine.go @@ -76,19 +76,19 @@ func (engine *Engine) GenerateQuery(rule *alerting.Rule, filterParam *alerting.F } var rootAggs util.MapStr - groups := rule.Metrics.Items[0].Group - limit := rule.Metrics.Items[0].Limit - //top group 10 - if limit <= 0 { - limit = 10 - } + groups := rule.Metrics.Groups if grpLength := len(groups); grpLength > 0 { var lastGroupAgg util.MapStr for i := grpLength-1; i>=0; i-- { + limit := groups[i].Limit + //top group 10 + if limit <= 0 { + limit = 10 + } groupAgg := util.MapStr{ "terms": util.MapStr{ - "field": groups[i], + "field": groups[i].Field, "size": limit, }, }