2019-01-14 08:44:58 -06:00
|
|
|
// Libraries
|
|
|
|
import React, { PureComponent } from 'react';
|
2019-01-16 07:00:29 -06:00
|
|
|
import classNames from 'classnames';
|
2019-01-17 03:26:08 -06:00
|
|
|
import _ from 'lodash';
|
2019-01-14 08:44:58 -06:00
|
|
|
|
|
|
|
// Utils & Services
|
2019-01-15 04:40:12 -06:00
|
|
|
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
|
2019-06-03 10:55:59 -05:00
|
|
|
import { AngularComponent, getAngularLoader } from '@grafana/runtime';
|
2019-01-15 04:40:12 -06:00
|
|
|
import { Emitter } from 'app/core/utils/emitter';
|
2019-02-10 10:05:58 -06:00
|
|
|
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
2019-01-14 08:44:58 -06:00
|
|
|
|
|
|
|
// Types
|
2019-01-31 01:56:17 -06:00
|
|
|
import { PanelModel } from '../state/PanelModel';
|
2019-09-04 06:59:30 -05:00
|
|
|
import { DataQuery, DataSourceApi, PanelData, DataQueryRequest, ErrorBoundaryAlert } from '@grafana/ui';
|
2019-07-06 01:05:53 -05:00
|
|
|
import { TimeRange, LoadingState } from '@grafana/data';
|
2019-02-19 11:53:07 -06:00
|
|
|
import { DashboardModel } from '../state/DashboardModel';
|
2019-01-14 08:44:58 -06:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
panel: PanelModel;
|
2019-04-18 01:22:14 -05:00
|
|
|
data: PanelData;
|
2019-01-15 04:40:12 -06:00
|
|
|
query: DataQuery;
|
2019-02-19 11:53:07 -06:00
|
|
|
dashboard: DashboardModel;
|
2019-01-15 04:40:12 -06:00
|
|
|
onAddQuery: (query?: DataQuery) => void;
|
|
|
|
onRemoveQuery: (query: DataQuery) => void;
|
|
|
|
onMoveQuery: (query: DataQuery, direction: number) => void;
|
2019-01-29 02:39:23 -06:00
|
|
|
onChange: (query: DataQuery) => void;
|
2019-01-21 11:54:57 -06:00
|
|
|
dataSourceValue: string | null;
|
2019-01-17 03:26:08 -06:00
|
|
|
inMixedMode: boolean;
|
2019-01-14 08:44:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
2019-01-21 11:54:57 -06:00
|
|
|
loadedDataSourceValue: string | null | undefined;
|
2019-01-15 04:40:12 -06:00
|
|
|
datasource: DataSourceApi | null;
|
2019-01-16 07:00:29 -06:00
|
|
|
isCollapsed: boolean;
|
2019-02-14 08:18:55 -06:00
|
|
|
hasTextEditMode: boolean;
|
2019-04-20 14:26:49 -05:00
|
|
|
queryResponse?: PanelData;
|
2019-01-14 08:44:58 -06:00
|
|
|
}
|
|
|
|
|
2019-01-15 04:40:12 -06:00
|
|
|
export class QueryEditorRow extends PureComponent<Props, State> {
|
|
|
|
element: HTMLElement | null = null;
|
2019-02-14 08:18:55 -06:00
|
|
|
angularScope: AngularQueryComponentScope | null;
|
2019-01-15 04:40:12 -06:00
|
|
|
angularQueryEditor: AngularComponent | null = null;
|
2019-01-14 08:44:58 -06:00
|
|
|
|
2019-01-15 04:40:12 -06:00
|
|
|
state: State = {
|
|
|
|
datasource: null,
|
2019-01-16 07:00:29 -06:00
|
|
|
isCollapsed: false,
|
2019-01-21 11:54:57 -06:00
|
|
|
loadedDataSourceValue: undefined,
|
2019-02-14 08:18:55 -06:00
|
|
|
hasTextEditMode: false,
|
2019-04-02 00:22:52 -05:00
|
|
|
queryResponse: null,
|
2019-01-15 04:40:12 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.loadDatasource();
|
2019-02-14 08:18:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.angularQueryEditor) {
|
|
|
|
this.angularQueryEditor.destroy();
|
|
|
|
}
|
2019-01-15 04:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getAngularQueryComponentScope(): AngularQueryComponentScope {
|
2019-02-19 11:53:07 -06:00
|
|
|
const { panel, query, dashboard } = this.props;
|
2019-01-15 04:40:12 -06:00
|
|
|
const { datasource } = this.state;
|
|
|
|
|
|
|
|
return {
|
|
|
|
datasource: datasource,
|
|
|
|
target: query,
|
|
|
|
panel: panel,
|
2019-02-19 11:53:07 -06:00
|
|
|
dashboard: dashboard,
|
2019-01-15 04:40:12 -06:00
|
|
|
refresh: () => panel.refresh(),
|
2019-01-18 04:20:55 -06:00
|
|
|
render: () => panel.render(),
|
2019-01-15 04:40:12 -06:00
|
|
|
events: panel.events,
|
2019-02-10 10:05:58 -06:00
|
|
|
range: getTimeSrv().timeRange(),
|
2019-01-15 04:40:12 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadDatasource() {
|
|
|
|
const { query, panel } = this.props;
|
|
|
|
const dataSourceSrv = getDatasourceSrv();
|
|
|
|
const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
|
|
|
|
|
2019-02-14 08:18:55 -06:00
|
|
|
this.setState({
|
|
|
|
datasource,
|
|
|
|
loadedDataSourceValue: this.props.dataSourceValue,
|
2019-08-02 01:42:39 -05:00
|
|
|
hasTextEditMode: _.has(datasource, 'components.QueryCtrl.prototype.toggleEditorMode'),
|
2019-02-14 08:18:55 -06:00
|
|
|
});
|
2019-01-15 04:40:12 -06:00
|
|
|
}
|
|
|
|
|
2019-04-18 01:22:14 -05:00
|
|
|
componentDidUpdate(prevProps: Props) {
|
2019-01-21 11:54:57 -06:00
|
|
|
const { loadedDataSourceValue } = this.state;
|
2019-04-18 01:22:14 -05:00
|
|
|
const { data, query } = this.props;
|
|
|
|
|
|
|
|
if (data !== prevProps.data) {
|
2019-04-20 14:26:49 -05:00
|
|
|
this.setState({ queryResponse: filterPanelDataToQuery(data, query.refId) });
|
2019-04-18 01:22:14 -05:00
|
|
|
|
|
|
|
if (this.angularScope) {
|
|
|
|
this.angularScope.range = getTimeSrv().timeRange();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.angularQueryEditor) {
|
|
|
|
// Some query controllers listen to data error events and need a digest
|
|
|
|
// for some reason this needs to be done in next tick
|
|
|
|
setTimeout(this.angularQueryEditor.digest);
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 04:40:12 -06:00
|
|
|
|
|
|
|
// check if we need to load another datasource
|
2019-01-21 11:54:57 -06:00
|
|
|
if (loadedDataSourceValue !== this.props.dataSourceValue) {
|
2019-01-15 04:40:12 -06:00
|
|
|
if (this.angularQueryEditor) {
|
|
|
|
this.angularQueryEditor.destroy();
|
|
|
|
this.angularQueryEditor = null;
|
|
|
|
}
|
|
|
|
this.loadDatasource();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.element || this.angularQueryEditor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const loader = getAngularLoader();
|
|
|
|
const template = '<plugin-component type="query-ctrl" />';
|
|
|
|
const scopeProps = { ctrl: this.getAngularQueryComponentScope() };
|
|
|
|
this.angularQueryEditor = loader.load(this.element, scopeProps, template);
|
2019-02-14 08:18:55 -06:00
|
|
|
this.angularScope = scopeProps.ctrl;
|
2019-01-15 04:40:12 -06:00
|
|
|
}
|
|
|
|
|
2019-01-16 07:00:29 -06:00
|
|
|
onToggleCollapse = () => {
|
|
|
|
this.setState({ isCollapsed: !this.state.isCollapsed });
|
|
|
|
};
|
|
|
|
|
2019-01-23 10:44:22 -06:00
|
|
|
onRunQuery = () => {
|
2019-01-17 13:02:43 -06:00
|
|
|
this.props.panel.refresh();
|
|
|
|
};
|
|
|
|
|
2019-01-16 10:53:40 -06:00
|
|
|
renderPluginEditor() {
|
2019-04-20 14:26:49 -05:00
|
|
|
const { query, data, onChange } = this.props;
|
|
|
|
const { datasource, queryResponse } = this.state;
|
2019-01-16 10:53:40 -06:00
|
|
|
|
2019-04-04 11:30:15 -05:00
|
|
|
if (datasource.components.QueryCtrl) {
|
2019-01-17 06:08:20 -06:00
|
|
|
return <div ref={element => (this.element = element)} />;
|
2019-01-16 10:53:40 -06:00
|
|
|
}
|
|
|
|
|
2019-04-04 11:30:15 -05:00
|
|
|
if (datasource.components.QueryEditor) {
|
|
|
|
const QueryEditor = datasource.components.QueryEditor;
|
|
|
|
|
2019-04-02 00:22:52 -05:00
|
|
|
return (
|
|
|
|
<QueryEditor
|
|
|
|
query={query}
|
|
|
|
datasource={datasource}
|
|
|
|
onChange={onChange}
|
|
|
|
onRunQuery={this.onRunQuery}
|
|
|
|
queryResponse={queryResponse}
|
2019-04-20 14:26:49 -05:00
|
|
|
panelData={data}
|
2019-04-02 00:22:52 -05:00
|
|
|
/>
|
|
|
|
);
|
2019-01-16 10:53:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return <div>Data source plugin does not export any Query Editor component</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
onToggleEditMode = () => {
|
2019-02-14 08:18:55 -06:00
|
|
|
if (this.angularScope && this.angularScope.toggleEditorMode) {
|
|
|
|
this.angularScope.toggleEditorMode();
|
2019-01-16 10:53:40 -06:00
|
|
|
this.angularQueryEditor.digest();
|
|
|
|
}
|
2019-01-17 06:15:25 -06:00
|
|
|
|
|
|
|
if (this.state.isCollapsed) {
|
|
|
|
this.setState({ isCollapsed: false });
|
|
|
|
}
|
2019-01-17 06:08:20 -06:00
|
|
|
};
|
2019-01-16 10:53:40 -06:00
|
|
|
|
2019-01-17 03:26:08 -06:00
|
|
|
onRemoveQuery = () => {
|
|
|
|
this.props.onRemoveQuery(this.props.query);
|
|
|
|
};
|
|
|
|
|
|
|
|
onCopyQuery = () => {
|
|
|
|
const copy = _.cloneDeep(this.props.query);
|
|
|
|
this.props.onAddQuery(copy);
|
|
|
|
};
|
|
|
|
|
|
|
|
onDisableQuery = () => {
|
|
|
|
this.props.query.hide = !this.props.query.hide;
|
2019-01-29 02:39:23 -06:00
|
|
|
this.onRunQuery();
|
2019-01-17 03:26:08 -06:00
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
2019-01-17 06:08:20 -06:00
|
|
|
renderCollapsedText(): string | null {
|
2019-04-10 11:31:32 -05:00
|
|
|
const { datasource } = this.state;
|
|
|
|
if (datasource.getQueryDisplayText) {
|
|
|
|
return datasource.getQueryDisplayText(this.props.query);
|
|
|
|
}
|
|
|
|
|
2019-02-14 08:18:55 -06:00
|
|
|
if (this.angularScope && this.angularScope.getCollapsedText) {
|
|
|
|
return this.angularScope.getCollapsedText();
|
2019-01-17 06:08:20 -06:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-01-14 08:44:58 -06:00
|
|
|
render() {
|
2019-01-21 11:54:57 -06:00
|
|
|
const { query, inMixedMode } = this.props;
|
2019-02-14 08:18:55 -06:00
|
|
|
const { datasource, isCollapsed, hasTextEditMode } = this.state;
|
2019-01-17 03:26:08 -06:00
|
|
|
const isDisabled = query.hide;
|
|
|
|
|
|
|
|
const bodyClasses = classNames('query-editor-row__body gf-form-query', {
|
|
|
|
'query-editor-row__body--collapsed': isCollapsed,
|
|
|
|
});
|
|
|
|
|
|
|
|
const rowClasses = classNames('query-editor-row', {
|
|
|
|
'query-editor-row--disabled': isDisabled,
|
|
|
|
'gf-form-disabled': isDisabled,
|
|
|
|
});
|
2019-01-15 04:40:12 -06:00
|
|
|
|
|
|
|
if (!datasource) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-01-14 08:44:58 -06:00
|
|
|
|
2019-01-16 10:53:40 -06:00
|
|
|
return (
|
2019-01-17 03:26:08 -06:00
|
|
|
<div className={rowClasses}>
|
|
|
|
<div className="query-editor-row__header">
|
|
|
|
<div className="query-editor-row__ref-id" onClick={this.onToggleCollapse}>
|
2019-01-16 10:53:40 -06:00
|
|
|
{isCollapsed && <i className="fa fa-caret-right" />}
|
|
|
|
{!isCollapsed && <i className="fa fa-caret-down" />}
|
|
|
|
<span>{query.refId}</span>
|
2019-01-21 11:54:57 -06:00
|
|
|
{inMixedMode && <em className="query-editor-row__context-info"> ({datasource.name})</em>}
|
2019-01-17 03:26:08 -06:00
|
|
|
{isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
|
2019-01-16 07:00:29 -06:00
|
|
|
</div>
|
2019-01-19 01:22:56 -06:00
|
|
|
<div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
|
2019-01-17 06:08:20 -06:00
|
|
|
{isCollapsed && <div>{this.renderCollapsedText()}</div>}
|
|
|
|
</div>
|
2019-01-17 03:26:08 -06:00
|
|
|
<div className="query-editor-row__actions">
|
2019-02-14 08:18:55 -06:00
|
|
|
{hasTextEditMode && (
|
2019-01-17 06:08:20 -06:00
|
|
|
<button
|
|
|
|
className="query-editor-row__action"
|
|
|
|
onClick={this.onToggleEditMode}
|
|
|
|
title="Toggle text edit mode"
|
|
|
|
>
|
2019-01-16 10:53:40 -06:00
|
|
|
<i className="fa fa-fw fa-pencil" />
|
|
|
|
</button>
|
|
|
|
)}
|
2019-01-17 03:26:08 -06:00
|
|
|
<button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, 1)}>
|
2019-01-16 10:53:40 -06:00
|
|
|
<i className="fa fa-fw fa-arrow-down" />
|
|
|
|
</button>
|
2019-01-17 03:26:08 -06:00
|
|
|
<button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, -1)}>
|
2019-01-16 10:53:40 -06:00
|
|
|
<i className="fa fa-fw fa-arrow-up" />
|
|
|
|
</button>
|
2019-01-17 03:26:08 -06:00
|
|
|
<button className="query-editor-row__action" onClick={this.onCopyQuery} title="Duplicate query">
|
2019-01-16 10:53:40 -06:00
|
|
|
<i className="fa fa-fw fa-copy" />
|
|
|
|
</button>
|
2019-01-17 03:26:08 -06:00
|
|
|
<button className="query-editor-row__action" onClick={this.onDisableQuery} title="Disable/enable query">
|
|
|
|
{isDisabled && <i className="fa fa-fw fa-eye-slash" />}
|
|
|
|
{!isDisabled && <i className="fa fa-fw fa-eye" />}
|
2019-01-16 10:53:40 -06:00
|
|
|
</button>
|
2019-01-17 03:26:08 -06:00
|
|
|
<button className="query-editor-row__action" onClick={this.onRemoveQuery} title="Remove query">
|
2019-01-16 10:53:40 -06:00
|
|
|
<i className="fa fa-fw fa-trash" />
|
|
|
|
</button>
|
2019-01-16 07:00:29 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-09-02 13:45:18 -05:00
|
|
|
<div className={bodyClasses}>
|
2019-09-04 06:59:30 -05:00
|
|
|
<ErrorBoundaryAlert>{this.renderPluginEditor()}</ErrorBoundaryAlert>
|
2019-09-02 13:45:18 -05:00
|
|
|
</div>
|
2019-01-16 10:53:40 -06:00
|
|
|
</div>
|
|
|
|
);
|
2019-01-14 08:44:58 -06:00
|
|
|
}
|
|
|
|
}
|
2019-01-15 04:40:12 -06:00
|
|
|
|
|
|
|
export interface AngularQueryComponentScope {
|
|
|
|
target: DataQuery;
|
|
|
|
panel: PanelModel;
|
2019-02-19 11:53:07 -06:00
|
|
|
dashboard: DashboardModel;
|
2019-01-15 04:40:12 -06:00
|
|
|
events: Emitter;
|
|
|
|
refresh: () => void;
|
|
|
|
render: () => void;
|
|
|
|
datasource: DataSourceApi;
|
2019-01-16 10:53:40 -06:00
|
|
|
toggleEditorMode?: () => void;
|
2019-01-17 06:08:20 -06:00
|
|
|
getCollapsedText?: () => string;
|
2019-02-10 10:05:58 -06:00
|
|
|
range: TimeRange;
|
2019-01-15 04:40:12 -06:00
|
|
|
}
|
2019-04-20 14:26:49 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a version of the PanelData limited to the query we are looking at
|
|
|
|
*/
|
|
|
|
export function filterPanelDataToQuery(data: PanelData, refId: string): PanelData | undefined {
|
|
|
|
const series = data.series.filter(series => series.refId === refId);
|
|
|
|
|
|
|
|
// No matching series
|
|
|
|
if (!series.length) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't pass the request if all requests are the same
|
|
|
|
const request: DataQueryRequest = undefined;
|
|
|
|
// TODO: look in sub-requets to match the info
|
|
|
|
|
|
|
|
// Only say this is an error if the error links to the query
|
|
|
|
let state = LoadingState.Done;
|
|
|
|
const error = data.error && data.error.refId === refId ? data.error : undefined;
|
|
|
|
if (error) {
|
|
|
|
state = LoadingState.Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
state,
|
|
|
|
series,
|
|
|
|
request,
|
|
|
|
error,
|
|
|
|
};
|
|
|
|
}
|