diff --git a/public/app/features/dashboard/panel_editor/QueriesTab.tsx b/public/app/features/dashboard/panel_editor/QueriesTab.tsx index 9aacaf6cfb6..cbb374c3682 100644 --- a/public/app/features/dashboard/panel_editor/QueriesTab.tsx +++ b/public/app/features/dashboard/panel_editor/QueriesTab.tsx @@ -4,7 +4,7 @@ import React, { PureComponent } from 'react'; import { DataSourcePicker } from 'app/core/components/Select/DataSourcePicker'; import { QueryOptions } from './QueryOptions'; import { Button, CustomScrollbar, HorizontalGroup, Modal, stylesFactory } from '@grafana/ui'; -import { getLocationSrv } from '@grafana/runtime'; +import { getLocationSrv, getDataSourceSrv } from '@grafana/runtime'; import { QueryEditorRows } from './QueryEditorRows'; // Services import { getDatasourceSrv } from 'app/features/plugins/datasource_srv'; @@ -13,7 +13,14 @@ import config from 'app/core/config'; // Types import { PanelModel } from '../state/PanelModel'; import { DashboardModel } from '../state/DashboardModel'; -import { DataQuery, DataSourceSelectItem, DefaultTimeRange, LoadingState, PanelData } from '@grafana/data'; +import { + DataQuery, + DataSourceSelectItem, + DefaultTimeRange, + LoadingState, + PanelData, + DataSourceApi, +} from '@grafana/data'; import { PluginHelp } from 'app/core/components/PluginHelp/PluginHelp'; import { addQuery } from 'app/core/utils/query'; import { Unsubscribable } from 'rxjs'; @@ -28,7 +35,8 @@ interface Props { } interface State { - currentDS: DataSourceSelectItem; + dataSource?: DataSourceApi; + dataSourceItem: DataSourceSelectItem; helpContent: JSX.Element; isLoadingHelp: boolean; isPickerOpen: boolean; @@ -45,7 +53,7 @@ export class QueriesTab extends PureComponent { state: State = { isLoadingHelp: false, - currentDS: this.findCurrentDataSource(), + dataSourceItem: this.findCurrentDataSource(), helpContent: null, isPickerOpen: false, isAddingMixed: false, @@ -58,13 +66,16 @@ export class QueriesTab extends PureComponent { }, }; - componentDidMount() { + async componentDidMount() { const { panel } = this.props; const queryRunner = panel.getQueryRunner(); this.querySubscription = queryRunner.getData(false).subscribe({ next: (data: PanelData) => this.onPanelDataUpdate(data), }); + + const ds = await getDataSourceSrv().get(panel.datasource); + this.setState({ dataSource: ds }); } componentWillUnmount() { @@ -83,12 +94,12 @@ export class QueriesTab extends PureComponent { return this.datasources.find(datasource => datasource.value === panel.datasource) || this.datasources[0]; } - onChangeDataSource = (datasource: DataSourceSelectItem) => { + onChangeDataSource = async (newDsItem: DataSourceSelectItem) => { const { panel } = this.props; - const { currentDS } = this.state; + const { dataSourceItem } = this.state; // switching to mixed - if (datasource.meta.mixed) { + if (newDsItem.meta.mixed) { // Set the datasource on all targets panel.targets.forEach(target => { if (target.datasource !== ExpressionDatasourceID) { @@ -98,27 +109,32 @@ export class QueriesTab extends PureComponent { } } }); - } else if (currentDS) { + } else if (dataSourceItem) { // if switching from mixed - if (currentDS.meta.mixed) { + if (dataSourceItem.meta.mixed) { // Remove the explicit datasource for (const target of panel.targets) { if (target.datasource !== ExpressionDatasourceID) { delete target.datasource; } } - } else if (currentDS.meta.id !== datasource.meta.id) { + } else if (dataSourceItem.meta.id !== newDsItem.meta.id) { // we are changing data source type, clear queries panel.targets = [{ refId: 'A' }]; } } - panel.datasource = datasource.value; - panel.refresh(); + const dataSource = await getDataSourceSrv().get(newDsItem.value); - this.setState({ - currentDS: datasource, - }); + panel.datasource = newDsItem.value; + + this.setState( + { + dataSourceItem: newDsItem, + dataSource: dataSource, + }, + () => panel.refresh() + ); }; openQueryInspector = () => { @@ -143,7 +159,7 @@ export class QueriesTab extends PureComponent { }; onAddQueryClick = () => { - if (this.state.currentDS.meta.mixed) { + if (this.state.dataSourceItem.meta.mixed) { this.setState({ isAddingMixed: true }); return; } @@ -163,13 +179,21 @@ export class QueriesTab extends PureComponent { renderTopSection(styles: QueriesTabStyls) { const { panel } = this.props; - const { currentDS, data } = this.state; + const { dataSourceItem, data, dataSource } = this.state; + + if (!dataSource) { + return null; + } return (
- +
- +