Skip to content

Commit

Permalink
date Filter (grafana#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerwizard authored Oct 21, 2022
1 parent 10a833b commit 4f153c9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ WHERE $__timeFilter(date_time)
```

| Macro | Description | Output example |
| -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------- |
|----------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------|
| *$__timeFilter(columnName)* | Replaced by a conditional that filters the data (using the provided column) based on the time range of the panel in seconds | `time >= '1480001790' AND time <= '1482576232' )` |
| *$__dateFilter(columnName)* | Replaced by a conditional that filters the data (using the provided column) based on the date range of the panel | `date >= '2022-10-21' AND date <= '2022-10-23' )` |
| *$__timeFilter_ms(columnName)* | Replaced by a conditional that filters the data (using the provided column) based on the time range of the panel in milliseconds | `time >= '1480001790671' AND time <= '1482576232479' )` |
| *$__fromTime* | Replaced by the starting time of the range of the panel casted to DateTime | `toDateTime(intDiv(1415792726371,1000))` |
| *$__toTime* | Replaced by the ending time of the range of the panel casted to DateTime | `toDateTime(intDiv(1415792726371,1000))` |
Expand Down
13 changes: 13 additions & 0 deletions pkg/macros/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ func TimeFilter(query *sqlds.Query, args []string) (string, error) {
return fmt.Sprintf("%s >= '%d' AND %s <= '%d'", column, from, column, to), nil
}

func DateFilter(query *sqlds.Query, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args))
}
var (
column = args[0]
from = query.TimeRange.From.Format("2006-01-02")
to = query.TimeRange.To.Format("2006-01-02")
)

return fmt.Sprintf("%s >= '%s' AND %s <= '%s'", column, from, column, to), nil
}

func TimeFilterMs(query *sqlds.Query, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("%w: expected 1 argument, received %d", sqlds.ErrorBadArgumentCount, len(args))
Expand Down
14 changes: 14 additions & 0 deletions pkg/macros/macros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ func TestMacroToTimeFilter(t *testing.T) {
}
}

func TestMacroDateFilter(t *testing.T) {
from, _ := time.Parse("2006-01-02T15:04:05.000Z", "2014-11-12T11:45:26.371Z")
to, _ := time.Parse("2006-01-02T15:04:05.000Z", "2015-11-12T11:45:26.371Z")
query := sqlds.Query{
TimeRange: backend.TimeRange{
From: from,
To: to,
},
}
got, err := macros.DateFilter(&query, []string{"dateCol"})
assert.Nil(t, err)
assert.Equal(t, "dateCol >= '2014-11-12' AND dateCol <= '2015-11-12'", got)
}

func TestMacroTimeInterval(t *testing.T) {
query := sqlds.Query{
RawSQL: "select $__timeInterval(col) from foo",
Expand Down
1 change: 1 addition & 0 deletions pkg/plugin/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func (h *Clickhouse) Macros() sqlds.Macros {
"toTime": macros.ToTimeFilter,
"timeFilter_ms": macros.TimeFilterMs,
"timeFilter": macros.TimeFilter,
"dateFilter": macros.DateFilter,
"timeInterval": macros.TimeInterval,
"interval_s": macros.IntervalSeconds,
}
Expand Down

0 comments on commit 4f153c9

Please sign in to comment.