From a8ac215039fe721aba0c9177b349a7d6b49be003 Mon Sep 17 00:00:00 2001 From: Brad Lhotsky Date: Thu, 8 Jun 2017 14:36:05 -0700 Subject: [PATCH] Add a few more MySQL macros: * $__timeFrom() -> Returns the dashboard 'from' suitable for use querying against a MySQL TIMESTAMP field. * $__timeTo() -> Returns the dashboard 'to' suitable for use querying against a MySQL TIMESTAMP field. * $__unixEpochFiler(column) -> If you store timestamps as UNIX epoch's, this builds: column > 'from' AND column < 'to' * $__unixEpochFrom() -> Returns the dashboard 'from' suitable for use querying against a MySQL integer field (UNIX epochs) * $__unixEpochTo() -> Returns the dashboard 'to' suitable for use querying against a MySQL integer field (UNIX epochs) --- pkg/tsdb/mysql/macros.go | 13 +++++++++++++ .../mysql/partials/annotations.editor.html | 9 ++++++++- .../datasource/mysql/partials/query.editor.html | 9 ++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/pkg/tsdb/mysql/macros.go b/pkg/tsdb/mysql/macros.go index 34bbfdc6865..5545168afbb 100644 --- a/pkg/tsdb/mysql/macros.go +++ b/pkg/tsdb/mysql/macros.go @@ -74,6 +74,19 @@ func (m *MySqlMacroEngine) EvaluateMacro(name string, args []string) (string, er return "", fmt.Errorf("missing time column argument for macro %v", name) } return fmt.Sprintf("%s >= FROM_UNIXTIME(%d) AND %s <= FROM_UNIXTIME(%d)", args[0], uint64(m.TimeRange.GetFromAsMsEpoch()/1000), args[0], uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil + case "__timeFrom": + return fmt.Sprintf("FROM_UNIXTIME(%d)", uint64(m.TimeRange.GetFromAsMsEpoch()/1000)), nil + case "__timeTo": + return fmt.Sprintf("FROM_UNIXTIME(%d)", uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil + case "__unixEpochFilter": + if len(args) == 0 { + return "", fmt.Errorf("missing time column argument for macro %v", name) + } + return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], uint64(m.TimeRange.GetFromAsMsEpoch()/1000), args[0], uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil + case "__unixEpochFrom": + return fmt.Sprintf("%d", uint64(m.TimeRange.GetFromAsMsEpoch()/1000)), nil + case "__unixEpochTo": + return fmt.Sprintf("%d", uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil default: return "", fmt.Errorf("Unknown macro %v", name) } diff --git a/public/app/plugins/datasource/mysql/partials/annotations.editor.html b/public/app/plugins/datasource/mysql/partials/annotations.editor.html index 2f0bfd936a5..09581e2a552 100644 --- a/public/app/plugins/datasource/mysql/partials/annotations.editor.html +++ b/public/app/plugins/datasource/mysql/partials/annotations.editor.html @@ -28,7 +28,14 @@ An annotation is an event that is overlayed on top of graphs. The query can have Macros: - $__time(column) -> UNIX_TIMESTAMP(column) as time_sec -- $__timeFilter(column) -> UNIX_TIMESTAMP(time_date_time) > from AND UNIX_TIMESTAMP(time_date_time) < 1492750877 +- $__timeFilter(column) -> UNIX_TIMESTAMP(time_date_time) > 1492750877 AND UNIX_TIMESTAMP(time_date_time) < 1492750877 +- $__unixEpochFilter(column) -> time_unix_epoch > 1492750877 AND time_unix_epoch < 1492750877 + +Or build your own conditionals using these macros which just return the values: +- $__timeFrom() -> FROM_UNIXTIME(1492750877) +- $__timeTo() -> FROM_UNIXTIME(1492750877) +- $__unixEpochFrom() -> 1492750877 +- $__unixEpochTo() -> 1492750877 diff --git a/public/app/plugins/datasource/mysql/partials/query.editor.html b/public/app/plugins/datasource/mysql/partials/query.editor.html index c7b90ad7815..32c3b097be3 100644 --- a/public/app/plugins/datasource/mysql/partials/query.editor.html +++ b/public/app/plugins/datasource/mysql/partials/query.editor.html @@ -46,7 +46,14 @@ Table: Macros: - $__time(column) -> UNIX_TIMESTAMP(column) as time_sec -- $__timeFilter(column) -> UNIX_TIMESTAMP(time_date_time) ≥ from AND UNIX_TIMESTAMP(time_date_time) ≤ 1492750877 +- $__timeFilter(column) -> UNIX_TIMESTAMP(time_date_time) ≥ 1492750877 AND UNIX_TIMESTAMP(time_date_time) ≤ 1492750877 +- $__unixEpochFilter(column) -> time_unix_epoch > 1492750877 AND time_unix_epoch < 1492750877 + +Or build your own conditionals using these macros which just return the values: +- $__timeFrom() -> FROM_UNIXTIME(1492750877) +- $__timeTo() -> FROM_UNIXTIME(1492750877) +- $__unixEpochFrom() -> 1492750877 +- $__unixEpochTo() -> 1492750877