Skip to content

Commit

Permalink
Adds an option to specify timezone in timeGroup macro (#141)
Browse files Browse the repository at this point in the history
* add timezone to timegroup
* fix formatting

---------

Co-authored-by: Zygimantas Koncius <[email protected]>
Co-authored-by: Zygimantas Koncius <[email protected]>
  • Loading branch information
3 people authored Feb 25, 2025
1 parent c744eb8 commit 7ffd31c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Macro example | Description
`$__timeGroup(dateColumn,'5m', 0)` | Same as above but with a fill parameter so missing points in that series will be added by grafana and 0 will be used as value.
`$__timeGroup(dateColumn,'5m', NULL)` | Same as above but NULL will be used as value for missing points.
`$__timeGroup(dateColumn,'5m', previous)` | Same as above but the previous value in that series will be used as fill value if no value has been seen yet NULL will be used (only available in Grafana 5.3+).
`$__timeGroup(dateColumn, interval, fill, timezone)` | Same as above but with a timezone parameter, which will be used to convert the timestamp in dateColumn to the specified timezone before slicing.
`$__timeGroupAlias(dateColumn,'5m')` | Will be replaced identical to $__timeGroup but with an added column alias `time` (only available in Grafana 5.3+).
`$__unixEpochFilter(dateColumn)` | Will be replaced by a time range filter using the specified column name with times represented as Unix timestamp. For example, *dateColumn > 1494410783 AND dateColumn < 1494497183*
`$__unixEpochNanoFilter(dateColumn)` | Will be replaced by a time range filter using the specified column name with times represented as nanosecond timestamp. For example, *dateColumn > 1494410783152415214 AND dateColumn < 1494497183142514872*
Expand Down
9 changes: 7 additions & 2 deletions pkg/query/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,18 @@ func handleTimeGroupMacro(args []string, configStruct *data.QueryConfigStruct, n
if err != nil {
return "", fmt.Errorf("error parsing interval %v", args[1])
}
if len(args) == 3 {
if len(args) > 2 {
err := SetupFillmode(configStruct, args[2])
if err != nil {
return "", err
}
}

timeExpr := fmt.Sprintf("TO_TIMESTAMP_NTZ(%s)", args[0])
if len(args) > 3 {
timeExpr = fmt.Sprintf("TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE(%s, %s))", args[3], args[0])
}

duration := interval.Seconds()
timeUnit := "SECOND"

Expand All @@ -203,7 +208,7 @@ func handleTimeGroupMacro(args []string, configStruct *data.QueryConfigStruct, n
timeUnit = "WEEK"
}

return fmt.Sprintf("TIME_SLICE(TO_TIMESTAMP_NTZ(%s), %v, '%s', 'START')", args[0], math.Max(1, duration), timeUnit), nil
return fmt.Sprintf("TIME_SLICE(%s, %v, '%s', 'START')", timeExpr, math.Max(1, duration), timeUnit), nil
}

func handleTimeGroupAliasMacro(args []string, configStruct *data.QueryConfigStruct) (string, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/query/macros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func TestEvaluateMacro(t *testing.T) {
{name: "__timeGroup", args: []string{"col", "1d", "12"}, response: "TIME_SLICE(TO_TIMESTAMP_NTZ(col), 86400, 'SECOND', 'START')", fillMode: ValueFill, fillValue: 12},
{name: "__timeGroup", args: []string{"col", "7d"}, response: "TIME_SLICE(TO_TIMESTAMP_NTZ(col), 1, 'WEEK', 'START')"},
{name: "__timeGroup", args: []string{"col", "2w"}, response: "TIME_SLICE(TO_TIMESTAMP_NTZ(col), 2, 'WEEK', 'START')"},
{name: "__timeGroup", args: []string{"col", "1d", "NULL", "'America/Los_Angeles'"}, response: "TIME_SLICE(TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE('America/Los_Angeles', col)), 86400, 'SECOND', 'START')", fillMode: NullFill},
// __timeGroupAlias
{name: "__timeGroupAlias", args: []string{}, err: "macro __timeGroup needs time column and interval and optional fill value"},
{name: "__timeGroupAlias", args: []string{"col", "xxxx"}, err: "error parsing interval xxxx"},
Expand Down

0 comments on commit 7ffd31c

Please sign in to comment.