Skip to content

Commit

Permalink
use timeInterval macro on timeseries date (#74)
Browse files Browse the repository at this point in the history
* use timeInterval macro on timeseries date
  • Loading branch information
scottlepp authored Feb 10, 2022
1 parent c6c22bf commit f7b8d90
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.12.0

Feature - Time series builder: use $__timeInterval macro on time field so buckets can be adjusted from query options.

## 0.11.0

Feature - Time series: Hide fields, use group by in select, use time field in group by
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clickhouse-datasource",
"version": "0.11.0",
"version": "0.12.0",
"description": "Clickhouse Datasource",
"scripts": {
"build": "grafana-toolkit plugin:build",
Expand Down
2 changes: 1 addition & 1 deletion pkg/macros/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TimeInterval(query *sqlds.Query, args []string) (string, error) {
}

seconds := query.Interval.Seconds()
return fmt.Sprintf("toStartOfInterval(%s, INTERVAL %d second)", args[0], int(seconds)), nil
return fmt.Sprintf("toStartOfInterval(toDateTime(%s), INTERVAL %d second)", args[0], int(seconds)), nil
}

func IntervalSeconds(query *sqlds.Query, args []string) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/macros/macros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestMacroTimeInterval(t *testing.T) {
}
got, err := macros.TimeInterval(&query, []string{"col"})
assert.Nil(t, err)
assert.Equal(t, "toStartOfInterval(col, INTERVAL 20 second)", got)
assert.Equal(t, "toStartOfInterval(toDateTime(col), INTERVAL 20 second)", got)
}

func TestMacroIntervalSeconds(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions src/components/queryBuilder/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,14 @@ const getTrendByQuery = (
return `${m.aggregation}(${m.field})${alias}`;
})
.join(', ');
const time = `$__timeInterval(${timeField})`;
if (metricsQuery !== '') {
const group = groupBy.length > 0 ? `${groupBy.join(', ')},` : '';
metricsQuery = `${timeField}, ${group} ${metricsQuery}`;
metricsQuery = `${time}, ${group} ${metricsQuery}`;
} else if (groupBy.length > 0) {
metricsQuery = `${timeField}, ${groupBy.join(', ')}`;
metricsQuery = `${time}, ${groupBy.join(', ')}`;
} else {
metricsQuery = `${timeField}`;
metricsQuery = `${time}`;
}

const sep = database === '' || table === '' ? '' : '.';
Expand Down

0 comments on commit f7b8d90

Please sign in to comment.