From a64bfdfb942fad840bb781a38e9a07a7088e9cf7 Mon Sep 17 00:00:00 2001 From: ismail simsek Date: Fri, 22 Sep 2023 17:40:18 +0200 Subject: [PATCH] InfluxDB SQL: Provide raw query preview for query history (#75030) * log the sql query * render the raw sql * Update rawQuery * unit tests --- pkg/tsdb/influxdb/fsql/fsql.go | 1 + .../plugins/datasource/influxdb/datasource.ts | 13 ++++-- .../datasource/influxdb/fsql/sqlUtil.test.ts | 32 +++++++++++++ .../datasource/influxdb/fsql/sqlUtil.ts | 45 +++++++++++++++++++ 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 public/app/plugins/datasource/influxdb/fsql/sqlUtil.test.ts create mode 100644 public/app/plugins/datasource/influxdb/fsql/sqlUtil.ts diff --git a/pkg/tsdb/influxdb/fsql/fsql.go b/pkg/tsdb/influxdb/fsql/fsql.go index b8f6d4c1b9e..b0c6dfd7317 100644 --- a/pkg/tsdb/influxdb/fsql/fsql.go +++ b/pkg/tsdb/influxdb/fsql/fsql.go @@ -50,6 +50,7 @@ func Query(ctx context.Context, dsInfo *models.DatasourceInfo, req backend.Query continue } + logger.Info(fmt.Sprintf("InfluxDB executing SQL: %s", qm.RawSQL)) info, err := r.client.Execute(ctx, qm.RawSQL) if err != nil { tRes.Responses[q.RefID] = backend.ErrDataResponse(backend.StatusInternal, fmt.Sprintf("flightsql: %s", err)) diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index 231c8a58419..0bf71a00113 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -36,6 +36,7 @@ import { getTemplateSrv, TemplateSrv } from 'app/features/templating/template_sr import { AnnotationEditor } from './components/editor/annotation/AnnotationEditor'; import { FluxQueryEditor } from './components/editor/query/flux/FluxQueryEditor'; import { BROWSER_MODE_DISABLED_MESSAGE } from './constants'; +import { toRawSql } from './fsql/sqlUtil'; import InfluxQueryModel from './influx_query_model'; import InfluxSeries from './influx_series'; import { buildMetadataQuery } from './influxql_query_builder'; @@ -185,10 +186,16 @@ export default class InfluxDatasource extends DataSourceWithBackend { + it('should render sql properly', () => { + const expected = 'SELECT host FROM iox.value1 LIMIT 50'; + const testQuery: SQLQuery = { + refId: 'A', + sql: { + limit: 50, + columns: [ + { + parameters: [ + { + name: 'host', + type: QueryEditorExpressionType.FunctionParameter, + }, + ], + type: QueryEditorExpressionType.Function, + }, + ], + }, + dataset: 'iox', + table: 'value1', + }; + const result = toRawSql(testQuery); + expect(result).toEqual(expected); + }); +}); diff --git a/public/app/plugins/datasource/influxdb/fsql/sqlUtil.ts b/public/app/plugins/datasource/influxdb/fsql/sqlUtil.ts new file mode 100644 index 00000000000..3b750cad827 --- /dev/null +++ b/public/app/plugins/datasource/influxdb/fsql/sqlUtil.ts @@ -0,0 +1,45 @@ +import { isEmpty } from 'lodash'; + +import { SQLQuery } from 'app/features/plugins/sql/types'; +import { createSelectClause, haveColumns } from 'app/features/plugins/sql/utils/sql.utils'; + +export function toRawSql({ sql, dataset, table }: SQLQuery): string { + let rawQuery = ''; + + // Return early with empty string if there is no sql column + if (!sql || !haveColumns(sql.columns)) { + return rawQuery; + } + + rawQuery += createSelectClause(sql.columns); + + if (dataset && table) { + rawQuery += `FROM ${dataset}.${table} `; + } + + if (sql.whereString) { + rawQuery += `WHERE ${sql.whereString} `; + } + + if (sql.groupBy?.[0]?.property.name) { + const groupBy = sql.groupBy.map((g) => g.property.name).filter((g) => !isEmpty(g)); + rawQuery += `GROUP BY ${groupBy.join(', ')} `; + } + + if (sql.orderBy?.property.name) { + rawQuery += `ORDER BY ${sql.orderBy.property.name} `; + } + + if (sql.orderBy?.property.name && sql.orderByDirection) { + rawQuery += `${sql.orderByDirection} `; + } + + // Although LIMIT 0 doesn't make sense, it is still possible to have LIMIT 0 + if (isLimit(sql.limit)) { + rawQuery += `LIMIT ${sql.limit}`; + } + + return rawQuery; +} + +const isLimit = (limit: number | undefined): boolean => limit !== undefined && limit >= 0;