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 { SelectableValue } from '@grafana/data';
import { Select } from '@grafana/ui'; import { Select } from '@grafana/ui';
import { DB, ResourceSelectorProps, toOption } from '../types'; import { DB, ResourceSelectorProps, SQLDialect, toOption } from '../types';
import { isSqlDatasourceDatabaseSelectionFeatureFlagEnabled } from './QueryEditorFeatureFlag.utils'; import { isSqlDatasourceDatabaseSelectionFeatureFlagEnabled } from './QueryEditorFeatureFlag.utils';
@ -12,17 +12,11 @@ export interface DatasetSelectorProps extends ResourceSelectorProps {
db: DB; db: DB;
dataset: string | undefined; dataset: string | undefined;
preconfiguredDataset: string; preconfiguredDataset: string;
isPostgresInstance: boolean | undefined; dialect: SQLDialect;
onChange: (v: SelectableValue) => void; onChange: (v: SelectableValue) => void;
} }
export const DatasetSelector = ({ export const DatasetSelector = ({ dataset, db, dialect, onChange, preconfiguredDataset }: DatasetSelectorProps) => {
dataset,
db,
isPostgresInstance,
onChange,
preconfiguredDataset,
}: DatasetSelectorProps) => {
/* /*
The behavior of this component - for MSSQL and MySQL datasources - is based on whether the user chose to create a datasource 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 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, // `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. // 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 () => { const state = useAsync(async () => {
if (isSqlDatasourceDatabaseSelectionFeatureFlagEnabled()) { if (isSqlDatasourceDatabaseSelectionFeatureFlagEnabled()) {

View File

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

View File

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

View File

@ -30,7 +30,7 @@ describe('DatasetSelector', () => {
}); });
it('should not query the database if Postgres instance, and no preconfigured database', async () => { 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} />); render(<DatasetSelector {...mockProps} />);
await waitFor(() => { await waitFor(() => {

View File

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

View File

@ -168,3 +168,5 @@ export interface MetaDefinition {
completion?: string; completion?: string;
kind: CompletionItemKind; 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 { SqlQueryEditor } from 'app/features/plugins/sql/components/QueryEditor';
import { SQLOptions, SQLQuery } from 'app/features/plugins/sql/types'; import { SQLOptions, SQLQuery } from 'app/features/plugins/sql/types';
import { QueryHeaderProps } from '../../../features/plugins/sql/components/QueryHeader';
import { PostgresDatasource } from './datasource'; import { PostgresDatasource } from './datasource';
const queryHeaderProps = { isPostgresInstance: true }; const queryHeaderProps: Pick<QueryHeaderProps, 'dialect'> = { dialect: 'postgres' };
export function PostgresQueryEditor(props: QueryEditorProps<PostgresDatasource, SQLQuery, SQLOptions>) { export function PostgresQueryEditor(props: QueryEditorProps<PostgresDatasource, SQLQuery, SQLOptions>) {
return <SqlQueryEditor {...props} queryHeaderProps={queryHeaderProps} />; return <SqlQueryEditor {...props} queryHeaderProps={queryHeaderProps} />;

View File

@ -61,6 +61,7 @@ class UnthemedSQLQueryEditor extends PureComponent<Props> {
const defaultQuery = applyQueryDefaults(query); const defaultQuery = applyQueryDefaults(query);
return { return {
...defaultQuery, ...defaultQuery,
dataset: 'iox',
sql: { sql: {
...defaultQuery.sql, ...defaultQuery.sql,
limit: undefined, limit: undefined,
@ -98,6 +99,7 @@ class UnthemedSQLQueryEditor extends PureComponent<Props> {
query={this.transformQuery(query)} query={this.transformQuery(query)}
onRunQuery={onRunSQLQuery} onRunQuery={onRunSQLQuery}
onChange={onSQLChange} onChange={onSQLChange}
queryHeaderProps={{ dialect: 'influx' }}
/> />
<div className={cx('gf-form-inline', styles.editorActions)}> <div className={cx('gf-form-inline', styles.editorActions)}>
<LinkButton <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 { formatSQL } from 'app/features/plugins/sql/utils/formatSQL';
import { mapFieldsToTypes } from './fields'; import { mapFieldsToTypes } from './fields';
import { buildColumnQuery, buildTableQuery, showDatabases } from './flightsqlMetaQuery'; import { buildColumnQuery, buildTableQuery } from './flightsqlMetaQuery';
import { getSqlCompletionProvider } from './sqlCompletionProvider'; import { getSqlCompletionProvider } from './sqlCompletionProvider';
import { quoteLiteral, quoteIdentifierIfNecessary, toRawSql } from './sqlUtil'; import { quoteLiteral, quoteIdentifierIfNecessary, toRawSql } from './sqlUtil';
import { FlightSQLOptions } from './types'; import { FlightSQLOptions } from './types';
@ -42,8 +42,7 @@ export class FlightSQLDatasource extends SqlDatasource {
} }
async fetchDatasets(): Promise<string[]> { async fetchDatasets(): Promise<string[]> {
const datasets = await this.runSql<string[]>(showDatabases(), { refId: 'datasets' }); return Promise.resolve(['iox']);
return datasets.map((t) => quoteIdentifierIfNecessary(t[0]));
} }
async fetchTables(dataset?: string): Promise<string[]> { async fetchTables(dataset?: string): Promise<string[]> {