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
3cb0bc3d
Commit
3cb0bc3d
authored
Mar 20, 2018
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sql datasource: extract common logic for converting time column to epoch time in ms
parent
e5e9d3c2
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
84 additions
and
0 deletions
+84
-0
pkg/tsdb/sql_engine.go
+28
-0
pkg/tsdb/sql_engine_test.go
+46
-0
pkg/tsdb/time_range.go
+10
-0
No files found.
pkg/tsdb/sql_engine.go
View file @
3cb0bc3d
...
...
@@ -3,6 +3,7 @@ package tsdb
import
(
"context"
"sync"
"time"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
...
...
@@ -133,3 +134,30 @@ func (e *DefaultSqlEngine) Query(
return
result
,
nil
}
// ConvertTimeColumnToEpochMs converts column named time to unix timestamp in milliseconds
// to make native datetime types and epoch dates work in annotation and table queries.
func
ConvertSqlTimeColumnToEpochMs
(
values
RowValues
,
timeIndex
int
)
{
if
timeIndex
>=
0
{
switch
value
:=
values
[
timeIndex
]
.
(
type
)
{
case
time
.
Time
:
values
[
timeIndex
]
=
EpochPrecisionToMs
(
float64
(
value
.
Unix
()))
case
*
time
.
Time
:
if
value
!=
nil
{
values
[
timeIndex
]
=
EpochPrecisionToMs
(
float64
((
*
value
)
.
Unix
()))
}
case
int64
:
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
value
)))
case
*
int64
:
if
value
!=
nil
{
values
[
timeIndex
]
=
int64
(
EpochPrecisionToMs
(
float64
(
*
value
)))
}
case
float64
:
values
[
timeIndex
]
=
EpochPrecisionToMs
(
value
)
case
*
float64
:
if
value
!=
nil
{
values
[
timeIndex
]
=
EpochPrecisionToMs
(
*
value
)
}
}
}
}
pkg/tsdb/sql_engine_test.go
0 → 100644
View file @
3cb0bc3d
package
tsdb
import
(
"testing"
"time"
.
"github.com/smartystreets/goconvey/convey"
)
func
TestSqlEngine
(
t
*
testing
.
T
)
{
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
)
fixtures
:=
make
([]
interface
{},
8
)
fixtures
[
0
]
=
dt
fixtures
[
1
]
=
dt
.
Unix
()
*
1000
fixtures
[
2
]
=
dt
.
Unix
()
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
{
ConvertSqlTimeColumnToEpochMs
(
fixtures
,
i
)
}
Convey
(
"Should convert sql time columns to epoch time in ms "
,
func
()
{
expected
:=
float64
(
dt
.
Unix
()
*
1000
)
So
(
fixtures
[
0
]
.
(
float64
),
ShouldEqual
,
expected
)
So
(
fixtures
[
1
]
.
(
int64
),
ShouldEqual
,
expected
)
So
(
fixtures
[
2
]
.
(
int64
),
ShouldEqual
,
expected
)
So
(
fixtures
[
3
]
.
(
float64
),
ShouldEqual
,
expected
)
So
(
fixtures
[
4
]
.
(
float64
),
ShouldEqual
,
expected
)
So
(
fixtures
[
5
],
ShouldBeNil
)
So
(
fixtures
[
6
],
ShouldBeNil
)
So
(
fixtures
[
7
],
ShouldBeNil
)
})
})
})
}
pkg/tsdb/time_range.go
View file @
3cb0bc3d
...
...
@@ -88,3 +88,13 @@ func (tr *TimeRange) ParseTo() (time.Time, error) {
return
time
.
Time
{},
fmt
.
Errorf
(
"cannot parse to value %s"
,
tr
.
To
)
}
// EpochPrecisionToMs converts epoch precision to millisecond, if needed.
// Only seconds to milliseconds supported right now
func
EpochPrecisionToMs
(
value
float64
)
float64
{
if
int64
(
value
)
/
1e10
==
0
{
return
float64
(
value
*
1e3
)
}
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