| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   DataQuery, | 
					
						
							|  |  |  |   rangeUtil, | 
					
						
							|  |  |  |   RelativeTimeRange, | 
					
						
							|  |  |  |   ScopedVars, | 
					
						
							|  |  |  |   getDefaultRelativeTimeRange, | 
					
						
							|  |  |  |   TimeRange, | 
					
						
							|  |  |  |   IntervalValues, | 
					
						
							|  |  |  | } from '@grafana/data'; | 
					
						
							| 
									
										
										
										
											2021-05-04 22:31:25 +08:00
										 |  |  | import { getDataSourceSrv } from '@grafana/runtime'; | 
					
						
							| 
									
										
										
										
											2021-05-17 16:15:17 +08:00
										 |  |  | import { contextSrv } from 'app/core/services/context_srv'; | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  | import { getNextRefIdChar } from 'app/core/utils/query'; | 
					
						
							|  |  |  | import { DashboardModel, PanelModel } from 'app/features/dashboard/state'; | 
					
						
							| 
									
										
										
										
											2021-05-04 22:31:25 +08:00
										 |  |  | import { ExpressionDatasourceID, ExpressionDatasourceUID } from 'app/features/expressions/ExpressionDatasource'; | 
					
						
							|  |  |  | import { ExpressionQuery, ExpressionQueryType } from 'app/features/expressions/types'; | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  | import { RuleWithLocation } from 'app/types/unified-alerting'; | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   Annotations, | 
					
						
							| 
									
										
										
										
											2021-05-06 16:21:58 +08:00
										 |  |  |   GrafanaAlertStateDecision, | 
					
						
							| 
									
										
										
										
											2021-05-27 18:29:10 +08:00
										 |  |  |   AlertQuery, | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  |   Labels, | 
					
						
							|  |  |  |   PostableRuleGrafanaRuleDTO, | 
					
						
							|  |  |  |   RulerAlertingRuleDTO, | 
					
						
							|  |  |  | } from 'app/types/unified-alerting-dto'; | 
					
						
							| 
									
										
										
										
											2021-05-04 22:31:25 +08:00
										 |  |  | import { EvalFunction } from '../../state/alertDef'; | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  | import { RuleFormType, RuleFormValues } from '../types/rule-form'; | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  | import { Annotation } from './constants'; | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  | import { isGrafanaRulesSource } from './datasource'; | 
					
						
							|  |  |  | import { arrayToRecord, recordToArray } from './misc'; | 
					
						
							|  |  |  | import { isAlertingRulerRule, isGrafanaRulerRule } from './rules'; | 
					
						
							| 
									
										
										
										
											2021-05-04 21:57:11 +08:00
										 |  |  | import { parseInterval } from './time'; | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 16:15:17 +08:00
										 |  |  | export const getDefaultFormValues = (): RuleFormValues => | 
					
						
							|  |  |  |   Object.freeze({ | 
					
						
							|  |  |  |     name: '', | 
					
						
							|  |  |  |     labels: [{ key: '', value: '' }], | 
					
						
							| 
									
										
										
										
											2021-05-19 00:14:57 +08:00
										 |  |  |     annotations: [ | 
					
						
							|  |  |  |       { key: Annotation.summary, value: '' }, | 
					
						
							|  |  |  |       { key: Annotation.description, value: '' }, | 
					
						
							|  |  |  |       { key: Annotation.runbookURL, value: '' }, | 
					
						
							|  |  |  |     ], | 
					
						
							| 
									
										
										
										
											2021-05-17 16:15:17 +08:00
										 |  |  |     dataSourceName: null, | 
					
						
							| 
									
										
										
										
											2021-05-19 00:14:57 +08:00
										 |  |  |     type: !contextSrv.isEditor ? RuleFormType.grafana : undefined, // viewers can't create prom alerts
 | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-19 00:14:57 +08:00
										 |  |  |     // grafana
 | 
					
						
							| 
									
										
										
										
											2021-05-17 16:15:17 +08:00
										 |  |  |     folder: null, | 
					
						
							|  |  |  |     queries: [], | 
					
						
							|  |  |  |     condition: '', | 
					
						
							|  |  |  |     noDataState: GrafanaAlertStateDecision.NoData, | 
					
						
							|  |  |  |     execErrState: GrafanaAlertStateDecision.Alerting, | 
					
						
							|  |  |  |     evaluateEvery: '1m', | 
					
						
							|  |  |  |     evaluateFor: '5m', | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-19 00:14:57 +08:00
										 |  |  |     // cortex / loki
 | 
					
						
							| 
									
										
										
										
											2021-05-17 16:15:17 +08:00
										 |  |  |     group: '', | 
					
						
							|  |  |  |     namespace: '', | 
					
						
							|  |  |  |     expression: '', | 
					
						
							|  |  |  |     forTime: 1, | 
					
						
							|  |  |  |     forTimeUnit: 'm', | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-04-14 20:57:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function formValuesToRulerAlertingRuleDTO(values: RuleFormValues): RulerAlertingRuleDTO { | 
					
						
							|  |  |  |   const { name, expression, forTime, forTimeUnit } = values; | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     alert: name, | 
					
						
							|  |  |  |     for: `${forTime}${forTimeUnit}`, | 
					
						
							|  |  |  |     annotations: arrayToRecord(values.annotations || []), | 
					
						
							|  |  |  |     labels: arrayToRecord(values.labels || []), | 
					
						
							|  |  |  |     expr: expression, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  | function listifyLabelsOrAnnotations(item: Labels | Annotations | undefined): Array<{ key: string; value: string }> { | 
					
						
							|  |  |  |   return [...recordToArray(item || {}), { key: '', value: '' }]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function formValuesToRulerGrafanaRuleDTO(values: RuleFormValues): PostableRuleGrafanaRuleDTO { | 
					
						
							| 
									
										
										
										
											2021-04-14 20:57:36 +08:00
										 |  |  |   const { name, condition, noDataState, execErrState, evaluateFor, queries } = values; | 
					
						
							|  |  |  |   if (condition) { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       grafana_alert: { | 
					
						
							|  |  |  |         title: name, | 
					
						
							|  |  |  |         condition, | 
					
						
							|  |  |  |         no_data_state: noDataState, | 
					
						
							|  |  |  |         exec_err_state: execErrState, | 
					
						
							|  |  |  |         data: queries, | 
					
						
							|  |  |  |       }, | 
					
						
							| 
									
										
										
										
											2021-04-19 17:53:02 +08:00
										 |  |  |       for: evaluateFor, | 
					
						
							|  |  |  |       annotations: arrayToRecord(values.annotations || []), | 
					
						
							|  |  |  |       labels: arrayToRecord(values.labels || []), | 
					
						
							| 
									
										
										
										
											2021-04-14 20:57:36 +08:00
										 |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   throw new Error('Cannot create rule without specifying alert condition'); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function rulerRuleToFormValues(ruleWithLocation: RuleWithLocation): RuleFormValues { | 
					
						
							|  |  |  |   const { ruleSourceName, namespace, group, rule } = ruleWithLocation; | 
					
						
							| 
									
										
										
										
											2021-05-17 16:15:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const defaultFormValues = getDefaultFormValues(); | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  |   if (isGrafanaRulesSource(ruleSourceName)) { | 
					
						
							|  |  |  |     if (isGrafanaRulerRule(rule)) { | 
					
						
							|  |  |  |       const ga = rule.grafana_alert; | 
					
						
							|  |  |  |       return { | 
					
						
							|  |  |  |         ...defaultFormValues, | 
					
						
							|  |  |  |         name: ga.title, | 
					
						
							| 
									
										
										
										
											2021-05-19 00:14:57 +08:00
										 |  |  |         type: RuleFormType.grafana, | 
					
						
							| 
									
										
										
										
											2021-06-21 20:54:42 +08:00
										 |  |  |         evaluateFor: rule.for || '0', | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  |         evaluateEvery: group.interval || defaultFormValues.evaluateEvery, | 
					
						
							|  |  |  |         noDataState: ga.no_data_state, | 
					
						
							|  |  |  |         execErrState: ga.exec_err_state, | 
					
						
							|  |  |  |         queries: ga.data, | 
					
						
							|  |  |  |         condition: ga.condition, | 
					
						
							| 
									
										
										
										
											2021-04-19 17:53:02 +08:00
										 |  |  |         annotations: listifyLabelsOrAnnotations(rule.annotations), | 
					
						
							|  |  |  |         labels: listifyLabelsOrAnnotations(rule.labels), | 
					
						
							| 
									
										
										
										
											2021-07-21 23:01:05 +08:00
										 |  |  |         folder: { title: namespace, id: ga.namespace_id }, | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  |       }; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       throw new Error('Unexpected type of rule for grafana rules source'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     if (isAlertingRulerRule(rule)) { | 
					
						
							|  |  |  |       const [forTime, forTimeUnit] = rule.for | 
					
						
							|  |  |  |         ? parseInterval(rule.for) | 
					
						
							|  |  |  |         : [defaultFormValues.forTime, defaultFormValues.forTimeUnit]; | 
					
						
							|  |  |  |       return { | 
					
						
							|  |  |  |         ...defaultFormValues, | 
					
						
							|  |  |  |         name: rule.alert, | 
					
						
							| 
									
										
										
										
											2021-05-19 00:14:57 +08:00
										 |  |  |         type: RuleFormType.cloud, | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  |         dataSourceName: ruleSourceName, | 
					
						
							| 
									
										
										
										
											2021-04-16 19:57:33 +08:00
										 |  |  |         namespace, | 
					
						
							|  |  |  |         group: group.name, | 
					
						
							| 
									
										
										
										
											2021-04-16 16:08:26 +08:00
										 |  |  |         expression: rule.expr, | 
					
						
							|  |  |  |         forTime, | 
					
						
							|  |  |  |         forTimeUnit, | 
					
						
							|  |  |  |         annotations: listifyLabelsOrAnnotations(rule.annotations), | 
					
						
							|  |  |  |         labels: listifyLabelsOrAnnotations(rule.labels), | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       throw new Error('Editing recording rules not supported (yet)'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-05-04 22:31:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-27 18:29:10 +08:00
										 |  |  | export const getDefaultQueries = (): AlertQuery[] => { | 
					
						
							| 
									
										
										
										
											2021-05-04 22:31:25 +08:00
										 |  |  |   const dataSource = getDataSourceSrv().getInstanceSettings('default'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!dataSource) { | 
					
						
							|  |  |  |     return [getDefaultExpression('A')]; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-05-19 15:42:40 +08:00
										 |  |  |   const relativeTimeRange = getDefaultRelativeTimeRange(); | 
					
						
							| 
									
										
										
										
											2021-05-04 22:31:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return [ | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       refId: 'A', | 
					
						
							|  |  |  |       datasourceUid: dataSource.uid, | 
					
						
							|  |  |  |       queryType: '', | 
					
						
							|  |  |  |       relativeTimeRange, | 
					
						
							|  |  |  |       model: { | 
					
						
							|  |  |  |         refId: 'A', | 
					
						
							|  |  |  |         hide: false, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     getDefaultExpression('B'), | 
					
						
							|  |  |  |   ]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-27 18:29:10 +08:00
										 |  |  | const getDefaultExpression = (refId: string): AlertQuery => { | 
					
						
							| 
									
										
										
										
											2021-05-04 22:31:25 +08:00
										 |  |  |   const model: ExpressionQuery = { | 
					
						
							|  |  |  |     refId, | 
					
						
							|  |  |  |     hide: false, | 
					
						
							|  |  |  |     type: ExpressionQueryType.classic, | 
					
						
							|  |  |  |     datasource: ExpressionDatasourceID, | 
					
						
							|  |  |  |     conditions: [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         type: 'query', | 
					
						
							|  |  |  |         evaluator: { | 
					
						
							|  |  |  |           params: [3], | 
					
						
							|  |  |  |           type: EvalFunction.IsAbove, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         operator: { | 
					
						
							|  |  |  |           type: 'and', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         query: { | 
					
						
							|  |  |  |           params: ['A'], | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         reducer: { | 
					
						
							|  |  |  |           params: [], | 
					
						
							|  |  |  |           type: 'last', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     refId, | 
					
						
							|  |  |  |     datasourceUid: ExpressionDatasourceUID, | 
					
						
							|  |  |  |     queryType: '', | 
					
						
							|  |  |  |     model, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  | const dataQueriesToGrafanaQueries = async ( | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |   queries: DataQuery[], | 
					
						
							|  |  |  |   relativeTimeRange: RelativeTimeRange, | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |   scopedVars: ScopedVars | {}, | 
					
						
							|  |  |  |   datasourceName?: string, | 
					
						
							|  |  |  |   maxDataPoints?: number, | 
					
						
							|  |  |  |   minInterval?: string | 
					
						
							|  |  |  | ): Promise<AlertQuery[]> => { | 
					
						
							|  |  |  |   const result: AlertQuery[] = []; | 
					
						
							|  |  |  |   for (const target of queries) { | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |     const dsName = target.datasource || datasourceName; | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |     const datasource = await getDataSourceSrv().get(dsName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const range = rangeUtil.relativeToTimeRange(relativeTimeRange); | 
					
						
							|  |  |  |     const { interval, intervalMs } = getIntervals(range, minInterval ?? datasource.interval, maxDataPoints); | 
					
						
							|  |  |  |     const queryVariables = { | 
					
						
							|  |  |  |       __interval: { text: interval, value: interval }, | 
					
						
							|  |  |  |       __interval_ms: { text: intervalMs, value: intervalMs }, | 
					
						
							|  |  |  |       ...scopedVars, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     const interpolatedTarget = datasource.interpolateVariablesInQueries | 
					
						
							|  |  |  |       ? await datasource.interpolateVariablesInQueries([target], queryVariables)[0] | 
					
						
							|  |  |  |       : target; | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |     if (dsName) { | 
					
						
							|  |  |  |       // expressions
 | 
					
						
							|  |  |  |       if (dsName === ExpressionDatasourceID) { | 
					
						
							| 
									
										
										
										
											2021-05-27 18:29:10 +08:00
										 |  |  |         const newQuery: AlertQuery = { | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |           refId: interpolatedTarget.refId, | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |           queryType: '', | 
					
						
							|  |  |  |           relativeTimeRange, | 
					
						
							|  |  |  |           datasourceUid: ExpressionDatasourceUID, | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |           model: interpolatedTarget, | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |         }; | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |         result.push(newQuery); | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |         // queries
 | 
					
						
							|  |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |         const datasourceSettings = getDataSourceSrv().getInstanceSettings(dsName); | 
					
						
							|  |  |  |         if (datasourceSettings && datasourceSettings.meta.alerting) { | 
					
						
							| 
									
										
										
										
											2021-05-27 18:29:10 +08:00
										 |  |  |           const newQuery: AlertQuery = { | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |             refId: interpolatedTarget.refId, | 
					
						
							|  |  |  |             queryType: interpolatedTarget.queryType ?? '', | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |             relativeTimeRange, | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |             datasourceUid: datasourceSettings.uid, | 
					
						
							|  |  |  |             model: { | 
					
						
							|  |  |  |               ...interpolatedTarget, | 
					
						
							|  |  |  |               maxDataPoints, | 
					
						
							|  |  |  |               intervalMs, | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |           }; | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |           result.push(newQuery); | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  | export const panelToRuleFormValues = async ( | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |   panel: PanelModel, | 
					
						
							|  |  |  |   dashboard: DashboardModel | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  | ): Promise<Partial<RuleFormValues> | undefined> => { | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |   const { targets } = panel; | 
					
						
							|  |  |  |   if (!panel.editSourceId || !dashboard.uid) { | 
					
						
							|  |  |  |     return undefined; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const relativeTimeRange = rangeUtil.timeRangeToRelative(rangeUtil.convertRawToRange(dashboard.time)); | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |   const queries = await dataQueriesToGrafanaQueries( | 
					
						
							|  |  |  |     targets, | 
					
						
							|  |  |  |     relativeTimeRange, | 
					
						
							|  |  |  |     panel.scopedVars || {}, | 
					
						
							|  |  |  |     panel.datasource ?? undefined, | 
					
						
							|  |  |  |     panel.maxDataPoints ?? undefined, | 
					
						
							|  |  |  |     panel.interval ?? undefined | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |   // if no alerting capable queries are found, can't create a rule
 | 
					
						
							|  |  |  |   if (!queries.length || !queries.find((query) => query.datasourceUid !== ExpressionDatasourceUID)) { | 
					
						
							|  |  |  |     return undefined; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!queries.find((query) => query.datasourceUid === ExpressionDatasourceUID)) { | 
					
						
							|  |  |  |     queries.push(getDefaultExpression(getNextRefIdChar(queries.map((query) => query.model)))); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { folderId, folderTitle } = dashboard.meta; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const formValues = { | 
					
						
							| 
									
										
										
										
											2021-05-19 00:14:57 +08:00
										 |  |  |     type: RuleFormType.grafana, | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |     folder: | 
					
						
							|  |  |  |       folderId && folderTitle | 
					
						
							|  |  |  |         ? { | 
					
						
							|  |  |  |             id: folderId, | 
					
						
							|  |  |  |             title: folderTitle, | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         : undefined, | 
					
						
							|  |  |  |     queries, | 
					
						
							|  |  |  |     name: panel.title, | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  |     condition: queries[queries.length - 1].refId, | 
					
						
							| 
									
										
										
										
											2021-05-17 15:39:42 +08:00
										 |  |  |     annotations: [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         key: Annotation.dashboardUID, | 
					
						
							|  |  |  |         value: dashboard.uid, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         key: Annotation.panelID, | 
					
						
							|  |  |  |         value: String(panel.editSourceId), | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   return formValues; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2021-08-10 15:59:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function getIntervals(range: TimeRange, lowLimit?: string, resolution?: number): IntervalValues { | 
					
						
							|  |  |  |   if (!resolution) { | 
					
						
							|  |  |  |     if (lowLimit && rangeUtil.intervalToMs(lowLimit) > 1000) { | 
					
						
							|  |  |  |       return { | 
					
						
							|  |  |  |         interval: lowLimit, | 
					
						
							|  |  |  |         intervalMs: rangeUtil.intervalToMs(lowLimit), | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return { interval: '1s', intervalMs: 1000 }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return rangeUtil.calculateInterval(range, resolution, lowLimit); | 
					
						
							|  |  |  | } |