update metric data struct
This commit is contained in:
parent
49e6e27d3d
commit
7864a482b7
|
@ -6,24 +6,18 @@ package alerting
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"infini.sh/framework/core/insight"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type Metric struct {
|
||||
PeriodInterval string `json:"period_interval"`
|
||||
Items []MetricItem `json:"items"`
|
||||
Formula string `json:"formula,omitempty"`
|
||||
insight.Metric
|
||||
Expression string `json:"expression" elastic_mapping:"expression:{type:keyword,copy_to:search_text}"` //告警表达式,自动生成 eg: avg(cpu) > 80
|
||||
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 {
|
||||
return fmt.Sprintf("%s(%s)", m.Items[0].Statistic, m.Items[0].Field), nil
|
||||
|
|
|
@ -185,7 +185,7 @@ func (alertAPI *AlertAPI) getRuleDetail(w http.ResponseWriter, req *http.Request
|
|||
"rule_name": obj.Name,
|
||||
"resource_name": obj.Resource.Name,
|
||||
"resource_objects": obj.Resource.Objects,
|
||||
"period_interval": obj.Metrics.PeriodInterval, //统计周期
|
||||
"bucket_size": obj.Metrics.BucketSize, //统计周期
|
||||
"updated": obj.Updated,
|
||||
"conditions": obj.Conditions,
|
||||
"message_count": alertNumbers[obj.ID], //所有关联告警消息数(包括已恢复的)
|
||||
|
@ -677,8 +677,8 @@ func (alertAPI *AlertAPI) getPreviewMetricData(w http.ResponseWriter, req *http.
|
|||
maxStr = alertAPI.Get(req, "max", "")
|
||||
)
|
||||
var bkSize float64 = 60
|
||||
if rule.Metrics.PeriodInterval != "" {
|
||||
duration, err := time.ParseDuration(rule.Metrics.PeriodInterval)
|
||||
if rule.Metrics.BucketSize != "" {
|
||||
duration, err := time.ParseDuration(rule.Metrics.BucketSize)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
alertAPI.WriteJSON(w, util.MapStr{
|
||||
|
|
|
@ -15,6 +15,7 @@ import (
|
|||
"infini.sh/console/service/alerting/action"
|
||||
"infini.sh/console/service/alerting/funcs"
|
||||
"infini.sh/framework/core/elastic"
|
||||
"infini.sh/framework/core/insight"
|
||||
"infini.sh/framework/core/kv"
|
||||
"infini.sh/framework/core/orm"
|
||||
"infini.sh/framework/core/util"
|
||||
|
@ -61,7 +62,7 @@ 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
|
||||
var periodInterval = rule.Metrics.BucketSize
|
||||
if filterParam != nil && filterParam.BucketSize != "" {
|
||||
periodInterval = filterParam.BucketSize
|
||||
}
|
||||
|
@ -124,7 +125,7 @@ func (engine *Engine) GenerateQuery(rule *alerting.Rule, filterParam *alerting.F
|
|||
}, nil
|
||||
}
|
||||
//generateAgg convert statistic of metric item to elasticsearch aggregation
|
||||
func (engine *Engine) generateAgg(metricItem *alerting.MetricItem) map[string]interface{}{
|
||||
func (engine *Engine) generateAgg(metricItem *insight.MetricItem) map[string]interface{}{
|
||||
var (
|
||||
aggType = "value_count"
|
||||
field = metricItem.Field
|
||||
|
@ -285,7 +286,7 @@ func (engine *Engine) generateTimeFilter(rule *alerting.Rule, filterParam *alert
|
|||
units string
|
||||
value int
|
||||
)
|
||||
intervalDuration, err := time.ParseDuration(rule.Metrics.PeriodInterval)
|
||||
intervalDuration, err := time.ParseDuration(rule.Metrics.BucketSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -299,7 +300,7 @@ func (engine *Engine) generateTimeFilter(rule *alerting.Rule, filterParam *alert
|
|||
units = "s"
|
||||
value = int(intervalDuration / time.Second)
|
||||
}else{
|
||||
return nil, fmt.Errorf("period interval: %s is too small", rule.Metrics.PeriodInterval)
|
||||
return nil, fmt.Errorf("period interval: %s is too small", rule.Metrics.BucketSize)
|
||||
}
|
||||
bucketCount := rule.Conditions.GetMinimumPeriodMatch() + 1
|
||||
if bucketCount <= 0 {
|
||||
|
|
Loading…
Reference in New Issue