Merge pull request 'support insight metric sort' (#132) from metric_sort into master
This commit is contained in:
commit
464a2f9bea
|
@ -6,6 +6,7 @@ package insight
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"infini.sh/framework/core/util"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
|
@ -16,6 +17,7 @@ type Metric struct {
|
|||
BucketSize string `json:"bucket_size,omitempty"`
|
||||
Filter interface{} `json:"filter,omitempty"`
|
||||
Groups []MetricGroupItem `json:"groups,omitempty"` //bucket group
|
||||
Sort []GroupSort `json:"sort,omitempty"`
|
||||
ClusterId string `json:"cluster_id,omitempty"`
|
||||
Formula string `json:"formula,omitempty"`
|
||||
Items []MetricItem `json:"items"`
|
||||
|
@ -25,6 +27,11 @@ type Metric struct {
|
|||
BucketLabel *BucketLabel `json:"bucket_label,omitempty"`
|
||||
}
|
||||
|
||||
type GroupSort struct {
|
||||
Key string `json:"key"`
|
||||
Direction string `json:"direction"`
|
||||
}
|
||||
|
||||
type MetricGroupItem struct {
|
||||
Field string `json:"field"`
|
||||
Limit int `json:"limit"`
|
||||
|
@ -60,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"`
|
||||
|
|
|
@ -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
|
||||
|
@ -125,11 +129,19 @@ func GenerateQuery(metric *insight.Metric) (interface{}, error) {
|
|||
if limit <= 0 {
|
||||
limit = 10
|
||||
}
|
||||
termsCfg := util.MapStr{
|
||||
"field": groups[i].Field,
|
||||
"size": limit,
|
||||
}
|
||||
if i == grpLength - 1 && len(metric.Sort) > 0 {
|
||||
var termsOrder []interface{}
|
||||
for _, sortItem := range metric.Sort {
|
||||
termsOrder = append(termsOrder, util.MapStr{sortItem.Key: sortItem.Direction})
|
||||
}
|
||||
termsCfg["order"] = termsOrder
|
||||
}
|
||||
groupAgg := util.MapStr{
|
||||
"terms": util.MapStr{
|
||||
"field": groups[i].Field,
|
||||
"size": limit,
|
||||
},
|
||||
"terms": termsCfg,
|
||||
}
|
||||
groupID := util.GetUUID()
|
||||
if lastGroupAgg != nil {
|
||||
|
|
Loading…
Reference in New Issue