TablePanel: Fix ad-hoc variabes not working on default datasources (#44314)

* Dashboards: Fix ad-hoc variabes not working on default datasources

* not async

* Update comment
This commit is contained in:
Josh Hunt 2022-01-31 11:16:46 +11:00 committed by GitHub
parent d584a27055
commit b1b6205651
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,13 @@
import React, { Component } from 'react';
import { Select, Table } from '@grafana/ui';
import { DataFrame, FieldMatcherID, getFrameDisplayName, PanelProps, SelectableValue } from '@grafana/data';
import {
DataFrame,
FieldMatcherID,
getDataSourceRef,
getFrameDisplayName,
PanelProps,
SelectableValue,
} from '@grafana/data';
import { PanelOptions } from './models.gen';
import { css } from '@emotion/css';
import { config } from 'app/core/config';
@ -9,6 +16,7 @@ import { dispatch } from '../../../store/store';
import { applyFilterFromTable } from '../../../features/variables/adhoc/actions';
import { getDashboardSrv } from '../../../features/dashboard/services/DashboardSrv';
import { getFooterCells } from './footer';
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
interface Props extends PanelProps<PanelOptions> {}
@ -68,13 +76,19 @@ export class TablePanel extends Component<Props> {
onCellFilterAdded = (filter: FilterItem) => {
const { key, value, operator } = filter;
const panelModel = getDashboardSrv().getCurrent()?.getPanelById(this.props.id);
const datasource = panelModel?.datasource;
if (!datasource) {
if (!panelModel) {
return;
}
dispatch(applyFilterFromTable({ datasource, key, operator, value }));
// When the datasource is null/undefined (for a default datasource), we use getInstanceSettings
// to find the real datasource ref for the default datasource.
const datasourceInstance = getDatasourceSrv().getInstanceSettings(panelModel.datasource);
const datasourceRef = datasourceInstance && getDataSourceRef(datasourceInstance);
if (!datasourceRef) {
return;
}
dispatch(applyFilterFromTable({ datasource: datasourceRef, key, operator, value }));
};
renderTable(frame: DataFrame, width: number, height: number) {