grafana/public/app/features/dashboard/panel_editor/QueriesTab.tsx

225 lines
6.1 KiB
TypeScript
Raw Normal View History

2018-12-11 06:36:44 -06:00
// Libraries
import React, { PureComponent } from 'react';
2018-12-11 06:36:44 -06:00
import _ from 'lodash';
2019-01-04 05:38:50 -06:00
2018-12-11 06:36:44 -06:00
// Components
import { EditorTabBody, EditorToolbarView } from './EditorTabBody';
2018-12-14 07:05:47 -06:00
import { DataSourcePicker } from 'app/core/components/Select/DataSourcePicker';
import { QueryInspector } from './QueryInspector';
import { QueryOptions } from './QueryOptions';
2019-01-13 05:42:21 -06:00
import { PanelOptionsGroup } from '@grafana/ui';
import { QueryEditorRow } from './QueryEditorRow';
2019-01-04 05:38:50 -06:00
// Services
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
2018-12-21 04:57:21 -06:00
import { BackendSrv, getBackendSrv } from 'app/core/services/backend_srv';
2018-12-11 06:36:44 -06:00
import config from 'app/core/config';
2019-01-04 05:38:50 -06:00
2018-12-11 06:36:44 -06:00
// Types
import { PanelModel } from '../state/PanelModel';
import { DashboardModel } from '../state/DashboardModel';
2019-01-18 08:29:58 -06:00
import { DataQuery, DataSourceSelectItem } from '@grafana/ui/src/types';
2018-12-19 03:53:14 -06:00
import { PluginHelp } from 'app/core/components/PluginHelp/PluginHelp';
2018-11-20 07:17:03 -06:00
interface Props {
panel: PanelModel;
dashboard: DashboardModel;
}
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;
scrollTop: number;
}
export class QueriesTab extends PureComponent<Props, State> {
datasources: DataSourceSelectItem[] = getDatasourceSrv().getMetricSources();
2018-11-20 07:17:03 -06:00
backendSrv: BackendSrv = getBackendSrv();
state: State = {
isLoadingHelp: false,
currentDS: this.findCurrentDataSource(),
helpContent: null,
isPickerOpen: false,
isAddingMixed: false,
scrollTop: 0,
};
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];
}
onChangeDataSource = datasource => {
const { panel } = this.props;
2018-12-11 10:03:38 -06:00
const { currentDS } = this.state;
2018-12-11 06:36:44 -06:00
// switching to mixed
if (datasource.meta.mixed) {
panel.targets.forEach(target => {
target.datasource = panel.datasource;
if (!target.datasource) {
target.datasource = config.defaultDatasource;
}
});
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) {
2018-12-11 06:36:44 -06:00
for (const target of panel.targets) {
delete target.datasource;
}
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' }];
}
}
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
};
renderQueryInspector = () => {
const { panel } = this.props;
2019-01-10 16:15:37 -06:00
return <QueryInspector panel={panel} />;
};
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-12-12 01:54:12 -06:00
onAddQuery = (query?: Partial<DataQuery>) => {
2018-12-11 06:36:44 -06:00
this.props.panel.addQuery(query);
2019-01-17 08:11:38 -06:00
this.setState({ scrollTop: this.state.scrollTop + 100000 });
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-01-17 08:11:38 -06:00
this.onAddQuery();
2018-12-11 06:36:44 -06:00
};
onRemoveQuery = (query: DataQuery) => {
const { panel } = this.props;
const index = _.indexOf(panel.targets, query);
panel.targets.splice(index, 1);
panel.refresh();
this.forceUpdate();
};
onMoveQuery = (query: DataQuery, direction: number) => {
const { panel } = this.props;
const index = _.indexOf(panel.targets, query);
_.move(panel.targets, index, index + direction);
this.forceUpdate();
};
2018-12-11 10:03:38 -06:00
renderToolbar = () => {
const { currentDS, isAddingMixed } = this.state;
2018-11-09 08:49:55 -06:00
return (
<>
<DataSourcePicker datasources={this.datasources} onChange={this.onChangeDataSource} current={currentDS} />
2019-01-18 08:29:58 -06:00
<div className="flex-grow" />
{!isAddingMixed && (
<button className="btn navbar-button navbar-button--primary" onClick={this.onAddQueryClick}>
Add Query
</button>
)}
{isAddingMixed && this.renderMixedPicker()}
</>
);
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 = () => {
return (
<DataSourcePicker
datasources={this.datasources}
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}
/>
);
};
onAddMixedQuery = datasource => {
this.onAddQuery({ datasource: datasource.name });
this.setState({ isAddingMixed: false, scrollTop: this.state.scrollTop + 10000 });
2018-12-12 01:33:49 -06:00
};
onMixedPickerBlur = () => {
this.setState({ isAddingMixed: false });
};
onQueryChange = (query: DataQuery, index) => {
this.props.panel.changeQuery(query, index);
this.forceUpdate();
};
setScrollTop = (event: React.MouseEvent<HTMLElement>) => {
const target = event.target as HTMLElement;
this.setState({ scrollTop: target.scrollTop });
};
2018-12-11 10:03:38 -06:00
render() {
const { panel } = this.props;
const { currentDS, scrollTop } = this.state;
2018-12-11 23:52:08 -06:00
2018-12-21 04:57:21 -06:00
const queryInspector: EditorToolbarView = {
2018-12-11 23:52:08 -06:00
title: 'Query Inspector',
render: this.renderQueryInspector,
};
2018-12-21 04:57:21 -06:00
const dsHelp: EditorToolbarView = {
2018-12-13 14:24:41 -06:00
heading: 'Help',
2018-12-11 23:52:08 -06:00
icon: 'fa fa-question',
render: this.renderHelp,
};
return (
<EditorTabBody
2019-01-17 06:08:20 -06:00
heading="Queries to"
renderToolbar={this.renderToolbar}
toolbarItems={[queryInspector, dsHelp]}
setScrollTop={this.setScrollTop}
scrollTop={scrollTop}
>
<>
<div className="query-editor-rows">
{panel.targets.map((query, index) => (
<QueryEditorRow
2019-01-21 11:54:57 -06:00
dataSourceValue={query.datasource || panel.datasource}
key={query.refId}
panel={panel}
query={query}
onChange={query => this.onQueryChange(query, index)}
onRemoveQuery={this.onRemoveQuery}
onAddQuery={this.onAddQuery}
onMoveQuery={this.onMoveQuery}
inMixedMode={currentDS.meta.mixed}
/>
))}
</div>
2019-01-13 05:42:21 -06:00
<PanelOptionsGroup>
<QueryOptions panel={panel} datasource={currentDS} />
2019-01-13 05:42:21 -06:00
</PanelOptionsGroup>
</>
</EditorTabBody>
);
}
}