InfluxDB SQL: Don't show dataset dropdown (#78553)

* Don't show dataset dropdown

* Default dataset is iox

* introduce sql dialect
This commit is contained in:
ismail simsek 2023-11-27 15:23:54 +01:00 committed by GitHub
parent f7bf818527
commit 5eae18c166
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 27 additions and 25 deletions

View File

@ -4,7 +4,7 @@ import { useAsync } from 'react-use';
import { SelectableValue } from '@grafana/data';
import { Select } from '@grafana/ui';
import { DB, ResourceSelectorProps, toOption } from '../types';
import { DB, ResourceSelectorProps, SQLDialect, toOption } from '../types';
import { isSqlDatasourceDatabaseSelectionFeatureFlagEnabled } from './QueryEditorFeatureFlag.utils';
@ -12,17 +12,11 @@ export interface DatasetSelectorProps extends ResourceSelectorProps {
db: DB;
dataset: string | undefined;
preconfiguredDataset: string;
isPostgresInstance: boolean | undefined;
dialect: SQLDialect;
onChange: (v: SelectableValue) => void;
}
export const DatasetSelector = ({
dataset,
db,
isPostgresInstance,
onChange,
preconfiguredDataset,
}: DatasetSelectorProps) => {
export const DatasetSelector = ({ dataset, db, dialect, onChange, preconfiguredDataset }: DatasetSelectorProps) => {
/*
The behavior of this component - for MSSQL and MySQL datasources - is based on whether the user chose to create a datasource
with or without a default database (preconfiguredDataset). If the user configured a default database, this selector
@ -31,7 +25,7 @@ export const DatasetSelector = ({
*/
// `hasPreconfigCondition` is true if either 1) the sql datasource has a preconfigured default database,
// OR if 2) the datasource is Postgres. In either case the only option available to the user is the preconfigured database.
const hasPreconfigCondition = !!preconfiguredDataset || isPostgresInstance;
const hasPreconfigCondition = !!preconfiguredDataset || dialect === 'postgres';
const state = useAsync(async () => {
if (isSqlDatasourceDatabaseSelectionFeatureFlagEnabled()) {

View File

@ -14,7 +14,7 @@ import { RawEditor } from './query-editor-raw/RawEditor';
import { VisualEditor } from './visual-query-builder/VisualEditor';
interface SqlQueryEditorProps extends QueryEditorProps<SqlDatasource, SQLQuery, SQLOptions> {
queryHeaderProps?: Pick<QueryHeaderProps, 'isPostgresInstance'>;
queryHeaderProps?: Pick<QueryHeaderProps, 'dialect'>;
}
export function SqlQueryEditor({
@ -29,7 +29,7 @@ export function SqlQueryEditor({
const db = datasource.getDB();
const { preconfiguredDatabase } = datasource;
const isPostgresInstance = !!queryHeaderProps?.isPostgresInstance;
const dialect = queryHeaderProps?.dialect ?? 'other';
const { loading, error } = useAsync(async () => {
return () => {
if (datasource.getDB(datasource.id).init !== undefined) {
@ -97,7 +97,7 @@ export function SqlQueryEditor({
queryRowFilter={queryRowFilter}
query={queryWithDefaults}
isQueryRunnable={isQueryRunnable}
isPostgresInstance={isPostgresInstance}
dialect={dialect}
/>
<Space v={0.5} />

View File

@ -8,7 +8,7 @@ import { reportInteraction } from '@grafana/runtime';
import { Button, InlineSwitch, RadioButtonGroup, Tooltip } from '@grafana/ui';
import { QueryWithDefaults } from '../defaults';
import { SQLQuery, QueryFormat, QueryRowFilter, QUERY_FORMAT_OPTIONS, DB } from '../types';
import { SQLQuery, QueryFormat, QueryRowFilter, QUERY_FORMAT_OPTIONS, DB, SQLDialect } from '../types';
import { ConfirmModal } from './ConfirmModal';
import { DatasetSelector } from './DatasetSelector';
@ -17,7 +17,7 @@ import { TableSelector } from './TableSelector';
export interface QueryHeaderProps {
db: DB;
isPostgresInstance?: boolean;
dialect: SQLDialect;
isQueryRunnable: boolean;
onChange: (query: SQLQuery) => void;
onQueryRowChange: (queryRowFilter: QueryRowFilter) => void;
@ -34,7 +34,7 @@ const editorModes = [
export function QueryHeader({
db,
isPostgresInstance,
dialect,
isQueryRunnable,
onChange,
onQueryRowChange,
@ -108,9 +108,12 @@ export function QueryHeader({
};
const datasetDropdownIsAvailable = () => {
// If the feature flag is DISABLED, && the datasource is Postgres (`isPostgresInstance`),
if (dialect === 'influx') {
return false;
}
// If the feature flag is DISABLED, && the datasource is Postgres (`dialect = 'postgres`),
// we want to hide the dropdown - as per previous behavior.
if (!isSqlDatasourceDatabaseSelectionFeatureFlagEnabled() && isPostgresInstance) {
if (!isSqlDatasourceDatabaseSelectionFeatureFlagEnabled() && dialect === 'postgres') {
return false;
}
@ -290,7 +293,7 @@ export function QueryHeader({
<DatasetSelector
db={db}
dataset={query.dataset}
isPostgresInstance={isPostgresInstance}
dialect={dialect}
preconfiguredDataset={preconfiguredDataset}
onChange={onDatasetChange}
/>

View File

@ -30,7 +30,7 @@ describe('DatasetSelector', () => {
});
it('should not query the database if Postgres instance, and no preconfigured database', async () => {
const mockProps = buildMockDatasetSelectorProps({ isPostgresInstance: true });
const mockProps = buildMockDatasetSelectorProps({ dialect: 'postgres' });
render(<DatasetSelector {...mockProps} />);
await waitFor(() => {

View File

@ -76,7 +76,7 @@ export function buildMockDatasetSelectorProps(overrides?: Partial<DatasetSelecto
return {
db: buildMockDB(),
dataset: '',
isPostgresInstance: false,
dialect: 'other',
onChange: jest.fn(),
preconfiguredDataset: '',
...overrides,

View File

@ -168,3 +168,5 @@ export interface MetaDefinition {
completion?: string;
kind: CompletionItemKind;
}
export type SQLDialect = 'postgres' | 'influx' | 'other';

View File

@ -4,9 +4,11 @@ import { QueryEditorProps } from '@grafana/data';
import { SqlQueryEditor } from 'app/features/plugins/sql/components/QueryEditor';
import { SQLOptions, SQLQuery } from 'app/features/plugins/sql/types';
import { QueryHeaderProps } from '../../../features/plugins/sql/components/QueryHeader';
import { PostgresDatasource } from './datasource';
const queryHeaderProps = { isPostgresInstance: true };
const queryHeaderProps: Pick<QueryHeaderProps, 'dialect'> = { dialect: 'postgres' };
export function PostgresQueryEditor(props: QueryEditorProps<PostgresDatasource, SQLQuery, SQLOptions>) {
return <SqlQueryEditor {...props} queryHeaderProps={queryHeaderProps} />;

View File

@ -61,6 +61,7 @@ class UnthemedSQLQueryEditor extends PureComponent<Props> {
const defaultQuery = applyQueryDefaults(query);
return {
...defaultQuery,
dataset: 'iox',
sql: {
...defaultQuery.sql,
limit: undefined,
@ -98,6 +99,7 @@ class UnthemedSQLQueryEditor extends PureComponent<Props> {
query={this.transformQuery(query)}
onRunQuery={onRunSQLQuery}
onChange={onSQLChange}
queryHeaderProps={{ dialect: 'influx' }}
/>
<div className={cx('gf-form-inline', styles.editorActions)}>
<LinkButton

View File

@ -6,7 +6,7 @@ import { DB, SQLQuery } from 'app/features/plugins/sql/types';
import { formatSQL } from 'app/features/plugins/sql/utils/formatSQL';
import { mapFieldsToTypes } from './fields';
import { buildColumnQuery, buildTableQuery, showDatabases } from './flightsqlMetaQuery';
import { buildColumnQuery, buildTableQuery } from './flightsqlMetaQuery';
import { getSqlCompletionProvider } from './sqlCompletionProvider';
import { quoteLiteral, quoteIdentifierIfNecessary, toRawSql } from './sqlUtil';
import { FlightSQLOptions } from './types';
@ -42,8 +42,7 @@ export class FlightSQLDatasource extends SqlDatasource {
}
async fetchDatasets(): Promise<string[]> {
const datasets = await this.runSql<string[]>(showDatabases(), { refId: 'datasets' });
return datasets.map((t) => quoteIdentifierIfNecessary(t[0]));
return Promise.resolve(['iox']);
}
async fetchTables(dataset?: string): Promise<string[]> {