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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"infini.sh/framework/core/util"
|
||||||
"regexp"
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -16,6 +17,7 @@ type Metric struct {
|
||||||
BucketSize string `json:"bucket_size,omitempty"`
|
BucketSize string `json:"bucket_size,omitempty"`
|
||||||
Filter interface{} `json:"filter,omitempty"`
|
Filter interface{} `json:"filter,omitempty"`
|
||||||
Groups []MetricGroupItem `json:"groups,omitempty"` //bucket group
|
Groups []MetricGroupItem `json:"groups,omitempty"` //bucket group
|
||||||
|
Sort []GroupSort `json:"sort,omitempty"`
|
||||||
ClusterId string `json:"cluster_id,omitempty"`
|
ClusterId string `json:"cluster_id,omitempty"`
|
||||||
Formula string `json:"formula,omitempty"`
|
Formula string `json:"formula,omitempty"`
|
||||||
Items []MetricItem `json:"items"`
|
Items []MetricItem `json:"items"`
|
||||||
|
@ -25,6 +27,11 @@ type Metric struct {
|
||||||
BucketLabel *BucketLabel `json:"bucket_label,omitempty"`
|
BucketLabel *BucketLabel `json:"bucket_label,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GroupSort struct {
|
||||||
|
Key string `json:"key"`
|
||||||
|
Direction string `json:"direction"`
|
||||||
|
}
|
||||||
|
|
||||||
type MetricGroupItem struct {
|
type MetricGroupItem struct {
|
||||||
Field string `json:"field"`
|
Field string `json:"field"`
|
||||||
Limit int `json:"limit"`
|
Limit int `json:"limit"`
|
||||||
|
@ -60,6 +67,31 @@ func (m *Metric) AutoTimeBeforeGroup() bool {
|
||||||
}
|
}
|
||||||
return true
|
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 {
|
type MetricItem struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name,omitempty"`
|
||||||
|
|
|
@ -115,6 +115,10 @@ func GenerateQuery(metric *insight.Metric) (interface{}, error) {
|
||||||
|
|
||||||
var rootAggs util.MapStr
|
var rootAggs util.MapStr
|
||||||
groups := metric.Groups
|
groups := metric.Groups
|
||||||
|
err = metric.ValidateSortKey()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if grpLength := len(groups); grpLength > 0 {
|
if grpLength := len(groups); grpLength > 0 {
|
||||||
var lastGroupAgg util.MapStr
|
var lastGroupAgg util.MapStr
|
||||||
|
@ -125,11 +129,19 @@ func GenerateQuery(metric *insight.Metric) (interface{}, error) {
|
||||||
if limit <= 0 {
|
if limit <= 0 {
|
||||||
limit = 10
|
limit = 10
|
||||||
}
|
}
|
||||||
groupAgg := util.MapStr{
|
termsCfg := util.MapStr{
|
||||||
"terms": util.MapStr{
|
|
||||||
"field": groups[i].Field,
|
"field": groups[i].Field,
|
||||||
"size": limit,
|
"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": termsCfg,
|
||||||
}
|
}
|
||||||
groupID := util.GetUUID()
|
groupID := util.GetUUID()
|
||||||
if lastGroupAgg != nil {
|
if lastGroupAgg != nil {
|
||||||
|
|
Loading…
Reference in New Issue