2018-12-11 06:36:44 -06:00
|
|
|
// Libraries
|
2019-01-10 15:47:09 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
2018-12-11 06:36:44 -06:00
|
|
|
// Components
|
2019-01-10 15:47:09 -06:00
|
|
|
import { EditorTabBody, EditorToolbarView } from './EditorTabBody';
|
2018-12-14 07:05:47 -06:00
|
|
|
import { DataSourcePicker } from 'app/core/components/Select/DataSourcePicker';
|
2018-12-13 07:28:44 -06:00
|
|
|
import { QueryOptions } from './QueryOptions';
|
2020-02-12 03:42:57 -06:00
|
|
|
import { PanelOptionsGroup } from '@grafana/ui';
|
2020-04-15 09:51:51 -05:00
|
|
|
import { getLocationSrv } from '@grafana/runtime';
|
2019-10-18 06:09:55 -05:00
|
|
|
import { QueryEditorRows } from './QueryEditorRows';
|
2018-11-19 07:35:16 -06:00
|
|
|
// Services
|
|
|
|
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
|
2020-01-21 03:08:07 -06:00
|
|
|
import { backendSrv } from 'app/core/services/backend_srv';
|
2018-12-11 06:36:44 -06:00
|
|
|
import config from 'app/core/config';
|
|
|
|
// Types
|
2019-01-31 01:56:17 -06:00
|
|
|
import { PanelModel } from '../state/PanelModel';
|
|
|
|
import { DashboardModel } from '../state/DashboardModel';
|
2020-04-24 05:51:38 -05:00
|
|
|
import { DataQuery, DataSourceSelectItem, DefaultTimeRange, LoadingState, PanelData } from '@grafana/data';
|
2018-12-19 03:53:14 -06:00
|
|
|
import { PluginHelp } from 'app/core/components/PluginHelp/PluginHelp';
|
2019-10-18 06:09:55 -05:00
|
|
|
import { addQuery } from 'app/core/utils/query';
|
2019-04-18 01:22:14 -05:00
|
|
|
import { Unsubscribable } from 'rxjs';
|
2020-04-24 05:51:38 -05:00
|
|
|
import { DashboardQueryEditor, isSharedDashboardQuery } from 'app/plugins/datasource/dashboard';
|
2019-10-30 13:38:28 -05:00
|
|
|
import { expressionDatasource, ExpressionDatasourceID } from 'app/features/expressions/ExpressionDatasource';
|
2020-04-24 05:51:38 -05:00
|
|
|
import { e2e } from '@grafana/e2e';
|
2018-11-20 07:17:03 -06:00
|
|
|
|
2018-06-26 14:07:41 -05:00
|
|
|
interface Props {
|
|
|
|
panel: PanelModel;
|
|
|
|
dashboard: DashboardModel;
|
|
|
|
}
|
|
|
|
|
2018-11-19 07:35:16 -06:00
|
|
|
interface State {
|
2018-12-11 10:03:38 -06:00
|
|
|
currentDS: DataSourceSelectItem;
|
2018-12-12 01:33:49 -06:00
|
|
|
helpContent: JSX.Element;
|
2018-12-11 23:52:08 -06:00
|
|
|
isLoadingHelp: boolean;
|
2018-12-11 10:03:38 -06:00
|
|
|
isPickerOpen: boolean;
|
2018-12-12 01:33:49 -06:00
|
|
|
isAddingMixed: boolean;
|
2019-01-17 04:25:44 -06:00
|
|
|
scrollTop: number;
|
2019-04-18 01:22:14 -05:00
|
|
|
data: PanelData;
|
2018-11-19 07:35:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export class QueriesTab extends PureComponent<Props, State> {
|
|
|
|
datasources: DataSourceSelectItem[] = getDatasourceSrv().getMetricSources();
|
2020-01-21 03:08:07 -06:00
|
|
|
backendSrv = backendSrv;
|
2019-04-18 01:22:14 -05:00
|
|
|
querySubscription: Unsubscribable;
|
2018-06-26 14:07:41 -05:00
|
|
|
|
2019-01-15 04:40:12 -06:00
|
|
|
state: State = {
|
|
|
|
isLoadingHelp: false,
|
|
|
|
currentDS: this.findCurrentDataSource(),
|
|
|
|
helpContent: null,
|
|
|
|
isPickerOpen: false,
|
|
|
|
isAddingMixed: false,
|
2019-01-17 04:25:44 -06:00
|
|
|
scrollTop: 0,
|
2019-04-18 01:22:14 -05:00
|
|
|
data: {
|
|
|
|
state: LoadingState.NotStarted,
|
|
|
|
series: [],
|
2019-09-25 04:19:17 -05:00
|
|
|
timeRange: DefaultTimeRange,
|
2019-04-18 01:22:14 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { panel } = this.props;
|
2019-04-18 16:10:18 -05:00
|
|
|
const queryRunner = panel.getQueryRunner();
|
2019-04-18 01:22:14 -05:00
|
|
|
|
2019-09-12 10:28:46 -05:00
|
|
|
this.querySubscription = queryRunner.getData(false).subscribe({
|
|
|
|
next: (data: PanelData) => this.onPanelDataUpdate(data),
|
|
|
|
});
|
2019-04-18 01:22:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.querySubscription) {
|
|
|
|
this.querySubscription.unsubscribe();
|
|
|
|
this.querySubscription = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 10:28:46 -05:00
|
|
|
onPanelDataUpdate(data: PanelData) {
|
|
|
|
this.setState({ data });
|
|
|
|
}
|
2018-06-26 14:07:41 -05:00
|
|
|
|
2019-01-08 08:54:12 -06:00
|
|
|
findCurrentDataSource(): DataSourceSelectItem {
|
|
|
|
const { panel } = this.props;
|
|
|
|
return this.datasources.find(datasource => datasource.value === panel.datasource) || this.datasources[0];
|
|
|
|
}
|
|
|
|
|
2019-10-18 06:09:55 -05:00
|
|
|
onChangeDataSource = (datasource: DataSourceSelectItem) => {
|
2018-11-19 07:35:16 -06:00
|
|
|
const { panel } = this.props;
|
2018-12-11 10:03:38 -06:00
|
|
|
const { currentDS } = this.state;
|
2018-12-11 06:36:44 -06:00
|
|
|
|
2018-11-19 07:35:16 -06:00
|
|
|
// switching to mixed
|
|
|
|
if (datasource.meta.mixed) {
|
2019-10-18 06:09:55 -05:00
|
|
|
// Set the datasource on all targets
|
2018-11-19 07:35:16 -06:00
|
|
|
panel.targets.forEach(target => {
|
2019-10-30 13:38:28 -05:00
|
|
|
if (target.datasource !== ExpressionDatasourceID) {
|
|
|
|
target.datasource = panel.datasource;
|
|
|
|
if (!target.datasource) {
|
|
|
|
target.datasource = config.defaultDatasource;
|
|
|
|
}
|
2018-11-19 07:35:16 -06:00
|
|
|
}
|
|
|
|
});
|
2018-12-11 10:03:38 -06:00
|
|
|
} else if (currentDS) {
|
2018-12-11 06:36:44 -06:00
|
|
|
// if switching from mixed
|
2018-12-11 10:03:38 -06:00
|
|
|
if (currentDS.meta.mixed) {
|
2019-10-18 06:09:55 -05:00
|
|
|
// Remove the explicit datasource
|
2018-12-11 06:36:44 -06:00
|
|
|
for (const target of panel.targets) {
|
2019-10-30 13:38:28 -05:00
|
|
|
if (target.datasource !== ExpressionDatasourceID) {
|
|
|
|
delete target.datasource;
|
|
|
|
}
|
2018-12-11 06:36:44 -06:00
|
|
|
}
|
2018-12-11 10:03:38 -06:00
|
|
|
} else if (currentDS.meta.id !== datasource.meta.id) {
|
2018-12-11 06:36:44 -06:00
|
|
|
// we are changing data source type, clear queries
|
|
|
|
panel.targets = [{ refId: 'A' }];
|
|
|
|
}
|
2018-11-19 07:35:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
panel.datasource = datasource.value;
|
|
|
|
panel.refresh();
|
|
|
|
|
2018-12-11 10:03:38 -06:00
|
|
|
this.setState({
|
|
|
|
currentDS: datasource,
|
|
|
|
});
|
2018-11-20 07:17:03 -06:00
|
|
|
};
|
|
|
|
|
2020-04-15 09:51:51 -05:00
|
|
|
openQueryInspector = () => {
|
2018-11-27 03:52:47 -06:00
|
|
|
const { panel } = this.props;
|
2020-04-15 09:51:51 -05:00
|
|
|
getLocationSrv().update({
|
|
|
|
query: { inspect: panel.id, inspectTab: 'query' },
|
|
|
|
partial: true,
|
|
|
|
});
|
2018-11-22 08:53:34 -06:00
|
|
|
};
|
|
|
|
|
2018-12-11 23:52:08 -06:00
|
|
|
renderHelp = () => {
|
2018-12-18 07:49:59 -06:00
|
|
|
return <PluginHelp plugin={this.state.currentDS.meta} type="query_help" />;
|
2018-12-11 23:52:08 -06:00
|
|
|
};
|
2018-11-21 04:34:01 -06:00
|
|
|
|
2019-10-18 06:09:55 -05:00
|
|
|
/**
|
|
|
|
* Sets the queries for the panel
|
|
|
|
*/
|
|
|
|
onUpdateQueries = (queries: DataQuery[]) => {
|
|
|
|
this.props.panel.targets = queries;
|
|
|
|
this.forceUpdate();
|
2018-12-11 06:36:44 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
onAddQueryClick = () => {
|
2018-12-12 01:33:49 -06:00
|
|
|
if (this.state.currentDS.meta.mixed) {
|
2018-12-12 01:54:12 -06:00
|
|
|
this.setState({ isAddingMixed: true });
|
2018-12-12 01:33:49 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-18 06:09:55 -05:00
|
|
|
this.onUpdateQueries(addQuery(this.props.panel.targets));
|
|
|
|
this.onScrollBottom();
|
2018-12-11 06:36:44 -06:00
|
|
|
};
|
|
|
|
|
2019-10-30 13:38:28 -05:00
|
|
|
onAddExpressionClick = () => {
|
|
|
|
this.onUpdateQueries(addQuery(this.props.panel.targets, expressionDatasource.newQuery()));
|
|
|
|
this.onScrollBottom();
|
|
|
|
};
|
|
|
|
|
2019-10-18 06:09:55 -05:00
|
|
|
onScrollBottom = () => {
|
|
|
|
this.setState({ scrollTop: this.state.scrollTop + 10000 });
|
2018-12-11 06:36:44 -06:00
|
|
|
};
|
|
|
|
|
2018-12-11 10:03:38 -06:00
|
|
|
renderToolbar = () => {
|
2019-01-17 04:25:44 -06:00
|
|
|
const { currentDS, isAddingMixed } = this.state;
|
2019-08-27 04:06:49 -05:00
|
|
|
const showAddButton = !(isAddingMixed || isSharedDashboardQuery(currentDS.name));
|
2018-11-09 08:49:55 -06:00
|
|
|
|
2019-01-17 04:25:44 -06:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<DataSourcePicker datasources={this.datasources} onChange={this.onChangeDataSource} current={currentDS} />
|
2019-02-04 10:26:20 -06:00
|
|
|
<div className="flex-grow-1" />
|
2019-08-27 04:06:49 -05:00
|
|
|
{showAddButton && (
|
2019-02-17 10:35:46 -06:00
|
|
|
<button className="btn navbar-button" onClick={this.onAddQueryClick}>
|
2020-04-15 09:51:51 -05:00
|
|
|
Add query
|
2019-01-18 08:29:58 -06:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
{isAddingMixed && this.renderMixedPicker()}
|
2019-10-30 13:38:28 -05:00
|
|
|
{config.featureToggles.expressions && (
|
|
|
|
<button className="btn navbar-button" onClick={this.onAddExpressionClick}>
|
|
|
|
Add Expression
|
|
|
|
</button>
|
|
|
|
)}
|
2019-01-17 04:25:44 -06:00
|
|
|
</>
|
|
|
|
);
|
2018-12-11 10:03:38 -06:00
|
|
|
};
|
2018-11-09 08:49:55 -06:00
|
|
|
|
2018-12-12 01:33:49 -06:00
|
|
|
renderMixedPicker = () => {
|
2019-12-10 16:17:55 -06:00
|
|
|
// We cannot filter on mixed flag as some mixed data sources like external plugin
|
|
|
|
// meta queries data source is mixed but also supports it's own queries
|
|
|
|
const filteredDsList = this.datasources.filter(ds => ds.meta.id !== 'mixed');
|
|
|
|
|
2018-12-12 01:33:49 -06:00
|
|
|
return (
|
|
|
|
<DataSourcePicker
|
2019-12-10 16:17:55 -06:00
|
|
|
datasources={filteredDsList}
|
2018-12-14 07:05:47 -06:00
|
|
|
onChange={this.onAddMixedQuery}
|
2018-12-12 01:33:49 -06:00
|
|
|
current={null}
|
|
|
|
autoFocus={true}
|
|
|
|
onBlur={this.onMixedPickerBlur}
|
2019-04-09 07:59:30 -05:00
|
|
|
openMenuOnFocus={true}
|
2018-12-12 01:33:49 -06:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-07-30 08:49:32 -05:00
|
|
|
onAddMixedQuery = (datasource: any) => {
|
2019-10-18 06:09:55 -05:00
|
|
|
this.props.panel.targets = addQuery(this.props.panel.targets, { datasource: datasource.name });
|
2019-01-17 04:25:44 -06:00
|
|
|
this.setState({ isAddingMixed: false, scrollTop: this.state.scrollTop + 10000 });
|
2019-10-18 06:09:55 -05:00
|
|
|
this.forceUpdate();
|
2018-12-12 01:33:49 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
onMixedPickerBlur = () => {
|
|
|
|
this.setState({ isAddingMixed: false });
|
|
|
|
};
|
|
|
|
|
2019-07-30 08:49:32 -05:00
|
|
|
onQueryChange = (query: DataQuery, index: number) => {
|
2019-01-29 02:39:23 -06:00
|
|
|
this.props.panel.changeQuery(query, index);
|
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
2019-01-17 04:25:44 -06:00
|
|
|
setScrollTop = (event: React.MouseEvent<HTMLElement>) => {
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
this.setState({ scrollTop: target.scrollTop });
|
|
|
|
};
|
|
|
|
|
2019-10-18 06:09:55 -05:00
|
|
|
renderQueryBody = () => {
|
2019-02-19 11:53:07 -06:00
|
|
|
const { panel, dashboard } = this.props;
|
2019-10-18 06:09:55 -05:00
|
|
|
const { currentDS, data } = this.state;
|
|
|
|
|
|
|
|
if (isSharedDashboardQuery(currentDS.name)) {
|
|
|
|
return <DashboardQueryEditor panel={panel} panelData={data} onChange={query => this.onUpdateQueries([query])} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-04-24 05:51:38 -05:00
|
|
|
<div aria-label={e2e.components.QueryTab.selectors.content}>
|
2019-10-18 06:09:55 -05:00
|
|
|
<QueryEditorRows
|
|
|
|
queries={panel.targets}
|
|
|
|
datasource={currentDS}
|
|
|
|
onChangeQueries={this.onUpdateQueries}
|
|
|
|
onScrollBottom={this.onScrollBottom}
|
|
|
|
panel={panel}
|
|
|
|
dashboard={dashboard}
|
|
|
|
data={data}
|
|
|
|
/>
|
|
|
|
<PanelOptionsGroup>
|
|
|
|
<QueryOptions panel={panel} datasource={currentDS} />
|
|
|
|
</PanelOptionsGroup>
|
2020-04-24 05:51:38 -05:00
|
|
|
</div>
|
2019-10-18 06:09:55 -05:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2020-02-12 03:42:57 -06:00
|
|
|
const { scrollTop } = this.state;
|
2018-12-21 04:57:21 -06:00
|
|
|
const queryInspector: EditorToolbarView = {
|
2020-04-15 09:51:51 -05:00
|
|
|
title: 'Query inspector',
|
|
|
|
onClick: this.openQueryInspector,
|
2018-12-11 23:52:08 -06:00
|
|
|
};
|
2018-11-09 06:17:41 -06:00
|
|
|
|
2018-12-21 04:57:21 -06:00
|
|
|
const dsHelp: EditorToolbarView = {
|
2018-12-13 14:24:41 -06:00
|
|
|
heading: 'Help',
|
2020-04-12 15:20:02 -05:00
|
|
|
icon: 'question-circle',
|
2018-12-11 23:52:08 -06:00
|
|
|
render: this.renderHelp,
|
|
|
|
};
|
|
|
|
|
2018-11-09 06:17:41 -06:00
|
|
|
return (
|
2019-01-17 04:25:44 -06:00
|
|
|
<EditorTabBody
|
2020-04-15 09:51:51 -05:00
|
|
|
heading="Data source"
|
2019-01-17 04:25:44 -06:00
|
|
|
renderToolbar={this.renderToolbar}
|
|
|
|
toolbarItems={[queryInspector, dsHelp]}
|
|
|
|
setScrollTop={this.setScrollTop}
|
|
|
|
scrollTop={scrollTop}
|
|
|
|
>
|
2020-02-12 03:42:57 -06:00
|
|
|
<>{this.renderQueryBody()}</>
|
2018-11-09 06:17:41 -06:00
|
|
|
</EditorTabBody>
|
|
|
|
);
|
2018-06-26 14:07:41 -05:00
|
|
|
}
|
|
|
|
}
|