InfluxDB SQL: Provide raw query preview for query history (#75030)

* log the sql query

* render the raw sql

* Update rawQuery

* unit tests
pull/75311/head
ismail simsek 2 years ago committed by GitHub
parent d076f733e9
commit a64bfdfb94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      pkg/tsdb/influxdb/fsql/fsql.go
  2. 13
      public/app/plugins/datasource/influxdb/datasource.ts
  3. 32
      public/app/plugins/datasource/influxdb/fsql/sqlUtil.test.ts
  4. 45
      public/app/plugins/datasource/influxdb/fsql/sqlUtil.ts

@ -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))

@ -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<InfluxQuery,
}
getQueryDisplayText(query: InfluxQuery) {
if (this.version === InfluxVersion.Flux) {
return query.query;
switch (this.version) {
case InfluxVersion.Flux:
return query.query;
case InfluxVersion.SQL:
return toRawSql(query);
case InfluxVersion.InfluxQL:
return new InfluxQueryModel(query).render(false);
default:
return '';
}
return new InfluxQueryModel(query).render(false);
}
/**

@ -0,0 +1,32 @@
import { SQLQuery } from 'app/features/plugins/sql/types';
import { QueryEditorExpressionType } from '../../../../features/plugins/sql/expressions';
import { toRawSql } from './sqlUtil';
describe('toRawSql', () => {
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);
});
});

@ -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;
Loading…
Cancel
Save