| 
									
										
										
										
											2019-01-14 22:44:58 +08:00
										 |  |  | // Libraries
 | 
					
						
							| 
									
										
										
										
											2019-01-16 21:00:29 +08:00
										 |  |  | import classNames from 'classnames'; | 
					
						
							| 
									
										
										
										
											2022-06-28 19:38:31 +08:00
										 |  |  | import { cloneDeep, filter, has, uniqBy } from 'lodash'; | 
					
						
							|  |  |  | import pluralize from 'pluralize'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | import React, { PureComponent, ReactNode } from 'react'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-14 22:44:58 +08:00
										 |  |  | // Utils & Services
 | 
					
						
							| 
									
										
										
										
											2019-10-31 17:48:05 +08:00
										 |  |  | import { | 
					
						
							| 
									
										
										
										
											2021-09-15 23:26:23 +08:00
										 |  |  |   CoreApp, | 
					
						
							| 
									
										
										
										
											2019-10-31 17:48:05 +08:00
										 |  |  |   DataQuery, | 
					
						
							|  |  |  |   DataSourceApi, | 
					
						
							| 
									
										
										
										
											2021-03-19 13:37:17 +08:00
										 |  |  |   DataSourceInstanceSettings, | 
					
						
							|  |  |  |   EventBusExtended, | 
					
						
							|  |  |  |   EventBusSrv, | 
					
						
							| 
									
										
										
										
											2021-09-15 23:26:23 +08:00
										 |  |  |   HistoryItem, | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |   LoadingState, | 
					
						
							| 
									
										
										
										
											2019-10-31 17:48:05 +08:00
										 |  |  |   PanelData, | 
					
						
							|  |  |  |   PanelEvents, | 
					
						
							| 
									
										
										
										
											2022-06-28 19:38:31 +08:00
										 |  |  |   QueryResultMetaNotice, | 
					
						
							| 
									
										
										
										
											2019-10-31 17:48:05 +08:00
										 |  |  |   TimeRange, | 
					
						
							|  |  |  |   toLegacyResponseData, | 
					
						
							|  |  |  | } from '@grafana/data'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | import { selectors } from '@grafana/e2e-selectors'; | 
					
						
							|  |  |  | import { AngularComponent, getAngularLoader } from '@grafana/runtime'; | 
					
						
							| 
									
										
										
										
											2022-06-28 19:38:31 +08:00
										 |  |  | import { Badge, ErrorBoundaryAlert, HorizontalGroup } from '@grafana/ui'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | import { OperationRowHelp } from 'app/core/components/QueryOperationRow/OperationRowHelp'; | 
					
						
							|  |  |  | import { QueryOperationAction } from 'app/core/components/QueryOperationRow/QueryOperationAction'; | 
					
						
							| 
									
										
										
										
											2021-01-07 22:33:15 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   QueryOperationRow, | 
					
						
							|  |  |  |   QueryOperationRowRenderProps, | 
					
						
							|  |  |  | } from 'app/core/components/QueryOperationRow/QueryOperationRow'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv'; | 
					
						
							| 
									
										
										
										
											2021-08-31 18:55:05 +08:00
										 |  |  | import { DashboardModel } from 'app/features/dashboard/state/DashboardModel'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | import { PanelModel } from 'app/features/dashboard/state/PanelModel'; | 
					
						
							|  |  |  | import { getDatasourceSrv } from 'app/features/plugins/datasource_srv'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 22:36:04 +08:00
										 |  |  | import { RowActionComponents } from './QueryActionComponent'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | import { QueryEditorRowHeader } from './QueryEditorRowHeader'; | 
					
						
							| 
									
										
										
										
											2022-04-14 18:57:56 +08:00
										 |  |  | import { QueryErrorAlert } from './QueryErrorAlert'; | 
					
						
							| 
									
										
										
										
											2019-01-14 22:44:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  | interface Props<TQuery extends DataQuery> { | 
					
						
							| 
									
										
										
										
											2019-04-18 14:22:14 +08:00
										 |  |  |   data: PanelData; | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  |   query: TQuery; | 
					
						
							|  |  |  |   queries: TQuery[]; | 
					
						
							| 
									
										
										
										
											2020-08-31 14:47:27 +08:00
										 |  |  |   id: string; | 
					
						
							|  |  |  |   index: number; | 
					
						
							| 
									
										
										
										
											2021-04-29 21:10:14 +08:00
										 |  |  |   dataSource: DataSourceInstanceSettings; | 
					
						
							|  |  |  |   onChangeDataSource?: (dsSettings: DataSourceInstanceSettings) => void; | 
					
						
							| 
									
										
										
										
											2021-05-12 23:51:31 +08:00
										 |  |  |   renderHeaderExtras?: () => ReactNode; | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  |   onAddQuery: (query: TQuery) => void; | 
					
						
							|  |  |  |   onRemoveQuery: (query: TQuery) => void; | 
					
						
							|  |  |  |   onChange: (query: TQuery) => void; | 
					
						
							| 
									
										
										
										
											2020-12-02 22:42:54 +08:00
										 |  |  |   onRunQuery: () => void; | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  |   visualization?: ReactNode; | 
					
						
							|  |  |  |   hideDisableQuery?: boolean; | 
					
						
							| 
									
										
										
										
											2021-09-15 23:26:23 +08:00
										 |  |  |   app?: CoreApp; | 
					
						
							|  |  |  |   history?: Array<HistoryItem<TQuery>>; | 
					
						
							|  |  |  |   eventBus?: EventBusExtended; | 
					
						
							| 
									
										
										
										
											2022-01-08 01:27:20 +08:00
										 |  |  |   alerting?: boolean; | 
					
						
							| 
									
										
										
										
											2022-09-20 18:50:23 +08:00
										 |  |  |   onQueryCopied?: () => void; | 
					
						
							|  |  |  |   onQueryRemoved?: () => void; | 
					
						
							|  |  |  |   onQueryToggled?: (queryStatus?: boolean | undefined) => void; | 
					
						
							| 
									
										
										
										
											2019-01-14 22:44:58 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  | interface State<TQuery extends DataQuery> { | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |   loadedDataSourceIdentifier?: string | null; | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  |   datasource: DataSourceApi<TQuery> | null; | 
					
						
							| 
									
										
										
										
											2019-02-14 22:18:55 +08:00
										 |  |  |   hasTextEditMode: boolean; | 
					
						
							| 
									
										
										
										
											2019-09-23 17:13:33 +08:00
										 |  |  |   data?: PanelData; | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |   isOpen?: boolean; | 
					
						
							| 
									
										
										
										
											2021-01-20 06:52:09 +08:00
										 |  |  |   showingHelp: boolean; | 
					
						
							| 
									
										
										
										
											2019-01-14 22:44:58 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  | export class QueryEditorRow<TQuery extends DataQuery> extends PureComponent<Props<TQuery>, State<TQuery>> { | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |   element: HTMLElement | null = null; | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  |   angularScope: AngularQueryComponentScope<TQuery> | null = null; | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |   angularQueryEditor: AngularComponent | null = null; | 
					
						
							| 
									
										
										
										
											2019-01-14 22:44:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  |   state: State<TQuery> = { | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |     datasource: null, | 
					
						
							| 
									
										
										
										
											2019-02-14 22:18:55 +08:00
										 |  |  |     hasTextEditMode: false, | 
					
						
							| 
									
										
										
										
											2020-05-15 02:06:20 +08:00
										 |  |  |     data: undefined, | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |     isOpen: true, | 
					
						
							| 
									
										
										
										
											2021-01-20 06:52:09 +08:00
										 |  |  |     showingHelp: false, | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-23 21:20:33 +08:00
										 |  |  |   async componentDidMount() { | 
					
						
							| 
									
										
										
										
											2022-08-29 21:49:58 +08:00
										 |  |  |     const { data, query } = this.props; | 
					
						
							|  |  |  |     const dataFilteredByRefId = filterPanelDataToQuery(data, query.refId); | 
					
						
							|  |  |  |     this.setState({ data: dataFilteredByRefId }); | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |     this.loadDatasource(); | 
					
						
							| 
									
										
										
										
											2019-02-14 22:18:55 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   componentWillUnmount() { | 
					
						
							|  |  |  |     if (this.angularQueryEditor) { | 
					
						
							|  |  |  |       this.angularQueryEditor.destroy(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  |   getAngularQueryComponentScope(): AngularQueryComponentScope<TQuery> { | 
					
						
							| 
									
										
										
										
											2021-02-15 18:24:03 +08:00
										 |  |  |     const { query, queries } = this.props; | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |     const { datasource } = this.state; | 
					
						
							| 
									
										
										
										
											2021-01-11 20:20:20 +08:00
										 |  |  |     const panel = new PanelModel({ targets: queries }); | 
					
						
							| 
									
										
										
										
											2020-12-02 22:42:54 +08:00
										 |  |  |     const dashboard = {} as DashboardModel; | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 16:49:59 +08:00
										 |  |  |     const me = this; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |     return { | 
					
						
							|  |  |  |       datasource: datasource, | 
					
						
							|  |  |  |       target: query, | 
					
						
							|  |  |  |       panel: panel, | 
					
						
							| 
									
										
										
										
											2020-12-02 22:42:54 +08:00
										 |  |  |       dashboard: dashboard, | 
					
						
							|  |  |  |       refresh: () => { | 
					
						
							|  |  |  |         // Old angular editors modify the query model and just call refresh
 | 
					
						
							| 
									
										
										
										
											2021-10-04 14:05:43 +08:00
										 |  |  |         // Important that this use this.props here so that as this function is only created on mount and it's
 | 
					
						
							| 
									
										
										
										
											2021-02-15 18:24:03 +08:00
										 |  |  |         // important not to capture old prop functions in this closure
 | 
					
						
							| 
									
										
										
										
											2021-02-19 16:49:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-04 14:05:43 +08:00
										 |  |  |         // the "hide" attribute of the queries can be changed from the "outside",
 | 
					
						
							| 
									
										
										
										
											2021-02-19 16:49:59 +08:00
										 |  |  |         // it will be applied to "this.props.query.hide", but not to "query.hide".
 | 
					
						
							|  |  |  |         // so we have to apply it.
 | 
					
						
							|  |  |  |         if (query.hide !== me.props.query.hide) { | 
					
						
							|  |  |  |           query.hide = me.props.query.hide; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-15 18:24:03 +08:00
										 |  |  |         this.props.onChange(query); | 
					
						
							|  |  |  |         this.props.onRunQuery(); | 
					
						
							| 
									
										
										
										
											2020-12-02 22:42:54 +08:00
										 |  |  |       }, | 
					
						
							| 
									
										
										
										
											2020-11-27 01:12:02 +08:00
										 |  |  |       render: () => () => console.log('legacy render function called, it does nothing'), | 
					
						
							| 
									
										
										
										
											2021-09-15 23:26:23 +08:00
										 |  |  |       events: this.props.eventBus || new EventBusSrv(), | 
					
						
							| 
									
										
										
										
											2019-02-11 00:05:58 +08:00
										 |  |  |       range: getTimeSrv().timeRange(), | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |   getQueryDataSourceIdentifier(): string | null | undefined { | 
					
						
							| 
									
										
										
										
											2021-04-29 21:10:14 +08:00
										 |  |  |     const { query, dataSource: dsSettings } = this.props; | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  |     return query.datasource?.uid ?? dsSettings.uid; | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |   async loadDatasource() { | 
					
						
							|  |  |  |     const dataSourceSrv = getDatasourceSrv(); | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |     let datasource: DataSourceApi; | 
					
						
							|  |  |  |     const dataSourceIdentifier = this.getQueryDataSourceIdentifier(); | 
					
						
							| 
									
										
										
										
											2020-05-12 14:03:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       datasource = await dataSourceSrv.get(dataSourceIdentifier); | 
					
						
							| 
									
										
										
										
											2020-05-12 14:03:52 +08:00
										 |  |  |     } catch (error) { | 
					
						
							|  |  |  |       datasource = await dataSourceSrv.get(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-14 22:18:55 +08:00
										 |  |  |     this.setState({ | 
					
						
							| 
									
										
										
										
											2022-02-02 20:02:32 +08:00
										 |  |  |       datasource: datasource as unknown as DataSourceApi<TQuery>, | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |       loadedDataSourceIdentifier: dataSourceIdentifier, | 
					
						
							| 
									
										
										
										
											2021-04-21 15:38:00 +08:00
										 |  |  |       hasTextEditMode: has(datasource, 'components.QueryCtrl.prototype.toggleEditorMode'), | 
					
						
							| 
									
										
										
										
											2019-02-14 22:18:55 +08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  |   componentDidUpdate(prevProps: Props<TQuery>) { | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |     const { datasource, loadedDataSourceIdentifier } = this.state; | 
					
						
							| 
									
										
										
										
											2020-12-02 22:42:54 +08:00
										 |  |  |     const { data, query } = this.props; | 
					
						
							| 
									
										
										
										
											2019-04-18 14:22:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (data !== prevProps.data) { | 
					
						
							| 
									
										
										
										
											2021-02-16 20:10:40 +08:00
										 |  |  |       const dataFilteredByRefId = filterPanelDataToQuery(data, query.refId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       this.setState({ data: dataFilteredByRefId }); | 
					
						
							| 
									
										
										
										
											2019-04-18 14:22:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (this.angularScope) { | 
					
						
							|  |  |  |         this.angularScope.range = getTimeSrv().timeRange(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-16 20:10:40 +08:00
										 |  |  |       if (this.angularQueryEditor && dataFilteredByRefId) { | 
					
						
							|  |  |  |         notifyAngularQueryEditorsOfData(this.angularScope!, dataFilteredByRefId, this.angularQueryEditor); | 
					
						
							| 
									
										
										
										
											2019-04-18 14:22:14 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // check if we need to load another datasource
 | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  |     if (datasource && loadedDataSourceIdentifier !== this.getQueryDataSourceIdentifier()) { | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |       if (this.angularQueryEditor) { | 
					
						
							|  |  |  |         this.angularQueryEditor.destroy(); | 
					
						
							|  |  |  |         this.angularQueryEditor = null; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       this.loadDatasource(); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!this.element || this.angularQueryEditor) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |     this.renderAngularQueryEditor(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |   renderAngularQueryEditor = () => { | 
					
						
							|  |  |  |     if (!this.element) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-27 01:12:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |     if (this.angularQueryEditor) { | 
					
						
							|  |  |  |       this.angularQueryEditor.destroy(); | 
					
						
							|  |  |  |       this.angularQueryEditor = null; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-27 01:12:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |     const loader = getAngularLoader(); | 
					
						
							|  |  |  |     const template = '<plugin-component type="query-ctrl" />'; | 
					
						
							|  |  |  |     const scopeProps = { ctrl: this.getAngularQueryComponentScope() }; | 
					
						
							| 
									
										
										
										
											2020-11-27 01:12:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |     this.angularQueryEditor = loader.load(this.element, scopeProps, template); | 
					
						
							| 
									
										
										
										
											2019-02-14 22:18:55 +08:00
										 |  |  |     this.angularScope = scopeProps.ctrl; | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |   onOpen = () => { | 
					
						
							|  |  |  |     this.renderAngularQueryEditor(); | 
					
						
							| 
									
										
										
										
											2019-01-16 21:00:29 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 23:26:23 +08:00
										 |  |  |   getReactQueryEditor(ds: DataSourceApi<TQuery>) { | 
					
						
							|  |  |  |     if (!ds) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (this.props.app) { | 
					
						
							|  |  |  |       case CoreApp.Explore: | 
					
						
							|  |  |  |         return ( | 
					
						
							|  |  |  |           ds.components?.ExploreMetricsQueryField || | 
					
						
							|  |  |  |           ds.components?.ExploreLogsQueryField || | 
					
						
							|  |  |  |           ds.components?.ExploreQueryField || | 
					
						
							|  |  |  |           ds.components?.QueryEditor | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-10-05 14:07:57 +08:00
										 |  |  |       case CoreApp.PanelEditor: | 
					
						
							| 
									
										
										
										
											2021-09-15 23:26:23 +08:00
										 |  |  |       case CoreApp.Dashboard: | 
					
						
							|  |  |  |       default: | 
					
						
							|  |  |  |         return ds.components?.QueryEditor; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |   renderPluginEditor = () => { | 
					
						
							| 
									
										
										
										
											2022-09-13 19:09:31 +08:00
										 |  |  |     const { query, onChange, queries, onRunQuery, onAddQuery, app = CoreApp.PanelEditor, history } = this.props; | 
					
						
							| 
									
										
										
										
											2019-09-23 17:13:33 +08:00
										 |  |  |     const { datasource, data } = this.state; | 
					
						
							| 
									
										
										
										
											2019-01-17 00:53:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-15 02:06:20 +08:00
										 |  |  |     if (datasource?.components?.QueryCtrl) { | 
					
						
							| 
									
										
										
										
											2021-01-20 14:59:48 +08:00
										 |  |  |       return <div ref={(element) => (this.element = element)} />; | 
					
						
							| 
									
										
										
										
											2019-01-17 00:53:40 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-15 23:26:23 +08:00
										 |  |  |     if (datasource) { | 
					
						
							|  |  |  |       let QueryEditor = this.getReactQueryEditor(datasource); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (QueryEditor) { | 
					
						
							|  |  |  |         return ( | 
					
						
							|  |  |  |           <QueryEditor | 
					
						
							|  |  |  |             key={datasource?.name} | 
					
						
							|  |  |  |             query={query} | 
					
						
							|  |  |  |             datasource={datasource} | 
					
						
							|  |  |  |             onChange={onChange} | 
					
						
							|  |  |  |             onRunQuery={onRunQuery} | 
					
						
							| 
									
										
										
										
											2022-09-13 19:09:31 +08:00
										 |  |  |             onAddQuery={onAddQuery} | 
					
						
							| 
									
										
										
										
											2021-09-15 23:26:23 +08:00
										 |  |  |             data={data} | 
					
						
							|  |  |  |             range={getTimeSrv().timeRange()} | 
					
						
							|  |  |  |             queries={queries} | 
					
						
							|  |  |  |             app={app} | 
					
						
							|  |  |  |             history={history} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-01-17 00:53:40 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return <div>Data source plugin does not export any Query Editor component</div>; | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-01-17 00:53:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 22:33:15 +08:00
										 |  |  |   onToggleEditMode = (e: React.MouseEvent, props: QueryOperationRowRenderProps) => { | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |     e.stopPropagation(); | 
					
						
							| 
									
										
										
										
											2019-02-14 22:18:55 +08:00
										 |  |  |     if (this.angularScope && this.angularScope.toggleEditorMode) { | 
					
						
							|  |  |  |       this.angularScope.toggleEditorMode(); | 
					
						
							| 
									
										
										
										
											2020-05-15 02:06:20 +08:00
										 |  |  |       this.angularQueryEditor?.digest(); | 
					
						
							| 
									
										
										
										
											2021-01-07 22:33:15 +08:00
										 |  |  |       if (!props.isOpen) { | 
					
						
							|  |  |  |         props.onOpen(); | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-01-17 20:15:25 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-17 20:08:20 +08:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-01-17 00:53:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 17:26:08 +08:00
										 |  |  |   onRemoveQuery = () => { | 
					
						
							| 
									
										
										
										
											2022-09-20 18:50:23 +08:00
										 |  |  |     const { onRemoveQuery, query, onQueryRemoved } = this.props; | 
					
						
							|  |  |  |     onRemoveQuery(query); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (onQueryRemoved) { | 
					
						
							|  |  |  |       onQueryRemoved(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-17 17:26:08 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onCopyQuery = () => { | 
					
						
							| 
									
										
										
										
											2022-09-20 18:50:23 +08:00
										 |  |  |     const { query, onAddQuery, onQueryCopied } = this.props; | 
					
						
							|  |  |  |     const copy = cloneDeep(query); | 
					
						
							|  |  |  |     onAddQuery(copy); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (onQueryCopied) { | 
					
						
							|  |  |  |       onQueryCopied(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-17 17:26:08 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   onDisableQuery = () => { | 
					
						
							| 
									
										
										
										
											2022-09-20 18:50:23 +08:00
										 |  |  |     const { query, onChange, onRunQuery, onQueryToggled } = this.props; | 
					
						
							|  |  |  |     onChange({ ...query, hide: !query.hide }); | 
					
						
							|  |  |  |     onRunQuery(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (onQueryToggled) { | 
					
						
							|  |  |  |       onQueryToggled(query.hide); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-17 17:26:08 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-20 06:52:09 +08:00
										 |  |  |   onToggleHelp = () => { | 
					
						
							| 
									
										
										
										
											2021-01-20 15:59:11 +08:00
										 |  |  |     this.setState((state) => ({ | 
					
						
							| 
									
										
										
										
											2021-01-20 06:52:09 +08:00
										 |  |  |       showingHelp: !state.showingHelp, | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  |   onClickExample = (query: TQuery) => { | 
					
						
							| 
									
										
										
										
											2021-01-20 06:52:09 +08:00
										 |  |  |     this.props.onChange({ | 
					
						
							|  |  |  |       ...query, | 
					
						
							|  |  |  |       refId: this.props.query.refId, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     this.onToggleHelp(); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 20:08:20 +08:00
										 |  |  |   renderCollapsedText(): string | null { | 
					
						
							| 
									
										
										
										
											2019-04-11 00:31:32 +08:00
										 |  |  |     const { datasource } = this.state; | 
					
						
							| 
									
										
										
										
											2020-05-15 02:06:20 +08:00
										 |  |  |     if (datasource?.getQueryDisplayText) { | 
					
						
							| 
									
										
										
										
											2019-04-11 00:31:32 +08:00
										 |  |  |       return datasource.getQueryDisplayText(this.props.query); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-14 22:18:55 +08:00
										 |  |  |     if (this.angularScope && this.angularScope.getCollapsedText) { | 
					
						
							|  |  |  |       return this.angularScope.getCollapsedText(); | 
					
						
							| 
									
										
										
										
											2019-01-17 20:08:20 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return null; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-28 19:38:31 +08:00
										 |  |  |   renderWarnings = (): JSX.Element | null => { | 
					
						
							|  |  |  |     const { data, query } = this.props; | 
					
						
							|  |  |  |     const dataFilteredByRefId = filterPanelDataToQuery(data, query.refId)?.series ?? []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const allWarnings = dataFilteredByRefId.reduce((acc: QueryResultMetaNotice[], serie) => { | 
					
						
							|  |  |  |       if (!serie.meta?.notices) { | 
					
						
							|  |  |  |         return acc; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const warnings = filter(serie.meta.notices, { severity: 'warning' }) ?? []; | 
					
						
							|  |  |  |       return acc.concat(warnings); | 
					
						
							|  |  |  |     }, []); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const uniqueWarnings = uniqBy(allWarnings, 'text'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const hasWarnings = uniqueWarnings.length > 0; | 
					
						
							|  |  |  |     if (!hasWarnings) { | 
					
						
							|  |  |  |       return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const serializedWarnings = uniqueWarnings.map((warning) => warning.text).join('\n'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ( | 
					
						
							|  |  |  |       <Badge | 
					
						
							|  |  |  |         color="orange" | 
					
						
							|  |  |  |         icon="exclamation-triangle" | 
					
						
							|  |  |  |         text={ | 
					
						
							|  |  |  |           <> | 
					
						
							|  |  |  |             {uniqueWarnings.length} {pluralize('warning', uniqueWarnings.length)} | 
					
						
							|  |  |  |           </> | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         tooltip={serializedWarnings} | 
					
						
							|  |  |  |       /> | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 22:36:04 +08:00
										 |  |  |   renderExtraActions = () => { | 
					
						
							| 
									
										
										
										
											2021-09-27 21:40:19 +08:00
										 |  |  |     const { query, queries, data, onAddQuery, dataSource } = this.props; | 
					
						
							| 
									
										
										
										
											2022-06-28 19:38:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const extraActions = RowActionComponents.getAllExtraRenderAction() | 
					
						
							| 
									
										
										
										
											2021-11-17 19:05:39 +08:00
										 |  |  |       .map((action, index) => | 
					
						
							|  |  |  |         action({ | 
					
						
							|  |  |  |           query, | 
					
						
							|  |  |  |           queries, | 
					
						
							|  |  |  |           timeRange: data.timeRange, | 
					
						
							|  |  |  |           onAddQuery: onAddQuery as (query: DataQuery) => void, | 
					
						
							|  |  |  |           dataSource, | 
					
						
							|  |  |  |           key: index, | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       ) | 
					
						
							|  |  |  |       .filter(Boolean); | 
					
						
							| 
									
										
										
										
											2022-06-28 19:38:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     extraActions.push(this.renderWarnings()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return extraActions; | 
					
						
							| 
									
										
										
										
											2021-09-20 22:36:04 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-07 22:33:15 +08:00
										 |  |  |   renderActions = (props: QueryOperationRowRenderProps) => { | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  |     const { query, hideDisableQuery = false } = this.props; | 
					
						
							| 
									
										
										
										
											2021-07-12 22:42:04 +08:00
										 |  |  |     const { hasTextEditMode, datasource, showingHelp } = this.state; | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |     const isDisabled = query.hide; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-20 06:52:09 +08:00
										 |  |  |     const hasEditorHelp = datasource?.components?.QueryEditorHelp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |     return ( | 
					
						
							| 
									
										
										
										
											2020-09-14 14:54:42 +08:00
										 |  |  |       <HorizontalGroup width="auto"> | 
					
						
							| 
									
										
										
										
											2021-01-20 06:52:09 +08:00
										 |  |  |         {hasEditorHelp && ( | 
					
						
							| 
									
										
										
										
											2021-07-12 22:42:04 +08:00
										 |  |  |           <QueryOperationAction | 
					
						
							|  |  |  |             title="Toggle data source help" | 
					
						
							|  |  |  |             icon="question-circle" | 
					
						
							|  |  |  |             onClick={this.onToggleHelp} | 
					
						
							|  |  |  |             active={showingHelp} | 
					
						
							|  |  |  |           /> | 
					
						
							| 
									
										
										
										
											2021-01-20 06:52:09 +08:00
										 |  |  |         )} | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |         {hasTextEditMode && ( | 
					
						
							|  |  |  |           <QueryOperationAction | 
					
						
							|  |  |  |             title="Toggle text edit mode" | 
					
						
							|  |  |  |             icon="pen" | 
					
						
							| 
									
										
										
										
											2021-01-20 14:59:48 +08:00
										 |  |  |             onClick={(e) => { | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |               this.onToggleEditMode(e, props); | 
					
						
							|  |  |  |             }} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         )} | 
					
						
							| 
									
										
										
										
											2021-09-20 22:36:04 +08:00
										 |  |  |         {this.renderExtraActions()} | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |         <QueryOperationAction title="Duplicate query" icon="copy" onClick={this.onCopyQuery} /> | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  |         {!hideDisableQuery ? ( | 
					
						
							|  |  |  |           <QueryOperationAction | 
					
						
							|  |  |  |             title="Disable/enable query" | 
					
						
							|  |  |  |             icon={isDisabled ? 'eye-slash' : 'eye'} | 
					
						
							| 
									
										
										
										
											2021-07-12 22:42:04 +08:00
										 |  |  |             active={isDisabled} | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  |             onClick={this.onDisableQuery} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         ) : null} | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |         <QueryOperationAction title="Remove query" icon="trash-alt" onClick={this.onRemoveQuery} /> | 
					
						
							|  |  |  |       </HorizontalGroup> | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 21:10:14 +08:00
										 |  |  |   renderHeader = (props: QueryOperationRowRenderProps) => { | 
					
						
							| 
									
										
										
										
											2022-01-08 01:27:20 +08:00
										 |  |  |     const { alerting, query, dataSource, onChangeDataSource, onChange, queries, renderHeaderExtras } = this.props; | 
					
						
							| 
									
										
										
										
											2020-07-09 21:16:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |     return ( | 
					
						
							| 
									
										
										
										
											2021-04-29 21:10:14 +08:00
										 |  |  |       <QueryEditorRowHeader | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |         query={query} | 
					
						
							| 
									
										
										
										
											2021-01-07 22:33:15 +08:00
										 |  |  |         queries={queries} | 
					
						
							| 
									
										
										
										
											2021-04-29 21:10:14 +08:00
										 |  |  |         onChangeDataSource={onChangeDataSource} | 
					
						
							|  |  |  |         dataSource={dataSource} | 
					
						
							|  |  |  |         disabled={query.hide} | 
					
						
							| 
									
										
										
										
											2021-01-20 14:59:48 +08:00
										 |  |  |         onClick={(e) => this.onToggleEditMode(e, props)} | 
					
						
							| 
									
										
										
										
											2021-01-07 22:33:15 +08:00
										 |  |  |         onChange={onChange} | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |         collapsedText={!props.isOpen ? this.renderCollapsedText() : null} | 
					
						
							| 
									
										
										
										
											2021-05-12 23:51:31 +08:00
										 |  |  |         renderExtras={renderHeaderExtras} | 
					
						
							| 
									
										
										
										
											2022-01-08 01:27:20 +08:00
										 |  |  |         alerting={alerting} | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |       /> | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2019-01-17 17:26:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |   render() { | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  |     const { query, id, index, visualization } = this.props; | 
					
						
							| 
									
										
										
										
											2022-04-14 18:57:56 +08:00
										 |  |  |     const { datasource, showingHelp, data } = this.state; | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |     const isDisabled = query.hide; | 
					
						
							| 
									
										
										
										
											2019-01-17 17:26:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const rowClasses = classNames('query-editor-row', { | 
					
						
							|  |  |  |       'query-editor-row--disabled': isDisabled, | 
					
						
							|  |  |  |       'gf-form-disabled': isDisabled, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!datasource) { | 
					
						
							|  |  |  |       return null; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-01-14 22:44:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  |     const editor = this.renderPluginEditor(); | 
					
						
							| 
									
										
										
										
											2021-01-20 06:52:09 +08:00
										 |  |  |     const DatasourceCheatsheet = datasource.components?.QueryEditorHelp; | 
					
						
							| 
									
										
										
										
											2020-04-10 03:23:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-17 00:53:40 +08:00
										 |  |  |     return ( | 
					
						
							| 
									
										
										
										
											2020-04-30 15:26:59 +08:00
										 |  |  |       <div aria-label={selectors.components.QueryEditorRows.rows}> | 
					
						
							| 
									
										
										
										
											2020-08-31 14:47:27 +08:00
										 |  |  |         <QueryOperationRow | 
					
						
							|  |  |  |           id={id} | 
					
						
							| 
									
										
										
										
											2020-09-14 14:54:42 +08:00
										 |  |  |           draggable={true} | 
					
						
							| 
									
										
										
										
											2020-08-31 14:47:27 +08:00
										 |  |  |           index={index} | 
					
						
							| 
									
										
										
										
											2021-04-29 21:10:14 +08:00
										 |  |  |           headerElement={this.renderHeader} | 
					
						
							| 
									
										
										
										
											2020-08-31 14:47:27 +08:00
										 |  |  |           actions={this.renderActions} | 
					
						
							|  |  |  |           onOpen={this.onOpen} | 
					
						
							|  |  |  |         > | 
					
						
							| 
									
										
										
										
											2020-04-30 15:26:59 +08:00
										 |  |  |           <div className={rowClasses}> | 
					
						
							| 
									
										
										
										
											2021-01-20 06:52:09 +08:00
										 |  |  |             <ErrorBoundaryAlert> | 
					
						
							|  |  |  |               {showingHelp && DatasourceCheatsheet && ( | 
					
						
							| 
									
										
										
										
											2021-07-12 22:42:04 +08:00
										 |  |  |                 <OperationRowHelp> | 
					
						
							| 
									
										
										
										
											2021-01-20 15:59:11 +08:00
										 |  |  |                   <DatasourceCheatsheet | 
					
						
							|  |  |  |                     onClickExample={(query) => this.onClickExample(query)} | 
					
						
							| 
									
										
										
										
											2022-04-26 01:59:52 +08:00
										 |  |  |                     query={this.props.query} | 
					
						
							| 
									
										
										
										
											2021-01-20 15:59:11 +08:00
										 |  |  |                     datasource={datasource} | 
					
						
							|  |  |  |                   /> | 
					
						
							| 
									
										
										
										
											2021-07-12 22:42:04 +08:00
										 |  |  |                 </OperationRowHelp> | 
					
						
							| 
									
										
										
										
											2021-01-20 06:52:09 +08:00
										 |  |  |               )} | 
					
						
							|  |  |  |               {editor} | 
					
						
							|  |  |  |             </ErrorBoundaryAlert> | 
					
						
							| 
									
										
										
										
											2022-04-14 18:57:56 +08:00
										 |  |  |             {data?.error && data.error.refId === query.refId && <QueryErrorAlert error={data.error} />} | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  |             {visualization} | 
					
						
							| 
									
										
										
										
											2020-04-30 15:26:59 +08:00
										 |  |  |           </div> | 
					
						
							|  |  |  |         </QueryOperationRow> | 
					
						
							|  |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2019-01-17 00:53:40 +08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2019-01-14 22:44:58 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  | function notifyAngularQueryEditorsOfData<TQuery extends DataQuery>( | 
					
						
							|  |  |  |   scope: AngularQueryComponentScope<TQuery>, | 
					
						
							|  |  |  |   data: PanelData, | 
					
						
							|  |  |  |   editor: AngularComponent | 
					
						
							|  |  |  | ) { | 
					
						
							| 
									
										
										
										
											2019-09-13 16:09:25 +08:00
										 |  |  |   if (data.state === LoadingState.Done) { | 
					
						
							| 
									
										
										
										
											2021-01-20 14:59:48 +08:00
										 |  |  |     const legacy = data.series.map((v) => toLegacyResponseData(v)); | 
					
						
							| 
									
										
										
										
											2021-01-19 19:36:50 +08:00
										 |  |  |     scope.events.emit(PanelEvents.dataReceived, legacy); | 
					
						
							| 
									
										
										
										
											2019-09-13 16:09:25 +08:00
										 |  |  |   } else if (data.state === LoadingState.Error) { | 
					
						
							| 
									
										
										
										
											2021-01-19 19:36:50 +08:00
										 |  |  |     scope.events.emit(PanelEvents.dataError, data.error); | 
					
						
							| 
									
										
										
										
											2019-09-13 16:09:25 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-09-12 23:28:46 +08: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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  | export interface AngularQueryComponentScope<TQuery extends DataQuery> { | 
					
						
							|  |  |  |   target: TQuery; | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |   panel: PanelModel; | 
					
						
							| 
									
										
										
										
											2019-02-20 01:53:07 +08:00
										 |  |  |   dashboard: DashboardModel; | 
					
						
							| 
									
										
										
										
											2020-11-03 20:08:54 +08:00
										 |  |  |   events: EventBusExtended; | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  |   refresh: () => void; | 
					
						
							|  |  |  |   render: () => void; | 
					
						
							| 
									
										
										
										
											2021-06-14 22:13:48 +08:00
										 |  |  |   datasource: DataSourceApi<TQuery> | null; | 
					
						
							| 
									
										
										
										
											2019-01-17 00:53:40 +08:00
										 |  |  |   toggleEditorMode?: () => void; | 
					
						
							| 
									
										
										
										
											2019-01-17 20:08:20 +08:00
										 |  |  |   getCollapsedText?: () => string; | 
					
						
							| 
									
										
										
										
											2019-02-11 00:05:58 +08:00
										 |  |  |   range: TimeRange; | 
					
						
							| 
									
										
										
										
											2019-01-15 18:40:12 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-21 03:26:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Get a version of the PanelData limited to the query we are looking at | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export function filterPanelDataToQuery(data: PanelData, refId: string): PanelData | undefined { | 
					
						
							| 
									
										
										
										
											2021-01-20 14:59:48 +08:00
										 |  |  |   const series = data.series.filter((series) => series.refId === refId); | 
					
						
							| 
									
										
										
										
											2019-04-21 03:26:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-14 18:57:56 +08:00
										 |  |  |   // If there was an error with no data, pass it to the QueryEditors
 | 
					
						
							|  |  |  |   if (data.error && !data.series.length) { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       ...data, | 
					
						
							|  |  |  |       state: LoadingState.Error, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2019-04-21 03:26:49 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Only say this is an error if the error links to the query
 | 
					
						
							| 
									
										
										
										
											2022-01-31 14:57:14 +08:00
										 |  |  |   let state = data.state; | 
					
						
							| 
									
										
										
										
											2019-04-21 03:26:49 +08:00
										 |  |  |   const error = data.error && data.error.refId === refId ? data.error : undefined; | 
					
						
							|  |  |  |   if (error) { | 
					
						
							|  |  |  |     state = LoadingState.Error; | 
					
						
							| 
									
										
										
										
											2022-02-16 18:48:22 +08:00
										 |  |  |   } else if (!error && data.state === LoadingState.Error) { | 
					
						
							| 
									
										
										
										
											2022-02-16 17:34:29 +08:00
										 |  |  |     state = LoadingState.Done; | 
					
						
							| 
									
										
										
										
											2019-04-21 03:26:49 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-25 17:19:17 +08:00
										 |  |  |   const timeRange = data.timeRange; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-21 03:26:49 +08:00
										 |  |  |   return { | 
					
						
							| 
									
										
										
										
											2019-11-11 23:47:17 +08:00
										 |  |  |     ...data, | 
					
						
							| 
									
										
										
										
											2019-04-21 03:26:49 +08:00
										 |  |  |     state, | 
					
						
							|  |  |  |     series, | 
					
						
							|  |  |  |     error, | 
					
						
							| 
									
										
										
										
											2019-09-25 17:19:17 +08:00
										 |  |  |     timeRange, | 
					
						
							| 
									
										
										
										
											2019-04-21 03:26:49 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | } |