validate metric sort

This commit is contained in:
liugq 2023-07-05 10:51:49 +08:00
parent e3ce446f4b
commit 518028fbdb
2 changed files with 30 additions and 0 deletions

View File

@ -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"`

View File

@ -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