| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- // Libraries
- import React, { PureComponent } from 'react';
- import classNames from 'classnames';
- import _ from 'lodash';
- // Utils & Services
- import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
- import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
- import { Emitter } from 'app/core/utils/emitter';
- import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
- // Types
- import { PanelModel } from '../state/PanelModel';
- import { DataQuery, DataSourceApi, TimeRange, DataQueryError, SeriesData } from '@grafana/ui';
- import { DashboardModel } from '../state/DashboardModel';
- interface Props {
- panel: PanelModel;
- query: DataQuery;
- dashboard: DashboardModel;
- onAddQuery: (query?: DataQuery) => void;
- onRemoveQuery: (query: DataQuery) => void;
- onMoveQuery: (query: DataQuery, direction: number) => void;
- onChange: (query: DataQuery) => void;
- dataSourceValue: string | null;
- inMixedMode: boolean;
- }
- interface State {
- loadedDataSourceValue: string | null | undefined;
- datasource: DataSourceApi | null;
- isCollapsed: boolean;
- hasTextEditMode: boolean;
- queryError: DataQueryError | null;
- queryResponse: SeriesData[] | null;
- }
- export class QueryEditorRow extends PureComponent<Props, State> {
- element: HTMLElement | null = null;
- angularScope: AngularQueryComponentScope | null;
- angularQueryEditor: AngularComponent | null = null;
- state: State = {
- datasource: null,
- isCollapsed: false,
- loadedDataSourceValue: undefined,
- hasTextEditMode: false,
- queryError: null,
- queryResponse: null,
- };
- componentDidMount() {
- this.loadDatasource();
- this.props.panel.events.on('refresh', this.onPanelRefresh);
- this.props.panel.events.on('data-error', this.onPanelDataError);
- this.props.panel.events.on('data-received', this.onPanelDataReceived);
- this.props.panel.events.on('series-data-received', this.onSeriesDataReceived);
- }
- componentWillUnmount() {
- this.props.panel.events.off('refresh', this.onPanelRefresh);
- this.props.panel.events.off('data-error', this.onPanelDataError);
- this.props.panel.events.off('data-received', this.onPanelDataReceived);
- this.props.panel.events.off('series-data-received', this.onSeriesDataReceived);
- if (this.angularQueryEditor) {
- this.angularQueryEditor.destroy();
- }
- }
- onPanelDataError = (error: DataQueryError) => {
- // Some query controllers listen to data error events and need a digest
- if (this.angularQueryEditor) {
- // for some reason this needs to be done in next tick
- setTimeout(this.angularQueryEditor.digest);
- return;
- }
- // if error relates to this query store it in state and pass it on to query editor
- if (error.refId === this.props.query.refId) {
- this.setState({ queryError: error });
- }
- };
- // Only used by angular plugins
- onPanelDataReceived = () => {
- // Some query controllers listen to data error events and need a digest
- if (this.angularQueryEditor) {
- // for some reason this needs to be done in next tick
- setTimeout(this.angularQueryEditor.digest);
- }
- };
- // Only used by the React Query Editors
- onSeriesDataReceived = (data: SeriesData[]) => {
- if (!this.angularQueryEditor) {
- // only pass series related to this query to query editor
- const filterByRefId = data.filter(series => series.refId === this.props.query.refId);
- this.setState({ queryResponse: filterByRefId, queryError: null });
- }
- };
- onPanelRefresh = () => {
- if (this.angularScope) {
- this.angularScope.range = getTimeSrv().timeRange();
- }
- };
- getAngularQueryComponentScope(): AngularQueryComponentScope {
- const { panel, query, dashboard } = this.props;
- const { datasource } = this.state;
- return {
- datasource: datasource,
- target: query,
- panel: panel,
- dashboard: dashboard,
- refresh: () => panel.refresh(),
- render: () => panel.render(),
- events: panel.events,
- range: getTimeSrv().timeRange(),
- };
- }
- async loadDatasource() {
- const { query, panel } = this.props;
- const dataSourceSrv = getDatasourceSrv();
- const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
- this.setState({
- datasource,
- loadedDataSourceValue: this.props.dataSourceValue,
- hasTextEditMode: false,
- });
- }
- componentDidUpdate() {
- const { loadedDataSourceValue } = this.state;
- // check if we need to load another datasource
- if (loadedDataSourceValue !== this.props.dataSourceValue) {
- 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);
- this.angularScope = scopeProps.ctrl;
- // give angular time to compile
- setTimeout(() => {
- this.setState({ hasTextEditMode: !!this.angularScope.toggleEditorMode });
- }, 100);
- }
- onToggleCollapse = () => {
- this.setState({ isCollapsed: !this.state.isCollapsed });
- };
- onRunQuery = () => {
- this.props.panel.refresh();
- };
- renderPluginEditor() {
- const { query, onChange } = this.props;
- const { datasource, queryResponse, queryError } = this.state;
- if (datasource.components.QueryCtrl) {
- return <div ref={element => (this.element = element)} />;
- }
- if (datasource.components.QueryEditor) {
- const QueryEditor = datasource.components.QueryEditor;
- return (
- <QueryEditor
- query={query}
- datasource={datasource}
- onChange={onChange}
- onRunQuery={this.onRunQuery}
- queryResponse={queryResponse}
- queryError={queryError}
- />
- );
- }
- return <div>Data source plugin does not export any Query Editor component</div>;
- }
- onToggleEditMode = () => {
- if (this.angularScope && this.angularScope.toggleEditorMode) {
- this.angularScope.toggleEditorMode();
- this.angularQueryEditor.digest();
- }
- if (this.state.isCollapsed) {
- this.setState({ isCollapsed: false });
- }
- };
- 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;
- this.onRunQuery();
- this.forceUpdate();
- };
- renderCollapsedText(): string | null {
- if (this.angularScope && this.angularScope.getCollapsedText) {
- return this.angularScope.getCollapsedText();
- }
- return null;
- }
- render() {
- const { query, inMixedMode } = this.props;
- const { datasource, isCollapsed, hasTextEditMode } = this.state;
- 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,
- });
- if (!datasource) {
- return null;
- }
- return (
- <div className={rowClasses}>
- <div className="query-editor-row__header">
- <div className="query-editor-row__ref-id" onClick={this.onToggleCollapse}>
- {isCollapsed && <i className="fa fa-caret-right" />}
- {!isCollapsed && <i className="fa fa-caret-down" />}
- <span>{query.refId}</span>
- {inMixedMode && <em className="query-editor-row__context-info"> ({datasource.name})</em>}
- {isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
- </div>
- <div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
- {isCollapsed && <div>{this.renderCollapsedText()}</div>}
- </div>
- <div className="query-editor-row__actions">
- {hasTextEditMode && (
- <button
- className="query-editor-row__action"
- onClick={this.onToggleEditMode}
- title="Toggle text edit mode"
- >
- <i className="fa fa-fw fa-pencil" />
- </button>
- )}
- <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, 1)}>
- <i className="fa fa-fw fa-arrow-down" />
- </button>
- <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, -1)}>
- <i className="fa fa-fw fa-arrow-up" />
- </button>
- <button className="query-editor-row__action" onClick={this.onCopyQuery} title="Duplicate query">
- <i className="fa fa-fw fa-copy" />
- </button>
- <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" />}
- </button>
- <button className="query-editor-row__action" onClick={this.onRemoveQuery} title="Remove query">
- <i className="fa fa-fw fa-trash" />
- </button>
- </div>
- </div>
- <div className={bodyClasses}>{this.renderPluginEditor()}</div>
- </div>
- );
- }
- }
- export interface AngularQueryComponentScope {
- target: DataQuery;
- panel: PanelModel;
- dashboard: DashboardModel;
- events: Emitter;
- refresh: () => void;
- render: () => void;
- datasource: DataSourceApi;
- toggleEditorMode?: () => void;
- getCollapsedText?: () => string;
- range: TimeRange;
- }
|