Commit dde6e73f by bergquist

feat(mqe): add basic support for functions list

parent 1e6210dd
...@@ -39,5 +39,20 @@ func (qp *QueryParser) Parse(model *simplejson.Json, dsInfo *models.DataSource, ...@@ -39,5 +39,20 @@ func (qp *QueryParser) Parse(model *simplejson.Json, dsInfo *models.DataSource,
query.Metrics = metrics 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 return query, nil
} }
...@@ -61,6 +61,14 @@ func TestMQEQueryParser(t *testing.T) { ...@@ -61,6 +61,14 @@ func TestMQEQueryParser(t *testing.T) {
"metric": "os.disk.sda.io_time" "metric": "os.disk.sda.io_time"
} }
], ],
"functionList": [
{
"func": "aggregate.min"
},
{
"func": "aggregate.max"
}
],
"rawQuery": "", "rawQuery": "",
"refId": "A", "refId": "A",
"addClusterToAlias": true, "addClusterToAlias": true,
...@@ -76,6 +84,8 @@ func TestMQEQueryParser(t *testing.T) { ...@@ -76,6 +84,8 @@ func TestMQEQueryParser(t *testing.T) {
So(query.Cluster[0], ShouldEqual, "demoapp") So(query.Cluster[0], ShouldEqual, "demoapp")
So(query.Metrics[0].Metric, ShouldEqual, "os.cpu.all.active_percentage") So(query.Metrics[0].Metric, ShouldEqual, "os.cpu.all.active_percentage")
So(query.Metrics[1].Metric, ShouldEqual, "os.disk.sda.io_time") 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() { Convey("can parse raw query", func() {
......
...@@ -16,10 +16,15 @@ type Metric struct { ...@@ -16,10 +16,15 @@ type Metric struct {
Alias string Alias string
} }
type Function struct {
Func string
}
type Query struct { type Query struct {
Metrics []Metric Metrics []Metric
Hosts []string Hosts []string
Cluster []string Cluster []string
FunctionList []Function
AddClusterToAlias bool AddClusterToAlias bool
AddHostToAlias bool AddHostToAlias bool
...@@ -35,6 +40,7 @@ var ( ...@@ -35,6 +40,7 @@ var (
func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) { func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
var queriesToSend []QueryToSend var queriesToSend []QueryToSend
where := q.buildWhereClause() where := q.buildWhereClause()
functions := q.buildFunctionList()
for _, v := range q.Metrics { for _, v := range q.Metrics {
if !containsWildcardPattern.Match([]byte(v.Metric)) { if !containsWildcardPattern.Match([]byte(v.Metric)) {
...@@ -42,9 +48,11 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) { ...@@ -42,9 +48,11 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
if v.Alias != "" { if v.Alias != "" {
alias = fmt.Sprintf(" {%s}", v.Alias) alias = fmt.Sprintf(" {%s}", v.Alias)
} }
rawQuery := fmt.Sprintf( rawQuery := fmt.Sprintf(
"`%s`%s %s from %v to %v", "`%s`%s%s %s from %v to %v",
v.Metric, v.Metric,
functions,
alias, alias,
where, where,
q.TimeRange.GetFromAsMsEpoch(), q.TimeRange.GetFromAsMsEpoch(),
...@@ -73,8 +81,9 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) { ...@@ -73,8 +81,9 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
} }
rawQuery := fmt.Sprintf( rawQuery := fmt.Sprintf(
"`%s`%s %s from %v to %v", "`%s`%s%s %s from %v to %v",
a, a,
functions,
alias, alias,
where, where,
q.TimeRange.GetFromAsMsEpoch(), q.TimeRange.GetFromAsMsEpoch(),
...@@ -90,6 +99,15 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) { ...@@ -90,6 +99,15 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
return queriesToSend, nil 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 { func (q *Query) buildWhereClause() string {
hasApps := len(q.Cluster) > 0 hasApps := len(q.Cluster) > 0
hasHosts := len(q.Hosts) > 0 hasHosts := len(q.Hosts) > 0
......
...@@ -35,15 +35,18 @@ func TestWildcardExpansion(t *testing.T) { ...@@ -35,15 +35,18 @@ func TestWildcardExpansion(t *testing.T) {
Cluster: []string{"demoapp-1", "demoapp-2"}, Cluster: []string{"demoapp-1", "demoapp-2"},
AddClusterToAlias: false, AddClusterToAlias: false,
AddHostToAlias: 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) expandeQueries, err := query.Build(availableMetrics)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(len(expandeQueries), ShouldEqual, 3) 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[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` 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` {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[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() { Convey("Containg wildcard series", func() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment