Przeglądaj źródła

tests(mqe): add query builder

bergquist 9 lat temu
rodzic
commit
4739608ffb

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

@@ -30,5 +30,15 @@ func init() {
 
 func (e *MQEExecutor) Execute(ctx context.Context, queries tsdb.QuerySlice, context *tsdb.QueryContext) *tsdb.BatchResult {
 
+	availableSeries, _ := NewTokenClient().GetTokenData(ctx, e.DataSourceInfo)
+	glog.Info("available series", availableSeries)
+
+  //query, _ := &MQEQueryParser{}.Parse()
+
+
+
+  //fetch all available serienames
+	//expaned parsed model into multiple queries
+
 	return &tsdb.BatchResult{}
 }

+ 1 - 1
pkg/tsdb/mqe/token_client_test.go

@@ -21,7 +21,7 @@ func TestTokenClient(t *testing.T) {
 		body, err := client.RequestTokenData(context.TODO(), dsInfo)
 
 		So(err, ShouldBeNil)
-		So(len(body.Functions), ShouldBeGreaterThan, 1)
+		//So(len(body.Functions), ShouldBeGreaterThan, 1)
 		So(len(body.Metrics), ShouldBeGreaterThan, 1)
 	})
 }

+ 73 - 11
pkg/tsdb/mqe/types.go

@@ -1,6 +1,10 @@
 package mqe
 
 import (
+	"fmt"
+
+	"strings"
+
 	"github.com/grafana/grafana/pkg/tsdb"
 )
 
@@ -10,27 +14,85 @@ type MQEMetric struct {
 }
 
 type MQEQuery struct {
-	Metrics []MQEMetric
-	Hosts   []string
-	Apps    []string
-
+	Metrics        []MQEMetric
+	Hosts          []string
+	Apps           []string
 	AddAppToAlias  bool
 	AddHostToAlias bool
-	UseRawQuery    bool
-	RawQuery       string
+
+	TimeRange   *tsdb.TimeRange
+	UseRawQuery bool
+	RawQuery    string
 }
 
-func (q *MQEQuery) Build(queryContext *tsdb.QueryContext) string {
-	return ""
+//`os.disk.sda.io_time` where host in ('staples-lab-1') from 1479197578194 to 1479219178194
+func (q *MQEQuery) Build(availableSeries []string) ([]string, error) {
+	var queries []string
+	where := q.buildWhereClause()
+
+	var metrics []
+
+	for _, v := range q.Metrics {
+    if noStar {
+				metrics = append(metrics, v)
+
+      continue
+    }
+
+		for _, a := range availableSeries {
+			if match {
+				metrics = append(metrics, a)
+			}
+		}
+	}
+
+	for _, v := range metrics {
+		queries = append(queries,
+			fmt.Sprintf(
+				"`%s` %s from %v to %v",
+				v.Metric,
+				where,
+				q.TimeRange.GetFromAsMsEpoch(),
+				q.TimeRange.GetToAsMsEpoch()))
+	}
+
+	return queries, nil
+}
+
+func (q *MQEQuery) buildWhereClause() string {
+	hasApps := len(q.Apps) > 0
+	hasHosts := len(q.Hosts) > 0
+
+	where := ""
+	if hasHosts || hasApps {
+		where += "where "
+	}
+
+	if hasApps {
+		apps := strings.Join(q.Apps, "', '")
+		where += fmt.Sprintf(" apps in ('%s')", apps)
+	}
+
+	if hasHosts && hasApps {
+		where += " and"
+	}
+
+	if hasHosts {
+		hosts := strings.Join(q.Hosts, "', '")
+		where += fmt.Sprintf(" hosts in ('%s')", hosts)
+	}
+
+	return where
 }
 
 type TokenBody struct {
-	Functions []string
-	Metrics   []string
-	//tagset
+	Metrics []string
 }
 
 type TokenResponse struct {
 	Success bool
 	Body    TokenBody
 }
+
+type MQEResponse struct {
+}

+ 79 - 0
pkg/tsdb/mqe/types_test.go

@@ -0,0 +1,79 @@
+package mqe
+
+import (
+	"testing"
+
+	"time"
+
+	"fmt"
+
+	"github.com/grafana/grafana/pkg/tsdb"
+	. "github.com/smartystreets/goconvey/convey"
+)
+
+func TestWildcardExpansion(t *testing.T) {
+	availableMetrics := map[string]bool{
+		"os.cpu.all.idle": true,
+		"os.cpu.1.idle":   true,
+		"os.cpu.2.idle":   true,
+		"os.cpu.3.idle":   true,
+	}
+
+	now := time.Now()
+	from := now.Add((time.Minute*5)*-1).UnixNano() / int64(time.Millisecond)
+	to := now.UnixNano() / int64(time.Millisecond)
+
+	Convey("Can expanding query", t, func() {
+
+		Convey("Without wildcard series", func() {
+			query := &MQEQuery{
+				Metrics: []MQEMetric{
+					MQEMetric{
+						Metric: "os.cpu.3.idle",
+						Alias:  "cpu on core 3",
+					},
+					MQEMetric{
+						Metric: "os.cpu.2.idle",
+						Alias:  "cpu on core 2",
+					},
+				},
+				Hosts:          []string{"staples-lab-1", "staples-lab-2"},
+				Apps:           []string{"demoapp-1", "demoapp-2"},
+				AddAppToAlias:  false,
+				AddHostToAlias: false,
+				TimeRange:      &tsdb.TimeRange{Now: now, From: "5m", To: "now"},
+			}
+
+			expandeQueries, err := query.Build(availableMetrics)
+			So(err, ShouldBeNil)
+			So(len(expandeQueries), ShouldEqual, 2)
+			So(expandeQueries[0], ShouldEqual, fmt.Sprintf("`os.cpu.3.idle` where host in ('staples-lab-1', 'staples-lab-2') from %v to %v", from, to))
+			So(expandeQueries[1], ShouldEqual, fmt.Sprintf("`os.cpu.2.idle` where host in ('staples-lab-1', 'staples-lab-2') from %v to %v", from, to))
+		})
+
+		Convey("Containg wildcard series", func() {
+			query := &MQEQuery{
+				Metrics: []MQEMetric{
+					MQEMetric{
+						Metric: "os.cpu*",
+						Alias:  "cpu on core *",
+					},
+				},
+				Hosts:          []string{"staples-lab-1"},
+				AddAppToAlias:  false,
+				AddHostToAlias: false,
+				TimeRange:      &tsdb.TimeRange{Now: now, From: "5m", To: "now"},
+			}
+
+			expandeQueries, err := query.Build(availableMetrics)
+			So(err, ShouldBeNil)
+			So(len(expandeQueries), ShouldEqual, 4)
+
+			So(expandeQueries[0], ShouldEqual, fmt.Sprintf("`os.cpu.all.idle` where host in ('staples-lab-1') from %v to %v", from, to))
+			So(expandeQueries[1], ShouldEqual, fmt.Sprintf("`os.cpu.1.idle` where host in ('staples-lab-1') from %v to %v", from, to))
+			So(expandeQueries[2], ShouldEqual, fmt.Sprintf("`os.cpu.2.idle` where host in ('staples-lab-1') from %v to %v", from, to))
+			So(expandeQueries[3], ShouldEqual, fmt.Sprintf("`os.cpu.3.idle` where host in ('staples-lab-1') from %v to %v", from, to))
+
+		})
+	})
+}