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

feat(mqe): add support for apps in where clause

bergquist 9 лет назад
Родитель
Сommit
fa92dfb7b2
2 измененных файлов с 29 добавлено и 22 удалено
  1. 22 15
      pkg/tsdb/mqe/types.go
  2. 7 7
      pkg/tsdb/mqe/types_test.go

+ 22 - 15
pkg/tsdb/mqe/types.go

@@ -5,6 +5,8 @@ import (
 
 	"strings"
 
+	"regexp"
+
 	"github.com/grafana/grafana/pkg/tsdb"
 )
 
@@ -25,32 +27,37 @@ type MQEQuery struct {
 	RawQuery    string
 }
 
+var (
+	containsWildcardPattern *regexp.Regexp = regexp.MustCompile(`\*`)
+)
+
 //`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 []
+	var metrics []string
 
 	for _, v := range q.Metrics {
-    if noStar {
-				metrics = append(metrics, v)
-
-      continue
-    }
+		if !containsWildcardPattern.Match([]byte(v.Metric)) {
+			metrics = append(metrics, v.Metric)
+			continue
+		}
 
-		for _, a := range availableSeries {
-			if match {
-				metrics = append(metrics, a)
+		/*
+			for _, a := range availableSeries {
+				if match {
+					metrics = append(metrics, a)
+				}
 			}
-		}
+		*/
 	}
 
-	for _, v := range metrics {
+	for _, metric := range metrics {
 		queries = append(queries,
 			fmt.Sprintf(
 				"`%s` %s from %v to %v",
-				v.Metric,
+				metric,
 				where,
 				q.TimeRange.GetFromAsMsEpoch(),
 				q.TimeRange.GetToAsMsEpoch()))
@@ -70,16 +77,16 @@ func (q *MQEQuery) buildWhereClause() string {
 
 	if hasApps {
 		apps := strings.Join(q.Apps, "', '")
-		where += fmt.Sprintf(" apps in ('%s')", apps)
+		where += fmt.Sprintf("app in ('%s')", apps)
 	}
 
 	if hasHosts && hasApps {
-		where += " and"
+		where += " and "
 	}
 
 	if hasHosts {
 		hosts := strings.Join(q.Hosts, "', '")
-		where += fmt.Sprintf(" hosts in ('%s')", hosts)
+		where += fmt.Sprintf("host in ('%s')", hosts)
 	}
 
 	return where

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

@@ -12,11 +12,11 @@ import (
 )
 
 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,
+	availableMetrics := []string{
+		"os.cpu.all.idle",
+		"os.cpu.1.idle",
+		"os.cpu.2.idle",
+		"os.cpu.3.idle",
 	}
 
 	now := time.Now()
@@ -47,8 +47,8 @@ func TestWildcardExpansion(t *testing.T) {
 			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))
+			So(expandeQueries[0], ShouldEqual, fmt.Sprintf("`os.cpu.3.idle` where app in ('demoapp-1', 'demoapp-2') and 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 app 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() {