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

This commit is contained in:
bergquist
2016-11-16 09:54:45 +01:00
parent ab7263579f
commit fa92dfb7b2
2 changed files with 30 additions and 23 deletions
+23 -16
View File
@@ -5,6 +5,8 @@ import (
"strings" "strings"
"regexp"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
) )
@@ -25,32 +27,37 @@ type MQEQuery struct {
RawQuery string RawQuery string
} }
var (
containsWildcardPattern *regexp.Regexp = regexp.MustCompile(`\*`)
)
//`os.disk.sda.io_time` where host in ('staples-lab-1') from 1479197578194 to 1479219178194 //`os.disk.sda.io_time` where host in ('staples-lab-1') from 1479197578194 to 1479219178194
func (q *MQEQuery) Build(availableSeries []string) ([]string, error) { func (q *MQEQuery) Build(availableSeries []string) ([]string, error) {
var queries []string var queries []string
where := q.buildWhereClause() where := q.buildWhereClause()
var metrics [] var metrics []string
for _, v := range q.Metrics { for _, v := range q.Metrics {
if noStar { if !containsWildcardPattern.Match([]byte(v.Metric)) {
metrics = append(metrics, v) metrics = append(metrics, v.Metric)
continue
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, queries = append(queries,
fmt.Sprintf( fmt.Sprintf(
"`%s` %s from %v to %v", "`%s` %s from %v to %v",
v.Metric, metric,
where, where,
q.TimeRange.GetFromAsMsEpoch(), q.TimeRange.GetFromAsMsEpoch(),
q.TimeRange.GetToAsMsEpoch())) q.TimeRange.GetToAsMsEpoch()))
@@ -70,16 +77,16 @@ func (q *MQEQuery) buildWhereClause() string {
if hasApps { if hasApps {
apps := strings.Join(q.Apps, "', '") apps := strings.Join(q.Apps, "', '")
where += fmt.Sprintf(" apps in ('%s')", apps) where += fmt.Sprintf("app in ('%s')", apps)
} }
if hasHosts && hasApps { if hasHosts && hasApps {
where += " and" where += " and "
} }
if hasHosts { if hasHosts {
hosts := strings.Join(q.Hosts, "', '") hosts := strings.Join(q.Hosts, "', '")
where += fmt.Sprintf(" hosts in ('%s')", hosts) where += fmt.Sprintf("host in ('%s')", hosts)
} }
return where return where
+7 -7
View File
@@ -12,11 +12,11 @@ import (
) )
func TestWildcardExpansion(t *testing.T) { func TestWildcardExpansion(t *testing.T) {
availableMetrics := map[string]bool{ availableMetrics := []string{
"os.cpu.all.idle": true, "os.cpu.all.idle",
"os.cpu.1.idle": true, "os.cpu.1.idle",
"os.cpu.2.idle": true, "os.cpu.2.idle",
"os.cpu.3.idle": true, "os.cpu.3.idle",
} }
now := time.Now() now := time.Now()
@@ -47,8 +47,8 @@ func TestWildcardExpansion(t *testing.T) {
expandeQueries, err := query.Build(availableMetrics) expandeQueries, err := query.Build(availableMetrics)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(len(expandeQueries), ShouldEqual, 2) 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[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 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() { Convey("Containg wildcard series", func() {