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,
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
}
......@@ -61,6 +61,14 @@ func TestMQEQueryParser(t *testing.T) {
"metric": "os.disk.sda.io_time"
}
],
"functionList": [
{
"func": "aggregate.min"
},
{
"func": "aggregate.max"
}
],
"rawQuery": "",
"refId": "A",
"addClusterToAlias": true,
......@@ -76,6 +84,8 @@ func TestMQEQueryParser(t *testing.T) {
So(query.Cluster[0], ShouldEqual, "demoapp")
So(query.Metrics[0].Metric, ShouldEqual, "os.cpu.all.active_percentage")
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() {
......
......@@ -16,10 +16,15 @@ type Metric struct {
Alias string
}
type Function struct {
Func string
}
type Query struct {
Metrics []Metric
Hosts []string
Cluster []string
FunctionList []Function
AddClusterToAlias bool
AddHostToAlias bool
......@@ -35,6 +40,7 @@ var (
func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
var queriesToSend []QueryToSend
where := q.buildWhereClause()
functions := q.buildFunctionList()
for _, v := range q.Metrics {
if !containsWildcardPattern.Match([]byte(v.Metric)) {
......@@ -42,9 +48,11 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
if v.Alias != "" {
alias = fmt.Sprintf(" {%s}", v.Alias)
}
rawQuery := fmt.Sprintf(
"`%s`%s %s from %v to %v",
"`%s`%s%s %s from %v to %v",
v.Metric,
functions,
alias,
where,
q.TimeRange.GetFromAsMsEpoch(),
......@@ -73,8 +81,9 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
}
rawQuery := fmt.Sprintf(
"`%s`%s %s from %v to %v",
"`%s`%s%s %s from %v to %v",
a,
functions,
alias,
where,
q.TimeRange.GetFromAsMsEpoch(),
......@@ -90,6 +99,15 @@ func (q *Query) Build(availableSeries []string) ([]QueryToSend, error) {
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 {
hasApps := len(q.Cluster) > 0
hasHosts := len(q.Hosts) > 0
......
......@@ -35,15 +35,18 @@ func TestWildcardExpansion(t *testing.T) {
Cluster: []string{"demoapp-1", "demoapp-2"},
AddClusterToAlias: 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)
So(err, ShouldBeNil)
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[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[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[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`|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`|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() {
......
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