diff --git a/model/insight/metric_data.go b/model/insight/metric_data.go index 66af1812..fcc6e37b 100644 --- a/model/insight/metric_data.go +++ b/model/insight/metric_data.go @@ -6,6 +6,7 @@ package insight import ( "fmt" + "infini.sh/framework/core/util" "regexp" ) @@ -66,6 +67,31 @@ func (m *Metric) AutoTimeBeforeGroup() bool { } return true } +func (m *Metric) ValidateSortKey() error { + if len(m.Sort) == 0 { + return nil + } + if len(m.Items) == 0 { + return nil + } + var mm = map[string]*MetricItem{} + for _, item := range m.Items { + mm[item.Name] = &item + } + for _, sortItem := range m.Sort { + if !util.StringInArray([]string{"desc", "asc"}, sortItem.Direction){ + return fmt.Errorf("unknown sort direction [%s]", sortItem.Direction) + } + if v, ok := mm[sortItem.Key]; !ok && !util.StringInArray([]string{"_key", "_count"}, sortItem.Key){ + return fmt.Errorf("unknown sort key [%s]", sortItem.Key) + }else{ + if v.Statistic == "derivative" { + return fmt.Errorf("can not sort by pipeline agg [%s]", v.Statistic) + } + } + } + return nil +} type MetricItem struct { Name string `json:"name,omitempty"` diff --git a/plugin/api/insight/metric_util.go b/plugin/api/insight/metric_util.go index c1121288..e71c5fe8 100644 --- a/plugin/api/insight/metric_util.go +++ b/plugin/api/insight/metric_util.go @@ -115,6 +115,10 @@ func GenerateQuery(metric *insight.Metric) (interface{}, error) { var rootAggs util.MapStr groups := metric.Groups + err = metric.ValidateSortKey() + if err != nil { + return nil, err + } if grpLength := len(groups); grpLength > 0 { var lastGroupAgg util.MapStr