2019-01-14 15:44:58 +01:00
|
|
|
// Libraries
|
|
|
|
|
import React, { PureComponent } from 'react';
|
2019-01-16 14:00:29 +01:00
|
|
|
import classNames from 'classnames';
|
2019-01-17 10:26:08 +01:00
|
|
|
import _ from 'lodash';
|
2019-01-14 15:44:58 +01:00
|
|
|
// Utils & Services
|
2019-01-15 11:40:12 +01:00
|
|
|
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
|
2019-06-03 17:55:59 +02:00
|
|
|
import { AngularComponent, getAngularLoader } from '@grafana/runtime';
|
2019-01-15 11:40:12 +01:00
|
|
|
import { Emitter } from 'app/core/utils/emitter';
|
2019-02-10 17:05:58 +01:00
|
|
|
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
2019-01-14 15:44:58 +01:00
|
|
|
// Types
|
2019-01-31 08:56:17 +01:00
|
|
|
import { PanelModel } from '../state/PanelModel';
|
2019-09-04 13:59:30 +02:00
|
|
|
import { DataQuery, DataSourceApi, PanelData, DataQueryRequest, ErrorBoundaryAlert } from '@grafana/ui';
|
2019-09-12 17:28:46 +02:00
|
|
|
import { TimeRange, LoadingState, toLegacyResponseData } from '@grafana/data';
|
2019-02-19 18:53:07 +01:00
|
|
|
import { DashboardModel } from '../state/DashboardModel';
|
2019-01-14 15:44:58 +01:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
panel: PanelModel;
|
2019-04-17 23:22:14 -07:00
|
|
|
data: PanelData;
|
2019-01-15 11:40:12 +01:00
|
|
|
query: DataQuery;
|
2019-02-19 18:53:07 +01:00
|
|
|
dashboard: DashboardModel;
|
2019-01-15 11:40:12 +01:00
|
|
|
onAddQuery: (query?: DataQuery) => void;
|
|
|
|
|
onRemoveQuery: (query: DataQuery) => void;
|
|
|
|
|
onMoveQuery: (query: DataQuery, direction: number) => void;
|
2019-01-29 09:39:23 +01:00
|
|
|
onChange: (query: DataQuery) => void;
|
2019-01-21 18:54:57 +01:00
|
|
|
dataSourceValue: string | null;
|
2019-01-17 10:26:08 +01:00
|
|
|
inMixedMode: boolean;
|
2019-01-14 15:44:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface State {
|
2019-01-21 18:54:57 +01:00
|
|
|
loadedDataSourceValue: string | null | undefined;
|
2019-01-15 11:40:12 +01:00
|
|
|
datasource: DataSourceApi | null;
|
2019-01-16 14:00:29 +01:00
|
|
|
isCollapsed: boolean;
|
2019-02-14 15:18:55 +01:00
|
|
|
hasTextEditMode: boolean;
|
2019-09-23 11:13:33 +02:00
|
|
|
data?: PanelData;
|
2019-01-14 15:44:58 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:40:12 +01:00
|
|
|
export class QueryEditorRow extends PureComponent<Props, State> {
|
|
|
|
|
element: HTMLElement | null = null;
|
2019-02-14 15:18:55 +01:00
|
|
|
angularScope: AngularQueryComponentScope | null;
|
2019-01-15 11:40:12 +01:00
|
|
|
angularQueryEditor: AngularComponent | null = null;
|
2019-01-14 15:44:58 +01:00
|
|
|
|
2019-01-15 11:40:12 +01:00
|
|
|
state: State = {
|
|
|
|
|
datasource: null,
|
2019-01-16 14:00:29 +01:00
|
|
|
isCollapsed: false,
|
2019-01-21 18:54:57 +01:00
|
|
|
loadedDataSourceValue: undefined,
|
2019-02-14 15:18:55 +01:00
|
|
|
hasTextEditMode: false,
|
2019-09-23 11:13:33 +02:00
|
|
|
data: null,
|
2019-01-15 11:40:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.loadDatasource();
|
2019-02-14 15:18:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
if (this.angularQueryEditor) {
|
|
|
|
|
this.angularQueryEditor.destroy();
|
|
|
|
|
}
|
2019-01-15 11:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAngularQueryComponentScope(): AngularQueryComponentScope {
|
2019-02-19 18:53:07 +01:00
|
|
|
const { panel, query, dashboard } = this.props;
|
2019-01-15 11:40:12 +01:00
|
|
|
const { datasource } = this.state;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
datasource: datasource,
|
|
|
|
|
target: query,
|
|
|
|
|
panel: panel,
|
2019-02-19 18:53:07 +01:00
|
|
|
dashboard: dashboard,
|
2019-01-15 11:40:12 +01:00
|
|
|
refresh: () => panel.refresh(),
|
2019-01-18 11:20:55 +01:00
|
|
|
render: () => panel.render(),
|
2019-01-15 11:40:12 +01:00
|
|
|
events: panel.events,
|
2019-02-10 17:05:58 +01:00
|
|
|
range: getTimeSrv().timeRange(),
|
2019-01-15 11:40:12 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async loadDatasource() {
|
|
|
|
|
const { query, panel } = this.props;
|
|
|
|
|
const dataSourceSrv = getDatasourceSrv();
|
|
|
|
|
const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
|
|
|
|
|
|
2019-02-14 15:18:55 +01:00
|
|
|
this.setState({
|
|
|
|
|
datasource,
|
|
|
|
|
loadedDataSourceValue: this.props.dataSourceValue,
|
2019-08-02 07:42:39 +01:00
|
|
|
hasTextEditMode: _.has(datasource, 'components.QueryCtrl.prototype.toggleEditorMode'),
|
2019-02-14 15:18:55 +01:00
|
|
|
});
|
2019-01-15 11:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-17 23:22:14 -07:00
|
|
|
componentDidUpdate(prevProps: Props) {
|
2019-01-21 18:54:57 +01:00
|
|
|
const { loadedDataSourceValue } = this.state;
|
2019-09-12 17:28:46 +02:00
|
|
|
const { data, query, panel } = this.props;
|
2019-04-17 23:22:14 -07:00
|
|
|
|
|
|
|
|
if (data !== prevProps.data) {
|
2019-09-23 11:13:33 +02:00
|
|
|
this.setState({ data: filterPanelDataToQuery(data, query.refId) });
|
2019-04-17 23:22:14 -07:00
|
|
|
|
|
|
|
|
if (this.angularScope) {
|
|
|
|
|
this.angularScope.range = getTimeSrv().timeRange();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.angularQueryEditor) {
|
2019-09-12 17:28:46 +02:00
|
|
|
notifyAngularQueryEditorsOfData(panel, data, this.angularQueryEditor);
|
2019-04-17 23:22:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-15 11:40:12 +01:00
|
|
|
|
|
|
|
|
// check if we need to load another datasource
|
2019-01-21 18:54:57 +01:00
|
|
|
if (loadedDataSourceValue !== this.props.dataSourceValue) {
|
2019-01-15 11:40:12 +01: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 15:18:55 +01:00
|
|
|
this.angularScope = scopeProps.ctrl;
|
2019-01-15 11:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-16 14:00:29 +01:00
|
|
|
onToggleCollapse = () => {
|
|
|
|
|
this.setState({ isCollapsed: !this.state.isCollapsed });
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-23 17:44:22 +01:00
|
|
|
onRunQuery = () => {
|
2019-01-17 20:02:43 +01:00
|
|
|
this.props.panel.refresh();
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-16 17:53:40 +01:00
|
|
|
renderPluginEditor() {
|
2019-09-23 11:13:33 +02:00
|
|
|
const { query, onChange } = this.props;
|
|
|
|
|
const { datasource, data } = this.state;
|
2019-01-16 17:53:40 +01:00
|
|
|
|
2019-04-04 18:30:15 +02:00
|
|
|
if (datasource.components.QueryCtrl) {
|
2019-01-17 13:08:20 +01:00
|
|
|
return <div ref={element => (this.element = element)} />;
|
2019-01-16 17:53:40 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-04 18:30:15 +02:00
|
|
|
if (datasource.components.QueryEditor) {
|
|
|
|
|
const QueryEditor = datasource.components.QueryEditor;
|
|
|
|
|
|
2019-04-01 22:22:52 -07:00
|
|
|
return (
|
|
|
|
|
<QueryEditor
|
|
|
|
|
query={query}
|
|
|
|
|
datasource={datasource}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
onRunQuery={this.onRunQuery}
|
2019-09-23 11:13:33 +02:00
|
|
|
data={data}
|
2019-04-01 22:22:52 -07:00
|
|
|
/>
|
|
|
|
|
);
|
2019-01-16 17:53:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <div>Data source plugin does not export any Query Editor component</div>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onToggleEditMode = () => {
|
2019-02-14 15:18:55 +01:00
|
|
|
if (this.angularScope && this.angularScope.toggleEditorMode) {
|
|
|
|
|
this.angularScope.toggleEditorMode();
|
2019-01-16 17:53:40 +01:00
|
|
|
this.angularQueryEditor.digest();
|
|
|
|
|
}
|
2019-01-17 13:15:25 +01:00
|
|
|
|
|
|
|
|
if (this.state.isCollapsed) {
|
|
|
|
|
this.setState({ isCollapsed: false });
|
|
|
|
|
}
|
2019-01-17 13:08:20 +01:00
|
|
|
};
|
2019-01-16 17:53:40 +01:00
|
|
|
|
2019-01-17 10:26:08 +01: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 09:39:23 +01:00
|
|
|
this.onRunQuery();
|
2019-01-17 10:26:08 +01:00
|
|
|
this.forceUpdate();
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-17 13:08:20 +01:00
|
|
|
renderCollapsedText(): string | null {
|
2019-04-10 09:31:32 -07:00
|
|
|
const { datasource } = this.state;
|
|
|
|
|
if (datasource.getQueryDisplayText) {
|
|
|
|
|
return datasource.getQueryDisplayText(this.props.query);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 15:18:55 +01:00
|
|
|
if (this.angularScope && this.angularScope.getCollapsedText) {
|
|
|
|
|
return this.angularScope.getCollapsedText();
|
2019-01-17 13:08:20 +01:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 15:44:58 +01:00
|
|
|
render() {
|
2019-01-21 18:54:57 +01:00
|
|
|
const { query, inMixedMode } = this.props;
|
2019-02-14 15:18:55 +01:00
|
|
|
const { datasource, isCollapsed, hasTextEditMode } = this.state;
|
2019-01-17 10:26:08 +01: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 11:40:12 +01:00
|
|
|
|
|
|
|
|
if (!datasource) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-01-14 15:44:58 +01:00
|
|
|
|
2019-01-16 17:53:40 +01:00
|
|
|
return (
|
2019-01-17 10:26:08 +01:00
|
|
|
<div className={rowClasses}>
|
|
|
|
|
<div className="query-editor-row__header">
|
|
|
|
|
<div className="query-editor-row__ref-id" onClick={this.onToggleCollapse}>
|
2019-01-16 17:53:40 +01:00
|
|
|
{isCollapsed && <i className="fa fa-caret-right" />}
|
|
|
|
|
{!isCollapsed && <i className="fa fa-caret-down" />}
|
|
|
|
|
<span>{query.refId}</span>
|
2019-01-21 18:54:57 +01:00
|
|
|
{inMixedMode && <em className="query-editor-row__context-info"> ({datasource.name})</em>}
|
2019-01-17 10:26:08 +01:00
|
|
|
{isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
|
2019-01-16 14:00:29 +01:00
|
|
|
</div>
|
2019-01-19 08:22:56 +01:00
|
|
|
<div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
|
2019-01-17 13:08:20 +01:00
|
|
|
{isCollapsed && <div>{this.renderCollapsedText()}</div>}
|
|
|
|
|
</div>
|
2019-01-17 10:26:08 +01:00
|
|
|
<div className="query-editor-row__actions">
|
2019-02-14 15:18:55 +01:00
|
|
|
{hasTextEditMode && (
|
2019-01-17 13:08:20 +01:00
|
|
|
<button
|
|
|
|
|
className="query-editor-row__action"
|
|
|
|
|
onClick={this.onToggleEditMode}
|
|
|
|
|
title="Toggle text edit mode"
|
|
|
|
|
>
|
2019-01-16 17:53:40 +01:00
|
|
|
<i className="fa fa-fw fa-pencil" />
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
2019-01-17 10:26:08 +01:00
|
|
|
<button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, 1)}>
|
2019-01-16 17:53:40 +01:00
|
|
|
<i className="fa fa-fw fa-arrow-down" />
|
|
|
|
|
</button>
|
2019-01-17 10:26:08 +01:00
|
|
|
<button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, -1)}>
|
2019-01-16 17:53:40 +01:00
|
|
|
<i className="fa fa-fw fa-arrow-up" />
|
|
|
|
|
</button>
|
2019-01-17 10:26:08 +01:00
|
|
|
<button className="query-editor-row__action" onClick={this.onCopyQuery} title="Duplicate query">
|
2019-01-16 17:53:40 +01:00
|
|
|
<i className="fa fa-fw fa-copy" />
|
|
|
|
|
</button>
|
2019-01-17 10:26:08 +01: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 17:53:40 +01:00
|
|
|
</button>
|
2019-01-17 10:26:08 +01:00
|
|
|
<button className="query-editor-row__action" onClick={this.onRemoveQuery} title="Remove query">
|
2019-01-16 17:53:40 +01:00
|
|
|
<i className="fa fa-fw fa-trash" />
|
|
|
|
|
</button>
|
2019-01-16 14:00:29 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2019-09-02 20:45:18 +02:00
|
|
|
<div className={bodyClasses}>
|
2019-09-04 13:59:30 +02:00
|
|
|
<ErrorBoundaryAlert>{this.renderPluginEditor()}</ErrorBoundaryAlert>
|
2019-09-02 20:45:18 +02:00
|
|
|
</div>
|
2019-01-16 17:53:40 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
2019-01-14 15:44:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-15 11:40:12 +01:00
|
|
|
|
2019-09-12 17:28:46 +02:00
|
|
|
// To avoid sending duplicate events for each row we have this global cached object here
|
|
|
|
|
// So we can check if we already emitted this legacy data event
|
|
|
|
|
let globalLastPanelDataCache: PanelData = null;
|
|
|
|
|
|
|
|
|
|
function notifyAngularQueryEditorsOfData(panel: PanelModel, data: PanelData, editor: AngularComponent) {
|
|
|
|
|
if (data === globalLastPanelDataCache) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
globalLastPanelDataCache = data;
|
|
|
|
|
|
2019-09-13 10:09:25 +02:00
|
|
|
if (data.state === LoadingState.Done) {
|
|
|
|
|
const legacy = data.series.map(v => toLegacyResponseData(v));
|
|
|
|
|
panel.events.emit('data-received', legacy);
|
|
|
|
|
} else if (data.state === LoadingState.Error) {
|
|
|
|
|
panel.events.emit('data-error', data.error);
|
|
|
|
|
}
|
2019-09-12 17:28:46 +02:00
|
|
|
|
|
|
|
|
// Some query controllers listen to data error events and need a digest
|
|
|
|
|
// for some reason this needs to be done in next tick
|
|
|
|
|
setTimeout(editor.digest);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-15 11:40:12 +01:00
|
|
|
export interface AngularQueryComponentScope {
|
|
|
|
|
target: DataQuery;
|
|
|
|
|
panel: PanelModel;
|
2019-02-19 18:53:07 +01:00
|
|
|
dashboard: DashboardModel;
|
2019-01-15 11:40:12 +01:00
|
|
|
events: Emitter;
|
|
|
|
|
refresh: () => void;
|
|
|
|
|
render: () => void;
|
|
|
|
|
datasource: DataSourceApi;
|
2019-01-16 17:53:40 +01:00
|
|
|
toggleEditorMode?: () => void;
|
2019-01-17 13:08:20 +01:00
|
|
|
getCollapsedText?: () => string;
|
2019-02-10 17:05:58 +01:00
|
|
|
range: TimeRange;
|
2019-01-15 11:40:12 +01:00
|
|
|
}
|
2019-04-20 12:26:49 -07: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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 02:19:17 -07:00
|
|
|
const timeRange = data.timeRange;
|
|
|
|
|
|
2019-04-20 12:26:49 -07:00
|
|
|
return {
|
|
|
|
|
state,
|
|
|
|
|
series,
|
|
|
|
|
request,
|
|
|
|
|
error,
|
2019-09-25 02:19:17 -07:00
|
|
|
timeRange,
|
2019-04-20 12:26:49 -07:00
|
|
|
};
|
|
|
|
|
}
|