From cc25b07439aa4830f36ee650ada4ea408ffe572d Mon Sep 17 00:00:00 2001 From: liugq Date: Mon, 3 Jul 2023 16:09:22 +0800 Subject: [PATCH 1/2] insight support terms top by sorted sub metric --- model/insight/metric_data.go | 6 ++++++ plugin/api/insight/metric_util.go | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/model/insight/metric_data.go b/model/insight/metric_data.go index 7125eeb8..66af1812 100644 --- a/model/insight/metric_data.go +++ b/model/insight/metric_data.go @@ -16,6 +16,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 +26,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"` diff --git a/plugin/api/insight/metric_util.go b/plugin/api/insight/metric_util.go index b20ec49f..c1121288 100644 --- a/plugin/api/insight/metric_util.go +++ b/plugin/api/insight/metric_util.go @@ -125,11 +125,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 { From 518028fbdbecea658adf5beb81bf428da045b647 Mon Sep 17 00:00:00 2001 From: liugq Date: Wed, 5 Jul 2023 10:51:49 +0800 Subject: [PATCH 2/2] validate metric sort --- model/insight/metric_data.go | 26 ++++++++++++++++++++++++++ plugin/api/insight/metric_util.go | 4 ++++ 2 files changed, 30 insertions(+) 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