Просмотр исходного кода

feat(mqe): add basic support for functions list

bergquist 9 лет назад
Родитель
Сommit
dde6e73fed

+ 15 - 0
pkg/tsdb/mqe/model_parser.go

@@ -39,5 +39,20 @@ func (qp *QueryParser) Parse(model *simplejson.Json, dsInfo *models.DataSource,
 
 	query.Metrics = metrics
 
+	var functions []Function
+	for _, functionListObj := range model.Get("functionList").MustArray() {
+		functionListJson := simplejson.NewFromAny(functionListObj)
+		var f Function
+
+		f.Func = functionListJson.Get("func").MustString("")
+		if err != nil {
+			return nil, err
+		}
+
+		functions = append(functions, f)
+	}
+
+	query.FunctionList = functions
+
 	return query, nil
 }

+ 10 - 0
pkg/tsdb/mqe/model_parser_test.go

@@ -61,6 +61,14 @@ func TestMQEQueryParser(t *testing.T) {
             "metric": "os.disk.sda.io_time"
           }
         ],
+        "functionList": [
+          {
+            "func": "aggregate.min"
+          },
+          {
+             "func": "aggregate.max"
+          }
+        ],
         "rawQuery": "",
         "refId": "A",
         "addClusterToAlias": true,
@@ -76,6 +84,8 @@ func TestMQEQueryParser(t *testing.T) {
 			So(query.Cluster[0], ShouldEqual, "demoapp")
 			So(query.Metrics[0].Metric, ShouldEqual, "os.cpu.all.active_percentage")
 			So(query.Metrics[1].Metric, ShouldEqual, "os.disk.sda.io_time")
+			So(query.FunctionList[0].Func, ShouldEqual, "aggregate.min")
+			So(query.FunctionList[1].Func, ShouldEqual, "aggregate.max")
 		})
 
 		Convey("can parse raw query", func() {

+ 20 - 2
pkg/tsdb/mqe/types.go

@@ -16,10 +16,15 @@ type Metric struct {
 	Alias  string
 }
 
+type Function struct {
+	Func string
+}
+
 type Query struct {
 	Metrics           []Metric
 	Hosts             []string
 	Cluster           []string
+	FunctionList      []Function
 	AddClusterToAlias bool
 	AddHostToAlias    bool
 
@@ -35,6 +40,7 @@ var (
 func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
 	var queriesToSend []QueryToSend
 	where := q.buildWhereClause()
+	functions := q.buildFunctionList()
 
 	for _, v := range q.Metrics {
 		if !containsWildcardPattern.Match([]byte(v.Metric)) {
@@ -42,9 +48,11 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
 			if v.Alias != "" {
 				alias = fmt.Sprintf(" {%s}", v.Alias)
 			}
+
 			rawQuery := fmt.Sprintf(
-				"`%s`%s %s from %v to %v",
+				"`%s`%s%s %s from %v to %v",
 				v.Metric,
+				functions,
 				alias,
 				where,
 				q.TimeRange.GetFromAsMsEpoch(),
@@ -73,8 +81,9 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
 				}
 
 				rawQuery := fmt.Sprintf(
-					"`%s`%s %s from %v to %v",
+					"`%s`%s%s %s from %v to %v",
 					a,
+					functions,
 					alias,
 					where,
 					q.TimeRange.GetFromAsMsEpoch(),
@@ -90,6 +99,15 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
 	return queriesToSend, nil
 }
 
+func (q *Query) buildFunctionList() string {
+	functions := ""
+	for _, v := range q.FunctionList {
+		functions = fmt.Sprintf("%s|%s", functions, v.Func)
+	}
+
+	return functions
+}
+
 func (q *Query) buildWhereClause() string {
 	hasApps := len(q.Cluster) > 0
 	hasHosts := len(q.Hosts) > 0

+ 7 - 4
pkg/tsdb/mqe/types_test.go

@@ -35,15 +35,18 @@ func TestWildcardExpansion(t *testing.T) {
 				Cluster:           []string{"demoapp-1", "demoapp-2"},
 				AddClusterToAlias: false,
 				AddHostToAlias:    false,
-				TimeRange:         &tsdb.TimeRange{Now: now, From: "5m", To: "now"},
+				FunctionList: []Function{
+					Function{Func: "aggregate.min"},
+				},
+				TimeRange: &tsdb.TimeRange{Now: now, From: "5m", To: "now"},
 			}
 
 			expandeQueries, err := query.Build(availableMetrics)
 			So(err, ShouldBeNil)
 			So(len(expandeQueries), ShouldEqual, 3)
-			So(expandeQueries[0].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.3.idle` where cluster in ('demoapp-1', 'demoapp-2') and host in ('staples-lab-1', 'staples-lab-2') from %v to %v", from, to))
-			So(expandeQueries[1].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.2.idle` where cluster in ('demoapp-1', 'demoapp-2') and host in ('staples-lab-1', 'staples-lab-2') from %v to %v", from, to))
-			So(expandeQueries[2].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.1.idle` {cpu} where cluster in ('demoapp-1', 'demoapp-2') and host in ('staples-lab-1', 'staples-lab-2') from %v to %v", from, to))
+			So(expandeQueries[0].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.3.idle`|aggregate.min where cluster in ('demoapp-1', 'demoapp-2') and host in ('staples-lab-1', 'staples-lab-2') from %v to %v", from, to))
+			So(expandeQueries[1].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.2.idle`|aggregate.min where cluster in ('demoapp-1', 'demoapp-2') and host in ('staples-lab-1', 'staples-lab-2') from %v to %v", from, to))
+			So(expandeQueries[2].RawQuery, ShouldEqual, fmt.Sprintf("`os.cpu.1.idle`|aggregate.min {cpu} where cluster in ('demoapp-1', 'demoapp-2') and host in ('staples-lab-1', 'staples-lab-2') from %v to %v", from, to))
 		})
 
 		Convey("Containg wildcard series", func() {