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
a314890f
Unverified
Commit
a314890f
authored
Apr 10, 2018
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tsdb: add support for more data types when converting sql time column to epoch (ms)
parent
b610f77b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
192 additions
and
25 deletions
+192
-25
pkg/tsdb/sql_engine.go
+27
-3
pkg/tsdb/sql_engine_test.go
+160
-22
pkg/tsdb/time_range.go
+5
-0
No files found.
pkg/tsdb/sql_engine.go
View file @
a314890f
...
@@ -135,16 +135,16 @@ func (e *DefaultSqlEngine) Query(
...
@@ -135,16 +135,16 @@ func (e *DefaultSqlEngine) Query(
return
result
,
nil
return
result
,
nil
}
}
// ConvertTimeColumnToEpochMs converts column named time to unix timestamp in milliseconds
// Convert
Sql
TimeColumnToEpochMs converts column named time to unix timestamp in milliseconds
// to make native datetime types and epoch dates work in annotation and table queries.
// to make native datetime types and epoch dates work in annotation and table queries.
func
ConvertSqlTimeColumnToEpochMs
(
values
RowValues
,
timeIndex
int
)
{
func
ConvertSqlTimeColumnToEpochMs
(
values
RowValues
,
timeIndex
int
)
{
if
timeIndex
>=
0
{
if
timeIndex
>=
0
{
switch
value
:=
values
[
timeIndex
]
.
(
type
)
{
switch
value
:=
values
[
timeIndex
]
.
(
type
)
{
case
time
.
Time
:
case
time
.
Time
:
values
[
timeIndex
]
=
EpochPrecisionToMs
(
float64
(
value
.
Unix
()))
values
[
timeIndex
]
=
EpochPrecisionToMs
(
float64
(
value
.
Unix
Nano
()))
case
*
time
.
Time
:
case
*
time
.
Time
:
if
value
!=
nil
{
if
value
!=
nil
{
values
[
timeIndex
]
=
EpochPrecisionToMs
(
float64
((
*
value
)
.
Unix
()))
values
[
timeIndex
]
=
EpochPrecisionToMs
(
float64
((
*
value
)
.
Unix
Nano
()))
}
}
case
int64
:
case
int64
:
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
value
)))
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
value
)))
...
@@ -152,12 +152,36 @@ func ConvertSqlTimeColumnToEpochMs(values RowValues, timeIndex int) {
...
@@ -152,12 +152,36 @@ func ConvertSqlTimeColumnToEpochMs(values RowValues, timeIndex int) {
if
value
!=
nil
{
if
value
!=
nil
{
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
*
value
)))
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
*
value
)))
}
}
case
uint64
:
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
value
)))
case
*
uint64
:
if
value
!=
nil
{
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
*
value
)))
}
case
int32
:
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
value
)))
case
*
int32
:
if
value
!=
nil
{
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
*
value
)))
}
case
uint32
:
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
value
)))
case
*
uint32
:
if
value
!=
nil
{
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
*
value
)))
}
case
float64
:
case
float64
:
values
[
timeIndex
]
=
EpochPrecisionToMs
(
value
)
values
[
timeIndex
]
=
EpochPrecisionToMs
(
value
)
case
*
float64
:
case
*
float64
:
if
value
!=
nil
{
if
value
!=
nil
{
values
[
timeIndex
]
=
EpochPrecisionToMs
(
*
value
)
values
[
timeIndex
]
=
EpochPrecisionToMs
(
*
value
)
}
}
case
float32
:
values
[
timeIndex
]
=
EpochPrecisionToMs
(
float64
(
value
))
case
*
float32
:
if
value
!=
nil
{
values
[
timeIndex
]
=
EpochPrecisionToMs
(
float64
(
*
value
))
}
}
}
}
}
}
}
pkg/tsdb/sql_engine_test.go
View file @
a314890f
...
@@ -9,37 +9,175 @@ import (
...
@@ -9,37 +9,175 @@ import (
func
TestSqlEngine
(
t
*
testing
.
T
)
{
func
TestSqlEngine
(
t
*
testing
.
T
)
{
Convey
(
"SqlEngine"
,
t
,
func
()
{
Convey
(
"SqlEngine"
,
t
,
func
()
{
Convey
(
"Given row values with time columns when converting them"
,
func
()
{
dt
:=
time
.
Date
(
2018
,
3
,
14
,
21
,
20
,
6
,
527e6
,
time
.
UTC
)
dt
:=
time
.
Date
(
2018
,
3
,
14
,
21
,
20
,
6
,
527e6
,
time
.
UTC
)
fixtures
:=
make
([]
interface
{},
8
)
Convey
(
"Given row values with time.Time as time columns"
,
func
()
{
var
nilPointer
*
time
.
Time
fixtures
:=
make
([]
interface
{},
3
)
fixtures
[
0
]
=
dt
fixtures
[
0
]
=
dt
fixtures
[
1
]
=
dt
.
Unix
()
*
1000
fixtures
[
1
]
=
&
dt
fixtures
[
2
]
=
dt
.
Unix
()
fixtures
[
2
]
=
nilPointer
fixtures
[
3
]
=
float64
(
dt
.
Unix
()
*
1000
)
fixtures
[
4
]
=
float64
(
dt
.
Unix
())
var
nilDt
*
time
.
Time
var
nilInt64
*
int64
var
nilFloat64
*
float64
fixtures
[
5
]
=
nilDt
fixtures
[
6
]
=
nilInt64
fixtures
[
7
]
=
nilFloat64
for
i
:=
range
fixtures
{
for
i
:=
range
fixtures
{
ConvertSqlTimeColumnToEpochMs
(
fixtures
,
i
)
ConvertSqlTimeColumnToEpochMs
(
fixtures
,
i
)
}
}
Convey
(
"
Should convert sql time columns to epoch time in ms
"
,
func
()
{
Convey
(
"
When converting them should return epoch time with millisecond precision
"
,
func
()
{
expected
:=
float64
(
dt
.
Unix
()
*
1000
)
expected
:=
float64
(
dt
.
Unix
Nano
()
/
1e6
)
So
(
fixtures
[
0
]
.
(
float64
),
ShouldEqual
,
expected
)
So
(
fixtures
[
0
]
.
(
float64
),
ShouldEqual
,
expected
)
So
(
fixtures
[
1
]
.
(
in
t64
),
ShouldEqual
,
expected
)
So
(
fixtures
[
1
]
.
(
floa
t64
),
ShouldEqual
,
expected
)
So
(
fixtures
[
2
]
.
(
int64
),
ShouldEqual
,
expected
)
So
(
fixtures
[
2
]
,
ShouldBeNil
)
So
(
fixtures
[
3
]
.
(
float64
),
ShouldEqual
,
expected
)
}
)
So
(
fixtures
[
4
]
.
(
float64
),
ShouldEqual
,
expected
)
}
)
So
(
fixtures
[
5
],
ShouldBeNil
)
Convey
(
"Given row values with int64 as time columns"
,
func
()
{
tSeconds
:=
dt
.
Unix
()
tMilliseconds
:=
dt
.
UnixNano
()
/
1e6
tNanoSeconds
:=
dt
.
UnixNano
()
var
nilPointer
*
int64
fixtures
:=
make
([]
interface
{},
7
)
fixtures
[
0
]
=
tSeconds
fixtures
[
1
]
=
&
tSeconds
fixtures
[
2
]
=
tMilliseconds
fixtures
[
3
]
=
&
tMilliseconds
fixtures
[
4
]
=
tNanoSeconds
fixtures
[
5
]
=
&
tNanoSeconds
fixtures
[
6
]
=
nilPointer
for
i
:=
range
fixtures
{
ConvertSqlTimeColumnToEpochMs
(
fixtures
,
i
)
}
Convey
(
"When converting them should return epoch time with millisecond precision "
,
func
()
{
So
(
fixtures
[
0
]
.
(
int64
),
ShouldEqual
,
tSeconds
*
1e3
)
So
(
fixtures
[
1
]
.
(
int64
),
ShouldEqual
,
tSeconds
*
1e3
)
So
(
fixtures
[
2
]
.
(
int64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
3
]
.
(
int64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
4
]
.
(
int64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
5
]
.
(
int64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
6
],
ShouldBeNil
)
So
(
fixtures
[
6
],
ShouldBeNil
)
So
(
fixtures
[
7
],
ShouldBeNil
)
})
})
Convey
(
"Given row values with uin64 as time columns"
,
func
()
{
tSeconds
:=
uint64
(
dt
.
Unix
())
tMilliseconds
:=
uint64
(
dt
.
UnixNano
()
/
1e6
)
tNanoSeconds
:=
uint64
(
dt
.
UnixNano
())
var
nilPointer
*
uint64
fixtures
:=
make
([]
interface
{},
7
)
fixtures
[
0
]
=
tSeconds
fixtures
[
1
]
=
&
tSeconds
fixtures
[
2
]
=
tMilliseconds
fixtures
[
3
]
=
&
tMilliseconds
fixtures
[
4
]
=
tNanoSeconds
fixtures
[
5
]
=
&
tNanoSeconds
fixtures
[
6
]
=
nilPointer
for
i
:=
range
fixtures
{
ConvertSqlTimeColumnToEpochMs
(
fixtures
,
i
)
}
Convey
(
"When converting them should return epoch time with millisecond precision "
,
func
()
{
So
(
fixtures
[
0
]
.
(
int64
),
ShouldEqual
,
tSeconds
*
1e3
)
So
(
fixtures
[
1
]
.
(
int64
),
ShouldEqual
,
tSeconds
*
1e3
)
So
(
fixtures
[
2
]
.
(
int64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
3
]
.
(
int64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
4
]
.
(
int64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
5
]
.
(
int64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
6
],
ShouldBeNil
)
})
})
Convey
(
"Given row values with int32 as time columns"
,
func
()
{
tSeconds
:=
int32
(
dt
.
Unix
())
var
nilInt
*
int32
fixtures
:=
make
([]
interface
{},
3
)
fixtures
[
0
]
=
tSeconds
fixtures
[
1
]
=
&
tSeconds
fixtures
[
2
]
=
nilInt
for
i
:=
range
fixtures
{
ConvertSqlTimeColumnToEpochMs
(
fixtures
,
i
)
}
Convey
(
"When converting them should return epoch time with millisecond precision "
,
func
()
{
So
(
fixtures
[
0
]
.
(
int64
),
ShouldEqual
,
dt
.
Unix
()
*
1e3
)
So
(
fixtures
[
1
]
.
(
int64
),
ShouldEqual
,
dt
.
Unix
()
*
1e3
)
So
(
fixtures
[
2
],
ShouldBeNil
)
})
})
Convey
(
"Given row values with uint32 as time columns"
,
func
()
{
tSeconds
:=
uint32
(
dt
.
Unix
())
var
nilInt
*
uint32
fixtures
:=
make
([]
interface
{},
3
)
fixtures
[
0
]
=
tSeconds
fixtures
[
1
]
=
&
tSeconds
fixtures
[
2
]
=
nilInt
for
i
:=
range
fixtures
{
ConvertSqlTimeColumnToEpochMs
(
fixtures
,
i
)
}
Convey
(
"When converting them should return epoch time with millisecond precision "
,
func
()
{
So
(
fixtures
[
0
]
.
(
int64
),
ShouldEqual
,
dt
.
Unix
()
*
1e3
)
So
(
fixtures
[
1
]
.
(
int64
),
ShouldEqual
,
dt
.
Unix
()
*
1e3
)
So
(
fixtures
[
2
],
ShouldBeNil
)
})
})
Convey
(
"Given row values with float64 as time columns"
,
func
()
{
tSeconds
:=
float64
(
dt
.
Unix
())
tMilliseconds
:=
float64
(
dt
.
UnixNano
()
/
1e6
)
tNanoSeconds
:=
float64
(
dt
.
UnixNano
())
var
nilPointer
*
float64
fixtures
:=
make
([]
interface
{},
7
)
fixtures
[
0
]
=
tSeconds
fixtures
[
1
]
=
&
tSeconds
fixtures
[
2
]
=
tMilliseconds
fixtures
[
3
]
=
&
tMilliseconds
fixtures
[
4
]
=
tNanoSeconds
fixtures
[
5
]
=
&
tNanoSeconds
fixtures
[
6
]
=
nilPointer
for
i
:=
range
fixtures
{
ConvertSqlTimeColumnToEpochMs
(
fixtures
,
i
)
}
Convey
(
"When converting them should return epoch time with millisecond precision "
,
func
()
{
So
(
fixtures
[
0
]
.
(
float64
),
ShouldEqual
,
tSeconds
*
1e3
)
So
(
fixtures
[
1
]
.
(
float64
),
ShouldEqual
,
tSeconds
*
1e3
)
So
(
fixtures
[
2
]
.
(
float64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
3
]
.
(
float64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
4
]
.
(
float64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
5
]
.
(
float64
),
ShouldEqual
,
tMilliseconds
)
So
(
fixtures
[
6
],
ShouldBeNil
)
})
})
Convey
(
"Given row values with float32 as time columns"
,
func
()
{
tSeconds
:=
float32
(
dt
.
Unix
())
var
nilInt
*
float32
fixtures
:=
make
([]
interface
{},
3
)
fixtures
[
0
]
=
tSeconds
fixtures
[
1
]
=
&
tSeconds
fixtures
[
2
]
=
nilInt
for
i
:=
range
fixtures
{
ConvertSqlTimeColumnToEpochMs
(
fixtures
,
i
)
}
Convey
(
"When converting them should return epoch time with millisecond precision "
,
func
()
{
So
(
fixtures
[
0
]
.
(
float64
),
ShouldEqual
,
float32
(
dt
.
Unix
()
*
1e3
))
So
(
fixtures
[
1
]
.
(
float64
),
ShouldEqual
,
float32
(
dt
.
Unix
()
*
1e3
))
So
(
fixtures
[
2
],
ShouldBeNil
)
})
})
})
})
})
})
...
...
pkg/tsdb/time_range.go
View file @
a314890f
...
@@ -96,5 +96,10 @@ func EpochPrecisionToMs(value float64) float64 {
...
@@ -96,5 +96,10 @@ func EpochPrecisionToMs(value float64) float64 {
return
float64
(
value
*
1e3
)
return
float64
(
value
*
1e3
)
}
}
s
:=
strconv
.
FormatFloat
(
value
,
'f'
,
-
1
,
64
)
if
len
(
s
)
==
19
{
return
float64
(
value
/
1e6
)
}
return
float64
(
value
)
return
float64
(
value
)
}
}
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