| 
									
										
										
										
											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-08-12 21:32:07 +08:00
										 |  |  | import React, { FC, useState } from 'react'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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'; | 
					
						
							| 
									
										
										
										
											2022-10-05 20:35:15 +08:00
										 |  |  | import { RelativeTimeRangePicker, useStyles2, Tooltip, Icon, Stack } 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-10-05 20:35:15 +08:00
										 |  |  | import { AlertConditionIndicator } from '../expressions/AlertConditionIndicator'; | 
					
						
							| 
									
										
										
										
											2022-04-22 21:33:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { VizWrapper } from './VizWrapper'; | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | interface Props { | 
					
						
							|  |  |  |   data: PanelData; | 
					
						
							| 
									
										
										
										
											2022-10-05 20:35:15 +08:00
										 |  |  |   error?: Error; | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2022-10-05 20:35:15 +08:00
										 |  |  |   condition: string | null; | 
					
						
							|  |  |  |   onSetCondition: (refId: string) => 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, | 
					
						
							| 
									
										
										
										
											2022-10-05 20:35:15 +08:00
										 |  |  |   error, | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |   dsSettings, | 
					
						
							|  |  |  |   index, | 
					
						
							|  |  |  |   onChangeDataSource, | 
					
						
							|  |  |  |   onChangeQuery, | 
					
						
							|  |  |  |   onChangeTimeRange, | 
					
						
							|  |  |  |   onRunQueries, | 
					
						
							|  |  |  |   onRemoveQuery, | 
					
						
							|  |  |  |   onDuplicateQuery, | 
					
						
							|  |  |  |   query, | 
					
						
							|  |  |  |   queries, | 
					
						
							| 
									
										
										
										
											2021-09-15 23:35:12 +08:00
										 |  |  |   thresholds, | 
					
						
							|  |  |  |   onChangeThreshold, | 
					
						
							| 
									
										
										
										
											2022-10-05 20:35:15 +08:00
										 |  |  |   condition, | 
					
						
							|  |  |  |   onSetCondition, | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 21:32:07 +08:00
										 |  |  |   function SelectingDataSourceTooltip() { | 
					
						
							|  |  |  |     const styles = useStyles2(getStyles); | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |     return ( | 
					
						
							| 
									
										
										
										
											2022-08-12 21:32:07 +08:00
										 |  |  |       <div className={styles.dsTooltip}> | 
					
						
							|  |  |  |         <Tooltip | 
					
						
							|  |  |  |           content={ | 
					
						
							|  |  |  |             <> | 
					
						
							|  |  |  |               Not finding the data source you want? Some data sources are not supported for alerting. Click on the icon | 
					
						
							|  |  |  |               for more information. | 
					
						
							|  |  |  |             </> | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |           <Icon | 
					
						
							|  |  |  |             name="info-circle" | 
					
						
							|  |  |  |             onClick={() => | 
					
						
							|  |  |  |               window.open( | 
					
						
							|  |  |  |                 ' https://grafana.com/docs/grafana/latest/alerting/fundamentals/data-source-alerting/', | 
					
						
							|  |  |  |                 '_blank' | 
					
						
							|  |  |  |               ) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         </Tooltip> | 
					
						
							|  |  |  |       </div> | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-08-12 21:32:07 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-05 20:35:15 +08:00
										 |  |  |   // TODO add a warning label here too when the data looks like time series data and is used as an alert condition
 | 
					
						
							|  |  |  |   function HeaderExtras({ query, error, index }: { query: AlertQuery; error?: Error; index: number }) { | 
					
						
							| 
									
										
										
										
											2022-08-12 21:32:07 +08:00
										 |  |  |     if (isExpressionQuery(query.model)) { | 
					
						
							|  |  |  |       return null; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return ( | 
					
						
							| 
									
										
										
										
											2022-10-05 20:35:15 +08:00
										 |  |  |         <Stack direction="row" alignItems="center" gap={1}> | 
					
						
							| 
									
										
										
										
											2022-08-12 21:32:07 +08:00
										 |  |  |           <SelectingDataSourceTooltip /> | 
					
						
							|  |  |  |           {onChangeTimeRange && ( | 
					
						
							|  |  |  |             <RelativeTimeRangePicker | 
					
						
							|  |  |  |               timeRange={query.relativeTimeRange ?? getDefaultRelativeTimeRange()} | 
					
						
							|  |  |  |               onChange={(range) => onChangeTimeRange(range, index)} | 
					
						
							|  |  |  |             /> | 
					
						
							|  |  |  |           )} | 
					
						
							| 
									
										
										
										
											2022-10-05 20:35:15 +08:00
										 |  |  |           <AlertConditionIndicator | 
					
						
							|  |  |  |             onSetCondition={() => onSetCondition(query.refId)} | 
					
						
							|  |  |  |             enabled={condition === query.refId} | 
					
						
							|  |  |  |             error={error} | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |         </Stack> | 
					
						
							| 
									
										
										
										
											2022-08-12 21:32:07 +08:00
										 |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   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} | 
					
						
							| 
									
										
										
										
											2022-10-05 20:35:15 +08:00
										 |  |  |         renderHeaderExtras={() => <HeaderExtras query={query} index={index} error={error} />} | 
					
						
							| 
									
										
										
										
											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-10-14 17:24:32 +08:00
										 |  |  | export const EmptyQueryWrapper = ({ children }: React.PropsWithChildren<{}>) => { | 
					
						
							| 
									
										
										
										
											2022-01-17 21:30:00 +08:00
										 |  |  |   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)}; | 
					
						
							|  |  |  |   `,
 | 
					
						
							| 
									
										
										
										
											2022-08-12 21:32:07 +08:00
										 |  |  |   dsTooltip: css`
 | 
					
						
							|  |  |  |     display: flex; | 
					
						
							|  |  |  |     align-items: center; | 
					
						
							|  |  |  |     &:hover { | 
					
						
							|  |  |  |       opacity: 0.85; | 
					
						
							|  |  |  |       cursor: pointer; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   `,
 | 
					
						
							| 
									
										
										
										
											2021-05-18 22:16:26 +08:00
										 |  |  | }); |