2016-10-05 03:56:34 -05:00
package influxdb
import (
"testing"
2016-11-07 07:26:20 -06:00
"strings"
2016-10-06 05:51:45 -05:00
"github.com/grafana/grafana/pkg/tsdb"
2016-10-05 03:56:34 -05:00
. "github.com/smartystreets/goconvey/convey"
)
func TestInfluxdbQueryBuilder ( t * testing . T ) {
2016-10-07 04:23:37 -05:00
2016-10-05 03:56:34 -05:00
Convey ( "Influxdb query builder" , t , func ( ) {
2016-10-05 13:36:05 -05:00
qp1 , _ := NewQueryPart ( "field" , [ ] string { "value" } )
qp2 , _ := NewQueryPart ( "mean" , [ ] string { } )
2017-01-11 05:10:26 -06:00
mathPartDivideBy100 , _ := NewQueryPart ( "math" , [ ] string { "/ 100" } )
mathPartDivideByIntervalMs , _ := NewQueryPart ( "math" , [ ] string { "/ $__interval_ms" } )
groupBy1 , _ := NewQueryPart ( "time" , [ ] string { "$__interval" } )
2016-10-07 08:09:54 -05:00
groupBy2 , _ := NewQueryPart ( "tag" , [ ] string { "datacenter" } )
groupBy3 , _ := NewQueryPart ( "fill" , [ ] string { "null" } )
2016-10-05 13:36:05 -05:00
2017-01-11 05:10:26 -06:00
groupByOldInterval , _ := NewQueryPart ( "time" , [ ] string { "$interval" } )
2016-10-05 13:36:05 -05:00
tag1 := & Tag { Key : "hostname" , Value : "server1" , Operator : "=" }
tag2 := & Tag { Key : "hostname" , Value : "server2" , Operator : "=" , Condition : "OR" }
2016-10-05 03:56:34 -05:00
2017-09-20 11:31:34 -05:00
queryContext := & tsdb . TsdbQuery {
2016-10-07 04:33:17 -05:00
TimeRange : tsdb . NewTimeRange ( "5m" , "now" ) ,
2016-10-06 05:51:45 -05:00
}
2016-10-06 07:16:26 -05:00
Convey ( "can build simple query" , func ( ) {
2016-10-05 13:36:05 -05:00
query := & Query {
Selects : [ ] * Select { { * qp1 , * qp2 } } ,
Measurement : "cpu" ,
2016-10-05 13:57:28 -05:00
Policy : "policy" ,
2016-10-07 08:09:54 -05:00
GroupBy : [ ] * QueryPart { groupBy1 , groupBy3 } ,
2016-10-06 05:51:45 -05:00
Interval : "10s" ,
2016-10-05 13:36:05 -05:00
}
2016-11-10 07:38:06 -06:00
rawQuery , err := query . Build ( queryContext )
2016-10-05 13:36:05 -05:00
So ( err , ShouldBeNil )
2016-10-13 04:42:51 -05:00
So ( rawQuery , ShouldEqual , ` SELECT mean("value") FROM "policy"."cpu" WHERE time > now() - 5m GROUP BY time(10s) fill(null) ` )
2016-10-05 03:56:34 -05:00
} )
2016-10-06 07:16:26 -05:00
Convey ( "can build query with group bys" , func ( ) {
2016-10-05 13:36:05 -05:00
query := & Query {
Selects : [ ] * Select { { * qp1 , * qp2 } } ,
Measurement : "cpu" ,
2016-10-07 08:09:54 -05:00
GroupBy : [ ] * QueryPart { groupBy1 , groupBy2 , groupBy3 } ,
2016-10-05 13:36:05 -05:00
Tags : [ ] * Tag { tag1 , tag2 } ,
2016-10-06 05:51:45 -05:00
Interval : "5s" ,
2016-10-05 13:36:05 -05:00
}
2016-11-10 07:38:06 -06:00
rawQuery , err := query . Build ( queryContext )
2016-10-05 13:36:05 -05:00
So ( err , ShouldBeNil )
2017-08-31 02:33:03 -05:00
So ( rawQuery , ShouldEqual , ` SELECT mean("value") FROM "cpu" WHERE ("hostname" = 'server1' OR "hostname" = 'server2') AND time > now() - 5m GROUP BY time(5s), "datacenter" fill(null) ` )
2016-10-07 04:23:37 -05:00
} )
2017-01-11 05:10:26 -06:00
Convey ( "can build query with math part" , func ( ) {
query := & Query {
Selects : [ ] * Select { { * qp1 , * qp2 , * mathPartDivideBy100 } } ,
Measurement : "cpu" ,
Interval : "5s" ,
}
rawQuery , err := query . Build ( queryContext )
So ( err , ShouldBeNil )
So ( rawQuery , ShouldEqual , ` SELECT mean("value") / 100 FROM "cpu" WHERE time > now() - 5m ` )
} )
Convey ( "can build query with math part using $__interval_ms variable" , func ( ) {
query := & Query {
Selects : [ ] * Select { { * qp1 , * qp2 , * mathPartDivideByIntervalMs } } ,
Measurement : "cpu" ,
Interval : "5s" ,
}
rawQuery , err := query . Build ( queryContext )
So ( err , ShouldBeNil )
So ( rawQuery , ShouldEqual , ` SELECT mean("value") / 5000 FROM "cpu" WHERE time > now() - 5m ` )
} )
Convey ( "can build query with old $interval variable" , func ( ) {
query := & Query {
Selects : [ ] * Select { { * qp1 , * qp2 } } ,
Measurement : "cpu" ,
Policy : "" ,
GroupBy : [ ] * QueryPart { groupByOldInterval } ,
}
rawQuery , err := query . Build ( queryContext )
So ( err , ShouldBeNil )
So ( rawQuery , ShouldEqual , ` SELECT mean("value") FROM "cpu" WHERE time > now() - 5m GROUP BY time(200ms) ` )
} )
2016-10-07 04:23:37 -05:00
Convey ( "can render time range" , func ( ) {
2016-10-07 04:33:17 -05:00
query := Query { }
2016-10-07 04:23:37 -05:00
Convey ( "render from: 2h to now-1h" , func ( ) {
2016-10-07 04:33:17 -05:00
query := Query { }
2017-09-20 11:31:34 -05:00
queryContext := & tsdb . TsdbQuery { TimeRange : tsdb . NewTimeRange ( "2h" , "now-1h" ) }
2016-11-10 07:38:06 -06:00
So ( query . renderTimeFilter ( queryContext ) , ShouldEqual , "time > now() - 2h and time < now() - 1h" )
2016-10-07 04:23:37 -05:00
} )
Convey ( "render from: 10m" , func ( ) {
2017-09-20 11:31:34 -05:00
queryContext := & tsdb . TsdbQuery { TimeRange : tsdb . NewTimeRange ( "10m" , "now" ) }
2016-11-10 07:38:06 -06:00
So ( query . renderTimeFilter ( queryContext ) , ShouldEqual , "time > now() - 10m" )
2016-10-07 04:23:37 -05:00
} )
2016-10-05 03:56:34 -05:00
} )
2016-10-11 11:29:09 -05:00
Convey ( "can build query from raw query" , func ( ) {
query := & Query {
Selects : [ ] * Select { { * qp1 , * qp2 } } ,
Measurement : "cpu" ,
Policy : "policy" ,
GroupBy : [ ] * QueryPart { groupBy1 , groupBy3 } ,
Interval : "10s" ,
RawQuery : "Raw query" ,
2016-11-10 07:16:18 -06:00
UseRawQuery : true ,
2016-10-11 11:29:09 -05:00
}
2016-11-10 07:38:06 -06:00
rawQuery , err := query . Build ( queryContext )
2016-10-11 11:29:09 -05:00
So ( err , ShouldBeNil )
So ( rawQuery , ShouldEqual , ` Raw query ` )
} )
2016-11-07 07:26:20 -06:00
2016-11-10 03:41:00 -06:00
Convey ( "can render normal tags without operator" , func ( ) {
2017-01-27 08:21:02 -06:00
query := & Query { Tags : [ ] * Tag { { Operator : "" , Value : ` value ` , Key : "key" } } }
2016-11-10 03:41:00 -06:00
2016-11-10 07:38:06 -06:00
So ( strings . Join ( query . renderTags ( ) , "" ) , ShouldEqual , ` "key" = 'value' ` )
2016-11-10 03:41:00 -06:00
} )
Convey ( "can render regex tags without operator" , func ( ) {
2017-01-27 08:21:02 -06:00
query := & Query { Tags : [ ] * Tag { { Operator : "" , Value : ` /value/ ` , Key : "key" } } }
2016-11-10 03:41:00 -06:00
2016-11-10 07:38:06 -06:00
So ( strings . Join ( query . renderTags ( ) , "" ) , ShouldEqual , ` "key" =~ /value/ ` )
2016-11-10 03:41:00 -06:00
} )
2016-11-07 07:26:20 -06:00
Convey ( "can render regex tags" , func ( ) {
2017-01-27 08:21:02 -06:00
query := & Query { Tags : [ ] * Tag { { Operator : "=~" , Value : ` /value/ ` , Key : "key" } } }
2016-11-07 07:26:20 -06:00
2016-11-10 07:38:06 -06:00
So ( strings . Join ( query . renderTags ( ) , "" ) , ShouldEqual , ` "key" =~ /value/ ` )
2016-11-07 07:26:20 -06:00
} )
Convey ( "can render number tags" , func ( ) {
2017-01-27 08:21:02 -06:00
query := & Query { Tags : [ ] * Tag { { Operator : "=" , Value : "10001" , Key : "key" } } }
2016-11-10 03:41:00 -06:00
2016-11-17 08:47:15 -06:00
So ( strings . Join ( query . renderTags ( ) , "" ) , ShouldEqual , ` "key" = '10001' ` )
2016-11-10 03:41:00 -06:00
} )
2016-11-17 08:47:15 -06:00
Convey ( "can render numbers less then condition tags" , func ( ) {
2017-01-27 08:21:02 -06:00
query := & Query { Tags : [ ] * Tag { { Operator : "<" , Value : "10001" , Key : "key" } } }
2016-11-07 07:26:20 -06:00
2016-11-17 08:47:15 -06:00
So ( strings . Join ( query . renderTags ( ) , "" ) , ShouldEqual , ` "key" < 10001 ` )
} )
Convey ( "can render number greather then condition tags" , func ( ) {
2017-01-27 08:21:02 -06:00
query := & Query { Tags : [ ] * Tag { { Operator : ">" , Value : "10001" , Key : "key" } } }
2016-11-17 08:47:15 -06:00
So ( strings . Join ( query . renderTags ( ) , "" ) , ShouldEqual , ` "key" > 10001 ` )
2016-11-07 07:26:20 -06:00
} )
Convey ( "can render string tags" , func ( ) {
2017-01-27 08:21:02 -06:00
query := & Query { Tags : [ ] * Tag { { Operator : "=" , Value : "value" , Key : "key" } } }
2016-11-07 07:26:20 -06:00
2016-11-10 07:38:06 -06:00
So ( strings . Join ( query . renderTags ( ) , "" ) , ShouldEqual , ` "key" = 'value' ` )
2016-11-07 07:26:20 -06:00
} )
2016-11-14 01:47:45 -06:00
Convey ( "can render regular measurement" , func ( ) {
query := & Query { Measurement : ` apa ` , Policy : "policy" }
So ( query . renderMeasurement ( ) , ShouldEqual , ` FROM "policy"."apa" ` )
} )
Convey ( "can render regexp measurement" , func ( ) {
query := & Query { Measurement : ` /apa/ ` , Policy : "policy" }
So ( query . renderMeasurement ( ) , ShouldEqual , ` FROM "policy"./apa/ ` )
} )
2016-10-05 03:56:34 -05:00
} )
2017-01-11 05:10:26 -06:00
2016-10-05 03:56:34 -05:00
}