Skip to content

Commit

Permalink
[v9.3.x] Bugfix: Prevent previous query editor to set default values …
Browse files Browse the repository at this point in the history
…when changing data source (#61286)

Bugfix: Prevent previous query editor to set default values when changing data source (#60218)

* Fixed issue where the query editor of the previous ds sets default values on query passed to the query editor of the next ds.

* Fixed issue with changing data source for query in Alerting.

* Will apply default values from DS if available.

* Fix failing tests.

* fixed spell error.

* reverted getDefaultQuery call so it can be added in a separate PR.

(cherry picked from commit b633d53)

Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
  • Loading branch information
grafanabot and mckn committed Jan 11, 2023
1 parent 31d483a commit 1ca625f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ export class QueryRows extends PureComponent<Props> {
return item;
}

return copyModel(item, settings.uid);
const previousSettings = this.getDataSourceSettings(item);

// Copy model if changing to a datasource of same type.
if (settings.type === previousSettings?.type) {
return copyModel(item, settings);
}
return newModel(item, settings);
});

onQueriesChange(updatedQueries);
};

Expand Down Expand Up @@ -181,11 +188,34 @@ export class QueryRows extends PureComponent<Props> {
}
}

function copyModel(item: AlertQuery, uid: string): Omit<AlertQuery, 'datasource'> {
function copyModel(item: AlertQuery, settings: DataSourceInstanceSettings): Omit<AlertQuery, 'datasource'> {
return {
...item,
model: omit(item.model, 'datasource'),
datasourceUid: uid,
model: {
...omit(item.model, 'datasource'),
datasource: {
type: settings.type,
uid: settings.uid,
},
},
datasourceUid: settings.uid,
};
}

function newModel(item: AlertQuery, settings: DataSourceInstanceSettings): Omit<AlertQuery, 'datasource'> {
return {
refId: item.refId,
relativeTimeRange: item.relativeTimeRange,
queryType: '',
datasourceUid: settings.uid,
model: {
refId: item.refId,
hide: false,
datasource: {
type: settings.type,
uid: settings.uid,
},
},
};
}

Expand Down
15 changes: 12 additions & 3 deletions public/app/features/query/components/QueryEditorRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
toLegacyResponseData,
} from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { AngularComponent, getAngularLoader } from '@grafana/runtime';
import { AngularComponent, getAngularLoader, getDataSourceSrv } from '@grafana/runtime';
import { Badge, ErrorBoundaryAlert, HorizontalGroup } from '@grafana/ui';
import { OperationRowHelp } from 'app/core/components/QueryOperationRow/OperationRowHelp';
import { QueryOperationAction } from 'app/core/components/QueryOperationRow/QueryOperationAction';
Expand All @@ -33,7 +33,6 @@ import {
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { DashboardModel } from 'app/features/dashboard/state/DashboardModel';
import { PanelModel } from 'app/features/dashboard/state/PanelModel';
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';

import { RowActionComponents } from './QueryActionComponent';
import { QueryEditorRowHeader } from './QueryEditorRowHeader';
Expand Down Expand Up @@ -141,7 +140,7 @@ export class QueryEditorRow<TQuery extends DataQuery> extends PureComponent<Prop
}

async loadDatasource() {
const dataSourceSrv = getDatasourceSrv();
const dataSourceSrv = getDataSourceSrv();
let datasource: DataSourceApi;
const dataSourceIdentifier = this.getQueryDataSourceIdentifier();

Expand Down Expand Up @@ -239,10 +238,20 @@ export class QueryEditorRow<TQuery extends DataQuery> extends PureComponent<Prop
}
}

waitingForDatasourceToLoad = (): boolean => {
// if we not yet have loaded the datasource in state the
// ds in props and the ds in state will have different values.
return this.props.dataSource.uid !== this.state.datasource?.uid;
};

renderPluginEditor = () => {
const { query, onChange, queries, onRunQuery, onAddQuery, app = CoreApp.PanelEditor, history } = this.props;
const { datasource, data } = this.state;

if (this.waitingForDatasourceToLoad()) {
return null;
}

if (datasource?.components?.QueryCtrl) {
return <div ref={(element) => (this.element = element)} />;
}
Expand Down

0 comments on commit 1ca625f

Please sign in to comment.