Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
4f3c8c66
Commit
4f3c8c66
authored
Nov 10, 2016
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(influxdb): add default operator
ref #6523
parent
71eb0f32
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
10 deletions
+44
-10
pkg/tsdb/influxdb/query_builder.go
+22
-6
pkg/tsdb/influxdb/query_builder_test.go
+22
-4
No files found.
pkg/tsdb/influxdb/query_builder.go
View file @
4f3c8c66
...
@@ -5,9 +5,15 @@ import (
...
@@ -5,9 +5,15 @@ import (
"strconv"
"strconv"
"strings"
"strings"
"regexp"
"github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/tsdb"
)
)
var
(
regexpOperatorPattern
*
regexp
.
Regexp
=
regexp
.
MustCompile
(
`^\/.*\/$`
)
)
type
QueryBuilder
struct
{}
type
QueryBuilder
struct
{}
func
(
qb
*
QueryBuilder
)
Build
(
query
*
Query
,
queryContext
*
tsdb
.
QueryContext
)
(
string
,
error
)
{
func
(
qb
*
QueryBuilder
)
Build
(
query
*
Query
,
queryContext
*
tsdb
.
QueryContext
)
(
string
,
error
)
{
...
@@ -43,18 +49,28 @@ func (qb *QueryBuilder) renderTags(query *Query) []string {
...
@@ -43,18 +49,28 @@ func (qb *QueryBuilder) renderTags(query *Query) []string {
str
+=
" "
str
+=
" "
}
}
value
:=
tag
.
Value
//If the operator is missing we fall back to sensible defaults
nValue
,
err
:=
strconv
.
ParseFloat
(
tag
.
Value
,
64
)
if
tag
.
Operator
==
""
{
if
regexpOperatorPattern
.
Match
([]
byte
(
tag
.
Value
))
{
tag
.
Operator
=
"=~"
}
else
{
tag
.
Operator
=
"="
}
}
textValue
:=
""
numericValue
,
err
:=
strconv
.
ParseFloat
(
tag
.
Value
,
64
)
// quote value unless regex or number
if
tag
.
Operator
==
"=~"
||
tag
.
Operator
==
"!~"
{
if
tag
.
Operator
==
"=~"
||
tag
.
Operator
==
"!~"
{
value
=
fmt
.
Sprintf
(
"%s"
,
value
)
textValue
=
tag
.
Value
}
else
if
err
==
nil
{
}
else
if
err
==
nil
{
value
=
fmt
.
Sprintf
(
"%v"
,
n
Value
)
textValue
=
fmt
.
Sprintf
(
"%v"
,
numeric
Value
)
}
else
{
}
else
{
value
=
fmt
.
Sprintf
(
"'%s'"
,
v
alue
)
textValue
=
fmt
.
Sprintf
(
"'%s'"
,
tag
.
V
alue
)
}
}
res
=
append
(
res
,
fmt
.
Sprintf
(
`%s"%s" %s %s`
,
str
,
tag
.
Key
,
tag
.
Operator
,
v
alue
))
res
=
append
(
res
,
fmt
.
Sprintf
(
`%s"%s" %s %s`
,
str
,
tag
.
Key
,
tag
.
Operator
,
textV
alue
))
}
}
return
res
return
res
...
...
pkg/tsdb/influxdb/query_builder_test.go
View file @
4f3c8c66
...
@@ -86,16 +86,34 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
...
@@ -86,16 +86,34 @@ func TestInfluxdbQueryBuilder(t *testing.T) {
So
(
rawQuery
,
ShouldEqual
,
`Raw query`
)
So
(
rawQuery
,
ShouldEqual
,
`Raw query`
)
})
})
Convey
(
"can render normal tags without operator"
,
func
()
{
query
:=
&
Query
{
Tags
:
[]
*
Tag
{
&
Tag
{
Operator
:
""
,
Value
:
`value`
,
Key
:
"key"
}}}
So
(
strings
.
Join
(
builder
.
renderTags
(
query
),
""
),
ShouldEqual
,
`"key" = 'value'`
)
})
Convey
(
"can render regex tags without operator"
,
func
()
{
query
:=
&
Query
{
Tags
:
[]
*
Tag
{
&
Tag
{
Operator
:
""
,
Value
:
`/value/`
,
Key
:
"key"
}}}
So
(
strings
.
Join
(
builder
.
renderTags
(
query
),
""
),
ShouldEqual
,
`"key" =~ /value/`
)
})
Convey
(
"can render regex tags"
,
func
()
{
Convey
(
"can render regex tags"
,
func
()
{
query
:=
&
Query
{
Tags
:
[]
*
Tag
{
&
Tag
{
Operator
:
"=~"
,
Value
:
"value"
,
Key
:
"key"
}}}
query
:=
&
Query
{
Tags
:
[]
*
Tag
{
&
Tag
{
Operator
:
"=~"
,
Value
:
`/value/`
,
Key
:
"key"
}}}
So
(
strings
.
Join
(
builder
.
renderTags
(
query
),
""
),
ShouldEqual
,
`"key" =~
value
`
)
So
(
strings
.
Join
(
builder
.
renderTags
(
query
),
""
),
ShouldEqual
,
`"key" =~
/value/
`
)
})
})
Convey
(
"can render number tags"
,
func
()
{
Convey
(
"can render number tags"
,
func
()
{
query
:=
&
Query
{
Tags
:
[]
*
Tag
{
&
Tag
{
Operator
:
"="
,
Value
:
"1"
,
Key
:
"key"
}}}
query
:=
&
Query
{
Tags
:
[]
*
Tag
{
&
Tag
{
Operator
:
"="
,
Value
:
"10001"
,
Key
:
"key"
}}}
So
(
strings
.
Join
(
builder
.
renderTags
(
query
),
""
),
ShouldEqual
,
`"key" = 10001`
)
})
Convey
(
"can render number tags with decimals"
,
func
()
{
query
:=
&
Query
{
Tags
:
[]
*
Tag
{
&
Tag
{
Operator
:
"="
,
Value
:
"10001.1"
,
Key
:
"key"
}}}
So
(
strings
.
Join
(
builder
.
renderTags
(
query
),
""
),
ShouldEqual
,
`"key" = 1`
)
So
(
strings
.
Join
(
builder
.
renderTags
(
query
),
""
),
ShouldEqual
,
`"key" = 1
0001.1
`
)
})
})
Convey
(
"can render string tags"
,
func
()
{
Convey
(
"can render string tags"
,
func
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment