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
3552a4cb
Commit
3552a4cb
authored
Aug 14, 2018
by
Sven Klemm
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor timescaledb handling in MacroEngine
parent
a4a33d80
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
18 additions
and
35 deletions
+18
-35
pkg/tsdb/postgres/macros.go
+9
-6
pkg/tsdb/postgres/macros_test.go
+8
-6
pkg/tsdb/postgres/postgres.go
+1
-1
pkg/tsdb/postgres/postgres_test.go
+0
-22
No files found.
pkg/tsdb/postgres/macros.go
View file @
3552a4cb
...
...
@@ -7,6 +7,7 @@ import (
"strings"
"time"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/tsdb"
)
...
...
@@ -15,12 +16,15 @@ const rsIdentifier = `([_a-zA-Z0-9]+)`
const
sExpr
=
`\$`
+
rsIdentifier
+
`\(([^\)]*)\)`
type
postgresMacroEngine
struct
{
timeRange
*
tsdb
.
TimeRange
query
*
tsdb
.
Query
timeRange
*
tsdb
.
TimeRange
query
*
tsdb
.
Query
timescaledb
bool
}
func
newPostgresMacroEngine
()
tsdb
.
SqlMacroEngine
{
return
&
postgresMacroEngine
{}
func
newPostgresMacroEngine
(
datasource
*
models
.
DataSource
)
tsdb
.
SqlMacroEngine
{
engine
:=
&
postgresMacroEngine
{}
engine
.
timescaledb
=
datasource
.
JsonData
.
Get
(
"timescaledb"
)
.
MustBool
(
false
)
return
engine
}
func
(
m
*
postgresMacroEngine
)
Interpolate
(
query
*
tsdb
.
Query
,
timeRange
*
tsdb
.
TimeRange
,
sql
string
)
(
string
,
error
)
{
...
...
@@ -131,7 +135,7 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string,
}
}
if
m
.
query
.
DataSource
.
JsonData
.
Get
(
"timescaledb"
)
.
MustBool
()
{
if
m
.
timescaledb
{
return
fmt
.
Sprintf
(
"time_bucket('%vs',%s)"
,
interval
.
Seconds
(),
args
[
0
]),
nil
}
else
{
return
fmt
.
Sprintf
(
"floor(extract(epoch from %s)/%v)*%v"
,
args
[
0
],
interval
.
Seconds
(),
interval
.
Seconds
()),
nil
...
...
@@ -142,7 +146,6 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string,
return
tg
+
" AS
\"
time
\"
"
,
err
}
return
""
,
err
case
"__unixEpochFilter"
:
if
len
(
args
)
==
0
{
return
""
,
fmt
.
Errorf
(
"missing time column argument for macro %v"
,
name
)
...
...
pkg/tsdb/postgres/macros_test.go
View file @
3552a4cb
...
...
@@ -14,10 +14,12 @@ import (
func
TestMacroEngine
(
t
*
testing
.
T
)
{
Convey
(
"MacroEngine"
,
t
,
func
()
{
engine
:=
newPostgresMacroEngine
()
query
:=
&
tsdb
.
Query
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()}}
queryTS
:=
&
tsdb
.
Query
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()}}
queryTS
.
DataSource
.
JsonData
.
Set
(
"timescaledb"
,
true
)
datasource
:=
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()}
engine
:=
newPostgresMacroEngine
(
datasource
)
datasourceTS
:=
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()}
datasourceTS
.
JsonData
.
Set
(
"timescaledb"
,
true
)
engineTS
:=
newPostgresMacroEngine
(
datasourceTS
)
query
:=
&
tsdb
.
Query
{}
Convey
(
"Given a time range between 2018-04-12 00:00 and 2018-04-12 00:05"
,
func
()
{
from
:=
time
.
Date
(
2018
,
4
,
12
,
18
,
0
,
0
,
0
,
time
.
UTC
)
...
...
@@ -89,7 +91,7 @@ func TestMacroEngine(t *testing.T) {
Convey
(
"interpolate __timeGroup function with TimescaleDB enabled"
,
func
()
{
sql
,
err
:=
engine
.
Interpolate
(
queryTS
,
timeRange
,
"GROUP BY $__timeGroup(time_column,'5m')"
)
sql
,
err
:=
engine
TS
.
Interpolate
(
query
,
timeRange
,
"GROUP BY $__timeGroup(time_column,'5m')"
)
So
(
err
,
ShouldBeNil
)
So
(
sql
,
ShouldEqual
,
"GROUP BY time_bucket('300s',time_column)"
)
...
...
@@ -97,7 +99,7 @@ func TestMacroEngine(t *testing.T) {
Convey
(
"interpolate __timeGroup function with spaces between args and TimescaleDB enabled"
,
func
()
{
sql
,
err
:=
engine
.
Interpolate
(
queryTS
,
timeRange
,
"GROUP BY $__timeGroup(time_column , '5m')"
)
sql
,
err
:=
engine
TS
.
Interpolate
(
query
,
timeRange
,
"GROUP BY $__timeGroup(time_column , '5m')"
)
So
(
err
,
ShouldBeNil
)
So
(
sql
,
ShouldEqual
,
"GROUP BY time_bucket('300s',time_column)"
)
...
...
pkg/tsdb/postgres/postgres.go
View file @
3552a4cb
...
...
@@ -32,7 +32,7 @@ func newPostgresQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndp
log
:
logger
,
}
return
tsdb
.
NewSqlQueryEndpoint
(
&
config
,
&
rowTransformer
,
newPostgresMacroEngine
(),
logger
)
return
tsdb
.
NewSqlQueryEndpoint
(
&
config
,
&
rowTransformer
,
newPostgresMacroEngine
(
datasource
),
logger
)
}
func
generateConnectionString
(
datasource
*
models
.
DataSource
)
string
{
...
...
pkg/tsdb/postgres/postgres_test.go
View file @
3552a4cb
...
...
@@ -102,7 +102,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
"SELECT * FROM postgres_types"
,
"format"
:
"table"
,
...
...
@@ -183,7 +182,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
"SELECT $__timeGroup(time, '5m') AS time, avg(value) as value FROM metric GROUP BY 1 ORDER BY 1"
,
"format"
:
"time_series"
,
...
...
@@ -228,7 +226,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
"SELECT $__timeGroup(time, '5m', NULL) AS time, avg(value) as value FROM metric GROUP BY 1 ORDER BY 1"
,
"format"
:
"time_series"
,
...
...
@@ -283,7 +280,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
"SELECT $__timeGroup(time, '5m', 1.5) AS time, avg(value) as value FROM metric GROUP BY 1 ORDER BY 1"
,
"format"
:
"time_series"
,
...
...
@@ -311,7 +307,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
"SELECT $__timeGroup(time, '5m', previous), avg(value) as value FROM metric GROUP BY 1 ORDER BY 1"
,
"format"
:
"time_series"
,
...
...
@@ -406,7 +401,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT "timeInt64" as time, "timeInt64" FROM metric_values ORDER BY time LIMIT 1`
,
"format"
:
"time_series"
,
...
...
@@ -429,7 +423,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT "timeInt64Nullable" as time, "timeInt64Nullable" FROM metric_values ORDER BY time LIMIT 1`
,
"format"
:
"time_series"
,
...
...
@@ -452,7 +445,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT "timeFloat64" as time, "timeFloat64" FROM metric_values ORDER BY time LIMIT 1`
,
"format"
:
"time_series"
,
...
...
@@ -475,7 +467,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT "timeFloat64Nullable" as time, "timeFloat64Nullable" FROM metric_values ORDER BY time LIMIT 1`
,
"format"
:
"time_series"
,
...
...
@@ -520,7 +511,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT "timeInt32Nullable" as time, "timeInt32Nullable" FROM metric_values ORDER BY time LIMIT 1`
,
"format"
:
"time_series"
,
...
...
@@ -543,7 +533,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT "timeFloat32" as time, "timeFloat32" FROM metric_values ORDER BY time LIMIT 1`
,
"format"
:
"time_series"
,
...
...
@@ -566,7 +555,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT "timeFloat32Nullable" as time, "timeFloat32Nullable" FROM metric_values ORDER BY time LIMIT 1`
,
"format"
:
"time_series"
,
...
...
@@ -589,7 +577,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT $__timeEpoch(time), measurement || ' - value one' as metric, "valueOne" FROM metric_values ORDER BY 1`
,
"format"
:
"time_series"
,
...
...
@@ -638,7 +625,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT $__timeEpoch(time), "valueOne", "valueTwo" FROM metric_values ORDER BY 1`
,
"format"
:
"time_series"
,
...
...
@@ -696,7 +682,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT "time_sec" as time, description as text, tags FROM event WHERE $__unixEpochFilter(time_sec) AND tags='deploy' ORDER BY 1 ASC`
,
"format"
:
"table"
,
...
...
@@ -720,7 +705,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT "time_sec" as time, description as text, tags FROM event WHERE $__unixEpochFilter(time_sec) AND tags='ticket' ORDER BY 1 ASC`
,
"format"
:
"table"
,
...
...
@@ -747,7 +731,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
fmt
.
Sprintf
(
`SELECT
CAST('%s' AS TIMESTAMP) as time,
...
...
@@ -778,7 +761,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
fmt
.
Sprintf
(
`SELECT
%d as time,
...
...
@@ -809,7 +791,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
fmt
.
Sprintf
(
`SELECT
cast(%d as bigint) as time,
...
...
@@ -840,7 +821,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
fmt
.
Sprintf
(
`SELECT
%d as time,
...
...
@@ -869,7 +849,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT
cast(null as bigint) as time,
...
...
@@ -898,7 +877,6 @@ func TestPostgres(t *testing.T) {
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
DataSource
:
&
models
.
DataSource
{
JsonData
:
simplejson
.
New
()},
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
`SELECT
cast(null as timestamp) as time,
...
...
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