From fa92dfb7b23758bdafeec0f16df9f3f7bd003351 Mon Sep 17 00:00:00 2001 From: bergquist Date: Wed, 16 Nov 2016 09:54:45 +0100 Subject: [PATCH] feat(mqe): add support for apps in where clause --- pkg/tsdb/mqe/types.go | 39 ++++++++++++++++++++++---------------- pkg/tsdb/mqe/types_test.go | 14 +++++++------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/pkg/tsdb/mqe/types.go b/pkg/tsdb/mqe/types.go index bd387cc3058..de5ff2fc8e9 100644 --- a/pkg/tsdb/mqe/types.go +++ b/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 - } - - for _, a := range availableSeries { - if match { - metrics = append(metrics, a) - } + if !containsWildcardPattern.Match([]byte(v.Metric)) { + metrics = append(metrics, v.Metric) + continue } + + /* + 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 diff --git a/pkg/tsdb/mqe/types_test.go b/pkg/tsdb/mqe/types_test.go index e78cb6e54c9..c136599cb93 100644 --- a/pkg/tsdb/mqe/types_test.go +++ b/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() {