InfluxDB SQL: Add time filter automatically (#75953)

* Use double quotes instead of backticks

* render time field as datetime

* add @react-awesome-query-builder/core

* Add time range filtering

* Update test

* Update macro test
This commit is contained in:
ismail simsek 2023-10-04 17:00:24 +02:00 committed by GitHub
parent 193ec8de2b
commit 2d603bed22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 14 additions and 22 deletions

View File

@ -273,6 +273,7 @@
"@react-aria/menu": "3.10.0",
"@react-aria/overlays": "3.15.0",
"@react-aria/utils": "3.18.0",
"@react-awesome-query-builder/core": "6.4.1",
"@react-awesome-query-builder/ui": "6.4.1",
"@react-stately/collections": "3.9.0",
"@react-stately/menu": "3.5.3",

View File

@ -15,13 +15,10 @@ var macros = sqlutil.Macros{
"timeGroupAlias": macroTimeGroupAlias,
// The behaviors of timeFrom and timeTo as defined in the SDK are different
// from all other Grafana SQL plugins. Instead we'll take their
// implementations, rename them and define timeFrom and timeTo ourselves.
"timeRangeFrom": sqlutil.DefaultMacros["timeFrom"],
"timeRangeTo": sqlutil.DefaultMacros["timeTo"],
"timeRange": sqlutil.DefaultMacros["timeFilter"],
"timeTo": macroTo,
"timeFrom": macroFrom,
// from all other Grafana SQL plugins. Instead we'll take the implementations,
// rename them and define timeFrom and timeTo ourselves.
"timeTo": macroTo,
"timeFrom": macroFrom,
}
func macroTimeGroup(query *sqlutil.Query, args []string) (string, error) {
@ -84,10 +81,12 @@ func macroInterval(query *sqlutil.Query, _ []string) (string, error) {
return fmt.Sprintf("interval '%d second'", int64(query.Interval.Seconds())), nil
}
// https://docs.influxdata.com/influxdb/cloud-serverless/query-data/sql/cast-types/?t=CAST%28%29#cast-to-a-timestamp-type
func macroFrom(query *sqlutil.Query, _ []string) (string, error) {
return fmt.Sprintf("cast('%s' as timestamp)", query.TimeRange.From.Format(time.RFC3339)), nil
}
// https://docs.influxdata.com/influxdb/cloud-serverless/query-data/sql/cast-types/?t=CAST%28%29#cast-to-a-timestamp-type
func macroTo(query *sqlutil.Query, _ []string) (string, error) {
return fmt.Sprintf("cast('%s' as timestamp)", query.TimeRange.To.Format(time.RFC3339)), nil
}

View File

@ -44,18 +44,6 @@ func TestMacros(t *testing.T) {
in: `select * from x where $__timeFilter(time)`,
out: `select * from x where time >= '2023-01-01T00:00:00Z' AND time <= '2023-01-01T00:10:00Z'`,
},
{
in: `select * from x where $__timeRangeFrom(time)`,
out: `select * from x where time >= '2023-01-01T00:00:00Z'`,
},
{
in: `select * from x where $__timeRangeTo(time)`,
out: `select * from x where time <= '2023-01-01T00:10:00Z'`,
},
{
in: `select * from x where $__timeRange(time)`,
out: `select * from x where time >= '2023-01-01T00:00:00Z' AND time <= '2023-01-01T00:10:00Z'`,
},
{
in: `select * from x where time >= $__timeFrom`,
out: `select * from x where time >= cast('2023-01-01T00:00:00Z' as timestamp)`,

View File

@ -29,6 +29,7 @@ export function mapFieldsToTypes(columns: SQLSelectableValue[]) {
type = 'date';
break;
}
case 'TIMESTAMP(NANOSECOND, NONE)':
case 'DATETIME': {
type = 'datetime';
break;

View File

@ -6,7 +6,7 @@ import { toRawSql } from './sqlUtil';
describe('toRawSql', () => {
it('should render sql properly', () => {
const expected = 'SELECT host FROM iox.value1 LIMIT 50';
const expected = 'SELECT host FROM iox.value1 WHERE time >= $__timeFrom AND time <= $__timeTo LIMIT 50';
const testQuery: SQLQuery = {
refId: 'A',
sql: {

View File

@ -32,8 +32,10 @@ export function toRawSql({ sql, dataset, table }: SQLQuery): string {
rawQuery += `FROM ${dataset}.${table} `;
}
// $__timeFrom and $__timeTo will be interpolated on the backend
rawQuery += `WHERE time >= $__timeFrom AND time <= $__timeTo `;
if (sql.whereString) {
rawQuery += `WHERE ${sql.whereString} `;
rawQuery += `AND ${sql.whereString} `;
}
if (sql.groupBy?.[0]?.property.name) {

View File

@ -6641,7 +6641,7 @@ __metadata:
languageName: node
linkType: hard
"@react-awesome-query-builder/core@npm:^6.4.1":
"@react-awesome-query-builder/core@npm:6.4.1, @react-awesome-query-builder/core@npm:^6.4.1":
version: 6.4.1
resolution: "@react-awesome-query-builder/core@npm:6.4.1"
dependencies:
@ -17670,6 +17670,7 @@ __metadata:
"@react-aria/menu": 3.10.0
"@react-aria/overlays": 3.15.0
"@react-aria/utils": 3.18.0
"@react-awesome-query-builder/core": 6.4.1
"@react-awesome-query-builder/ui": 6.4.1
"@react-stately/collections": 3.9.0
"@react-stately/menu": 3.5.3