| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  | import { css } from '@emotion/css'; | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  | import { cloneDeep } from 'lodash'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | import React, { FC, ReactNode, useState } from 'react'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  | import { | 
					
						
							| 
									
										
										
										
											2021-11-12 19:49:06 +08:00
										 |  |  |   CoreApp, | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |   DataQuery, | 
					
						
							|  |  |  |   DataSourceInstanceSettings, | 
					
						
							| 
									
										
										
										
											2021-09-15 23:35:12 +08:00
										 |  |  |   getDefaultRelativeTimeRange, | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |   GrafanaTheme2, | 
					
						
							| 
									
										
										
										
											2021-10-12 18:49:30 +08:00
										 |  |  |   LoadingState, | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |   PanelData, | 
					
						
							|  |  |  |   RelativeTimeRange, | 
					
						
							| 
									
										
										
										
											2021-09-15 23:35:12 +08:00
										 |  |  |   ThresholdsConfig, | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  | } from '@grafana/data'; | 
					
						
							| 
									
										
										
										
											2021-09-15 23:35:12 +08:00
										 |  |  | import { RelativeTimeRangePicker, useStyles2 } from '@grafana/ui'; | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  | import { isExpressionQuery } from 'app/features/expressions/guards'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | import { QueryEditorRow } from 'app/features/query/components/QueryEditorRow'; | 
					
						
							|  |  |  | import { AlertQuery } from 'app/types/unified-alerting-dto'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  | import { TABLE, TIMESERIES } from '../../utils/constants'; | 
					
						
							| 
									
										
										
										
											2021-07-01 18:02:41 +08:00
										 |  |  | import { SupportedPanelPlugins } from '../PanelPluginsButtonGroup'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { VizWrapper } from './VizWrapper'; | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | interface Props { | 
					
						
							|  |  |  |   data: PanelData; | 
					
						
							| 
									
										
										
										
											2021-05-27 18:29:10 +08:00
										 |  |  |   query: AlertQuery; | 
					
						
							|  |  |  |   queries: AlertQuery[]; | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |   dsSettings: DataSourceInstanceSettings; | 
					
						
							|  |  |  |   onChangeDataSource: (settings: DataSourceInstanceSettings, index: number) => void; | 
					
						
							|  |  |  |   onChangeQuery: (query: DataQuery, index: number) => void; | 
					
						
							|  |  |  |   onChangeTimeRange?: (timeRange: RelativeTimeRange, index: number) => void; | 
					
						
							|  |  |  |   onRemoveQuery: (query: DataQuery) => void; | 
					
						
							| 
									
										
										
										
											2021-05-27 18:29:10 +08:00
										 |  |  |   onDuplicateQuery: (query: AlertQuery) => void; | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |   onRunQueries: () => void; | 
					
						
							|  |  |  |   index: number; | 
					
						
							| 
									
										
										
										
											2021-09-15 23:35:12 +08:00
										 |  |  |   thresholds: ThresholdsConfig; | 
					
						
							|  |  |  |   onChangeThreshold: (thresholds: ThresholdsConfig, index: number) => void; | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-25 17:34:19 +08:00
										 |  |  | export const QueryWrapper: FC<Props> = ({ | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |   data, | 
					
						
							|  |  |  |   dsSettings, | 
					
						
							|  |  |  |   index, | 
					
						
							|  |  |  |   onChangeDataSource, | 
					
						
							|  |  |  |   onChangeQuery, | 
					
						
							|  |  |  |   onChangeTimeRange, | 
					
						
							|  |  |  |   onRunQueries, | 
					
						
							|  |  |  |   onRemoveQuery, | 
					
						
							|  |  |  |   onDuplicateQuery, | 
					
						
							|  |  |  |   query, | 
					
						
							|  |  |  |   queries, | 
					
						
							| 
									
										
										
										
											2021-09-15 23:35:12 +08:00
										 |  |  |   thresholds, | 
					
						
							|  |  |  |   onChangeThreshold, | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  | }) => { | 
					
						
							|  |  |  |   const styles = useStyles2(getStyles); | 
					
						
							|  |  |  |   const isExpression = isExpressionQuery(query.model); | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  |   const [pluginId, changePluginId] = useState<SupportedPanelPlugins>(isExpression ? TABLE : TIMESERIES); | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-27 18:29:10 +08:00
										 |  |  |   const renderTimePicker = (query: AlertQuery, index: number): ReactNode => { | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |     if (isExpressionQuery(query.model) || !onChangeTimeRange) { | 
					
						
							|  |  |  |       return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ( | 
					
						
							|  |  |  |       <RelativeTimeRangePicker | 
					
						
							|  |  |  |         timeRange={query.relativeTimeRange ?? getDefaultRelativeTimeRange()} | 
					
						
							|  |  |  |         onChange={(range) => onChangeTimeRange(range, index)} | 
					
						
							|  |  |  |       /> | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <div className={styles.wrapper}> | 
					
						
							| 
									
										
										
										
											2021-07-21 23:01:05 +08:00
										 |  |  |       <QueryEditorRow<DataQuery> | 
					
						
							| 
									
										
										
										
											2022-01-08 01:27:20 +08:00
										 |  |  |         alerting | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |         dataSource={dsSettings} | 
					
						
							|  |  |  |         onChangeDataSource={!isExpression ? (settings) => onChangeDataSource(settings, index) : undefined} | 
					
						
							|  |  |  |         id={query.refId} | 
					
						
							|  |  |  |         index={index} | 
					
						
							|  |  |  |         key={query.refId} | 
					
						
							|  |  |  |         data={data} | 
					
						
							| 
									
										
										
										
											2021-05-18 23:53:15 +08:00
										 |  |  |         query={cloneDeep(query.model)} | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |         onChange={(query) => onChangeQuery(query, index)} | 
					
						
							|  |  |  |         onRemoveQuery={onRemoveQuery} | 
					
						
							| 
									
										
										
										
											2021-11-02 22:18:35 +08:00
										 |  |  |         onAddQuery={() => onDuplicateQuery(cloneDeep(query))} | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |         onRunQuery={onRunQueries} | 
					
						
							|  |  |  |         queries={queries} | 
					
						
							|  |  |  |         renderHeaderExtras={() => renderTimePicker(query, index)} | 
					
						
							| 
									
										
										
										
											2021-11-12 19:49:06 +08:00
										 |  |  |         app={CoreApp.UnifiedAlerting} | 
					
						
							| 
									
										
										
										
											2021-09-15 23:35:12 +08:00
										 |  |  |         visualization={ | 
					
						
							| 
									
										
										
										
											2021-10-12 18:49:30 +08:00
										 |  |  |           data.state !== LoadingState.NotStarted ? ( | 
					
						
							| 
									
										
										
										
											2021-09-15 23:35:12 +08:00
										 |  |  |             <VizWrapper | 
					
						
							|  |  |  |               data={data} | 
					
						
							|  |  |  |               changePanel={changePluginId} | 
					
						
							|  |  |  |               currentPanel={pluginId} | 
					
						
							|  |  |  |               thresholds={thresholds} | 
					
						
							|  |  |  |               onThresholdsChange={(thresholds) => onChangeThreshold(thresholds, index)} | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           ) : null | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  |         hideDisableQuery={true} | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |       /> | 
					
						
							|  |  |  |     </div> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-17 21:30:00 +08:00
										 |  |  | export const EmptyQueryWrapper: FC<{}> = ({ children }) => { | 
					
						
							|  |  |  |   const styles = useStyles2(getStyles); | 
					
						
							|  |  |  |   return <div className={styles.wrapper}>{children}</div>; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  | const getStyles = (theme: GrafanaTheme2) => ({ | 
					
						
							|  |  |  |   wrapper: css`
 | 
					
						
							| 
									
										
										
										
											2021-05-27 16:46:06 +08:00
										 |  |  |     label: AlertingQueryWrapper; | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |     margin-bottom: ${theme.spacing(1)}; | 
					
						
							|  |  |  |     border: 1px solid ${theme.colors.border.medium}; | 
					
						
							|  |  |  |     border-radius: ${theme.shape.borderRadius(1)}; | 
					
						
							|  |  |  |   `,
 | 
					
						
							|  |  |  | }); |