| 
									
										
										
										
											2024-02-15 01:18:04 +08:00
										 |  |  | import { config } from '@grafana/runtime'; | 
					
						
							| 
									
										
										
										
											2024-03-21 23:35:48 +08:00
										 |  |  | import { MultiValueVariable, SceneVariables, sceneUtils } from '@grafana/scenes'; | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   VariableModel, | 
					
						
							|  |  |  |   VariableRefresh as OldVariableRefresh, | 
					
						
							|  |  |  |   VariableHide as OldVariableHide, | 
					
						
							|  |  |  |   VariableSort as OldVariableSort, | 
					
						
							|  |  |  | } from '@grafana/schema'; | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   AdhocVariableKind, | 
					
						
							|  |  |  |   ConstantVariableKind, | 
					
						
							|  |  |  |   CustomVariableKind, | 
					
						
							|  |  |  |   DataQueryKind, | 
					
						
							|  |  |  |   DatasourceVariableKind, | 
					
						
							|  |  |  |   IntervalVariableKind, | 
					
						
							|  |  |  |   QueryVariableKind, | 
					
						
							|  |  |  |   TextVariableKind, | 
					
						
							|  |  |  |   GroupByVariableKind, | 
					
						
							|  |  |  |   defaultVariableHide, | 
					
						
							|  |  |  |   VariableOption, | 
					
						
							| 
									
										
										
										
											2025-04-02 22:22:24 +08:00
										 |  |  | } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha1/types.spec.gen'; | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-16 23:34:09 +08:00
										 |  |  | import { getIntervalsQueryFromNewIntervalModel } from '../utils/utils'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-26 23:59:34 +08:00
										 |  |  | import { DSReferencesMapping } from './DashboardSceneSerializer'; | 
					
						
							|  |  |  | import { getDataQueryKind, getDataQuerySpec, getElementDatasource } from './transformSceneToSaveModelSchemaV2'; | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   transformVariableRefreshToEnum, | 
					
						
							|  |  |  |   transformVariableHideToEnum, | 
					
						
							|  |  |  |   transformSortVariableToEnum, | 
					
						
							| 
									
										
										
										
											2025-02-06 22:33:06 +08:00
										 |  |  |   LEGACY_STRING_VALUE_KEY, | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  | } from './transformToV2TypesUtils'; | 
					
						
							| 
									
										
										
										
											2024-10-05 01:19:18 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Converts a SceneVariables object into an array of VariableModel objects. | 
					
						
							|  |  |  |  * @param set - The SceneVariables object containing the variables to convert. | 
					
						
							|  |  |  |  * @param keepQueryOptions - (Optional) A boolean flag indicating whether to keep the options for query variables. | 
					
						
							|  |  |  |  *                           This should be set to `false` when variables are saved in the dashboard model, | 
					
						
							|  |  |  |  *                           but should be set to `true` when variables are used in the templateSrv to keep them in sync. | 
					
						
							|  |  |  |  *                           If `true`, the options for query variables are kept. | 
					
						
							|  |  |  |  *  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function sceneVariablesSetToVariables(set: SceneVariables, keepQueryOptions?: boolean) { | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |   const variables: VariableModel[] = []; | 
					
						
							|  |  |  |   for (const variable of set.state.variables) { | 
					
						
							|  |  |  |     const commonProperties = { | 
					
						
							|  |  |  |       name: variable.state.name, | 
					
						
							|  |  |  |       label: variable.state.label, | 
					
						
							| 
									
										
										
										
											2023-11-22 20:43:27 +08:00
										 |  |  |       description: variable.state.description ?? undefined, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |       skipUrlSync: Boolean(variable.state.skipUrlSync), | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |       hide: variable.state.hide || OldVariableHide.dontHide, | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |       type: variable.state.type, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2025-03-26 23:59:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |     if (sceneUtils.isQueryVariable(variable)) { | 
					
						
							| 
									
										
										
										
											2024-03-21 23:35:48 +08:00
										 |  |  |       let options: VariableOption[] = []; | 
					
						
							|  |  |  |       // Not sure if we actually have to still support this option given
 | 
					
						
							|  |  |  |       // that it's not exposed in the UI
 | 
					
						
							| 
									
										
										
										
											2024-11-28 17:45:31 +08:00
										 |  |  |       if (transformVariableRefreshToEnum(variable.state.refresh) === 'never' || keepQueryOptions) { | 
					
						
							| 
									
										
										
										
											2024-03-21 23:35:48 +08:00
										 |  |  |         options = variableValueOptionsToVariableOptions(variable.state); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |       variables.push({ | 
					
						
							|  |  |  |         ...commonProperties, | 
					
						
							|  |  |  |         current: { | 
					
						
							|  |  |  |           // @ts-expect-error
 | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |           value: variable.state.value, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |           // @ts-expect-error
 | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |           text: variable.state.text, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-03-21 23:35:48 +08:00
										 |  |  |         options, | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |         query: variable.state.query, | 
					
						
							| 
									
										
										
										
											2023-12-06 18:33:54 +08:00
										 |  |  |         definition: variable.state.definition, | 
					
						
							| 
									
										
										
										
											2025-03-26 23:59:34 +08:00
										 |  |  |         datasource: getElementDatasource(set, variable, 'variable'), | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |         sort: variable.state.sort, | 
					
						
							|  |  |  |         refresh: variable.state.refresh, | 
					
						
							|  |  |  |         regex: variable.state.regex, | 
					
						
							|  |  |  |         allValue: variable.state.allValue, | 
					
						
							|  |  |  |         includeAll: variable.state.includeAll, | 
					
						
							|  |  |  |         multi: variable.state.isMulti, | 
					
						
							| 
									
										
										
										
											2024-11-21 22:01:54 +08:00
										 |  |  |         allowCustomValue: variable.state.allowCustomValue, | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |         skipUrlSync: variable.state.skipUrlSync, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |     } else if (sceneUtils.isCustomVariable(variable)) { | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |       variables.push({ | 
					
						
							|  |  |  |         ...commonProperties, | 
					
						
							|  |  |  |         current: { | 
					
						
							|  |  |  |           // @ts-expect-error
 | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |           text: variable.state.value, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |           // @ts-expect-error
 | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |           value: variable.state.value, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-03-21 23:35:48 +08:00
										 |  |  |         options: variableValueOptionsToVariableOptions(variable.state), | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |         query: variable.state.query, | 
					
						
							|  |  |  |         multi: variable.state.isMulti, | 
					
						
							|  |  |  |         allValue: variable.state.allValue, | 
					
						
							|  |  |  |         includeAll: variable.state.includeAll, | 
					
						
							| 
									
										
										
										
											2024-11-21 22:01:54 +08:00
										 |  |  |         allowCustomValue: variable.state.allowCustomValue, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |     } else if (sceneUtils.isDataSourceVariable(variable)) { | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |       variables.push({ | 
					
						
							|  |  |  |         ...commonProperties, | 
					
						
							|  |  |  |         current: { | 
					
						
							|  |  |  |           // @ts-expect-error
 | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |           value: variable.state.value, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |           // @ts-expect-error
 | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |           text: variable.state.text, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         options: [], | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |         regex: variable.state.regex, | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |         refresh: OldVariableRefresh.onDashboardLoad, | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |         query: variable.state.pluginId, | 
					
						
							|  |  |  |         multi: variable.state.isMulti, | 
					
						
							|  |  |  |         allValue: variable.state.allValue, | 
					
						
							|  |  |  |         includeAll: variable.state.includeAll, | 
					
						
							| 
									
										
										
										
											2024-11-21 22:01:54 +08:00
										 |  |  |         allowCustomValue: variable.state.allowCustomValue, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |     } else if (sceneUtils.isConstantVariable(variable)) { | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |       variables.push({ | 
					
						
							|  |  |  |         ...commonProperties, | 
					
						
							|  |  |  |         current: { | 
					
						
							|  |  |  |           // @ts-expect-error
 | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |           value: variable.state.value, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |           // @ts-expect-error
 | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |           text: variable.state.value, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         // @ts-expect-error
 | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |         query: variable.state.value, | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |         hide: OldVariableHide.hideVariable, | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |     } else if (sceneUtils.isIntervalVariable(variable)) { | 
					
						
							|  |  |  |       const intervals = getIntervalsQueryFromNewIntervalModel(variable.state.intervals); | 
					
						
							| 
									
										
										
										
											2023-10-16 23:34:09 +08:00
										 |  |  |       variables.push({ | 
					
						
							|  |  |  |         ...commonProperties, | 
					
						
							|  |  |  |         current: { | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |           text: variable.state.value, | 
					
						
							|  |  |  |           value: variable.state.value, | 
					
						
							| 
									
										
										
										
											2023-10-16 23:34:09 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |         query: intervals, | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |         refresh: variable.state.refresh, | 
					
						
							| 
									
										
										
										
											2024-03-21 23:35:48 +08:00
										 |  |  |         options: variable.state.intervals.map((interval) => ({ | 
					
						
							|  |  |  |           value: interval, | 
					
						
							|  |  |  |           text: interval, | 
					
						
							|  |  |  |           selected: interval === variable.state.value, | 
					
						
							|  |  |  |         })), | 
					
						
							| 
									
										
										
										
											2023-10-16 23:34:09 +08:00
										 |  |  |         // @ts-expect-error ?? how to fix this without adding the ts-expect-error
 | 
					
						
							| 
									
										
										
										
											2023-11-22 16:47:44 +08:00
										 |  |  |         auto: variable.state.autoEnabled, | 
					
						
							|  |  |  |         auto_min: variable.state.autoMinInterval, | 
					
						
							|  |  |  |         auto_count: variable.state.autoStepCount, | 
					
						
							| 
									
										
										
										
											2023-10-16 23:34:09 +08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-11-22 20:43:27 +08:00
										 |  |  |     } else if (sceneUtils.isTextBoxVariable(variable)) { | 
					
						
							| 
									
										
										
										
											2024-03-21 23:35:48 +08:00
										 |  |  |       const current = { | 
					
						
							|  |  |  |         text: variable.state.value, | 
					
						
							|  |  |  |         value: variable.state.value, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-22 20:43:27 +08:00
										 |  |  |       variables.push({ | 
					
						
							|  |  |  |         ...commonProperties, | 
					
						
							| 
									
										
										
										
											2024-03-21 23:35:48 +08:00
										 |  |  |         current, | 
					
						
							|  |  |  |         options: [{ ...current, selected: true }], | 
					
						
							| 
									
										
										
										
											2023-11-22 20:43:27 +08:00
										 |  |  |         query: variable.state.value, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2024-02-15 01:18:04 +08:00
										 |  |  |     } else if (sceneUtils.isGroupByVariable(variable) && config.featureToggles.groupByVariable) { | 
					
						
							|  |  |  |       variables.push({ | 
					
						
							|  |  |  |         ...commonProperties, | 
					
						
							|  |  |  |         datasource: variable.state.datasource, | 
					
						
							|  |  |  |         // Only persist the statically defined options
 | 
					
						
							|  |  |  |         options: variable.state.defaultOptions?.map((option) => ({ | 
					
						
							|  |  |  |           text: option.text, | 
					
						
							|  |  |  |           value: String(option.value), | 
					
						
							|  |  |  |         })), | 
					
						
							|  |  |  |         current: { | 
					
						
							|  |  |  |           // @ts-expect-error
 | 
					
						
							|  |  |  |           text: variable.state.text, | 
					
						
							|  |  |  |           // @ts-expect-error
 | 
					
						
							|  |  |  |           value: variable.state.value, | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-11-25 18:21:25 +08:00
										 |  |  |         allowCustomValue: variable.state.allowCustomValue, | 
					
						
							| 
									
										
										
										
											2024-02-15 01:18:04 +08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2024-02-14 04:38:26 +08:00
										 |  |  |     } else if (sceneUtils.isAdHocVariable(variable)) { | 
					
						
							|  |  |  |       variables.push({ | 
					
						
							|  |  |  |         ...commonProperties, | 
					
						
							|  |  |  |         datasource: variable.state.datasource, | 
					
						
							| 
									
										
										
										
											2024-11-21 22:01:54 +08:00
										 |  |  |         allowCustomValue: variable.state.allowCustomValue, | 
					
						
							| 
									
										
										
										
											2024-02-14 04:38:26 +08:00
										 |  |  |         // @ts-expect-error
 | 
					
						
							|  |  |  |         baseFilters: variable.state.baseFilters, | 
					
						
							|  |  |  |         filters: variable.state.filters, | 
					
						
							| 
									
										
										
										
											2024-03-19 00:12:00 +08:00
										 |  |  |         defaultKeys: variable.state.defaultKeys, | 
					
						
							| 
									
										
										
										
											2024-02-14 04:38:26 +08:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       throw new Error('Unsupported variable type'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-10 16:17:21 +08:00
										 |  |  |   // Remove some defaults
 | 
					
						
							|  |  |  |   for (const variable of variables) { | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |     if (variable.hide === OldVariableHide.dontHide) { | 
					
						
							| 
									
										
										
										
											2023-10-10 16:17:21 +08:00
										 |  |  |       delete variable.hide; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!variable.skipUrlSync) { | 
					
						
							|  |  |  |       delete variable.skipUrlSync; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (variable.label === '') { | 
					
						
							|  |  |  |       delete variable.label; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!variable.multi) { | 
					
						
							|  |  |  |       delete variable.multi; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |     if (variable.sort === OldVariableSort.disabled) { | 
					
						
							| 
									
										
										
										
											2023-10-10 16:17:21 +08:00
										 |  |  |       delete variable.sort; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-06 20:32:29 +08:00
										 |  |  |   return variables; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-03-21 23:35:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function variableValueOptionsToVariableOptions(varState: MultiValueVariable['state']): VariableOption[] { | 
					
						
							|  |  |  |   return varState.options.map((o) => ({ | 
					
						
							|  |  |  |     value: String(o.value), | 
					
						
							|  |  |  |     text: o.label, | 
					
						
							|  |  |  |     selected: Array.isArray(varState.value) ? varState.value.includes(o.value) : varState.value === o.value, | 
					
						
							|  |  |  |   })); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function sceneVariablesSetToSchemaV2Variables( | 
					
						
							|  |  |  |   set: SceneVariables, | 
					
						
							| 
									
										
										
										
											2025-03-26 23:59:34 +08:00
										 |  |  |   keepQueryOptions?: boolean, | 
					
						
							|  |  |  |   dsReferencesMapping?: DSReferencesMapping | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  | ): Array< | 
					
						
							|  |  |  |   | QueryVariableKind | 
					
						
							|  |  |  |   | TextVariableKind | 
					
						
							|  |  |  |   | IntervalVariableKind | 
					
						
							|  |  |  |   | DatasourceVariableKind | 
					
						
							|  |  |  |   | CustomVariableKind | 
					
						
							|  |  |  |   | ConstantVariableKind | 
					
						
							|  |  |  |   | GroupByVariableKind | 
					
						
							|  |  |  |   | AdhocVariableKind | 
					
						
							|  |  |  | > { | 
					
						
							|  |  |  |   let variables: Array< | 
					
						
							|  |  |  |     | QueryVariableKind | 
					
						
							|  |  |  |     | TextVariableKind | 
					
						
							|  |  |  |     | IntervalVariableKind | 
					
						
							|  |  |  |     | DatasourceVariableKind | 
					
						
							|  |  |  |     | CustomVariableKind | 
					
						
							|  |  |  |     | ConstantVariableKind | 
					
						
							|  |  |  |     | GroupByVariableKind | 
					
						
							|  |  |  |     | AdhocVariableKind | 
					
						
							|  |  |  |   > = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (const variable of set.state.variables) { | 
					
						
							|  |  |  |     const commonProperties = { | 
					
						
							|  |  |  |       name: variable.state.name, | 
					
						
							|  |  |  |       label: variable.state.label, | 
					
						
							|  |  |  |       description: variable.state.description ?? undefined, | 
					
						
							|  |  |  |       skipUrlSync: Boolean(variable.state.skipUrlSync), | 
					
						
							|  |  |  |       hide: transformVariableHideToEnum(variable.state.hide) || defaultVariableHide(), | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // current: VariableOption;
 | 
					
						
							|  |  |  |     const currentVariableOption: VariableOption = { | 
					
						
							|  |  |  |       // @ts-expect-error
 | 
					
						
							|  |  |  |       value: variable.state.value, | 
					
						
							|  |  |  |       // @ts-expect-error
 | 
					
						
							|  |  |  |       text: variable.state.text, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let options: VariableOption[] = []; | 
					
						
							|  |  |  |     if (sceneUtils.isQueryVariable(variable)) { | 
					
						
							|  |  |  |       // Not sure if we actually have to still support this option given
 | 
					
						
							|  |  |  |       // that it's not exposed in the UI
 | 
					
						
							| 
									
										
										
										
											2024-11-28 17:45:31 +08:00
										 |  |  |       if (transformVariableRefreshToEnum(variable.state.refresh) === 'never' || keepQueryOptions) { | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |         options = variableValueOptionsToVariableOptions(variable.state); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       const query = variable.state.query; | 
					
						
							|  |  |  |       let dataQuery: DataQueryKind | string; | 
					
						
							|  |  |  |       if (typeof query !== 'string') { | 
					
						
							|  |  |  |         dataQuery = { | 
					
						
							| 
									
										
										
										
											2025-02-06 22:33:06 +08:00
										 |  |  |           kind: variable.state.datasource?.type ?? getDataQueryKind(query), | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |           spec: getDataQuerySpec(query), | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2025-02-06 22:33:06 +08:00
										 |  |  |         dataQuery = { | 
					
						
							|  |  |  |           kind: variable.state.datasource?.type ?? getDataQueryKind(query), | 
					
						
							|  |  |  |           spec: { | 
					
						
							|  |  |  |             [LEGACY_STRING_VALUE_KEY]: query, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |       const queryVariable: QueryVariableKind = { | 
					
						
							|  |  |  |         kind: 'QueryVariable', | 
					
						
							|  |  |  |         spec: { | 
					
						
							|  |  |  |           ...commonProperties, | 
					
						
							|  |  |  |           current: currentVariableOption, | 
					
						
							|  |  |  |           options, | 
					
						
							|  |  |  |           query: dataQuery, | 
					
						
							|  |  |  |           definition: variable.state.definition, | 
					
						
							| 
									
										
										
										
											2025-03-26 23:59:34 +08:00
										 |  |  |           datasource: getElementDatasource(set, variable, 'variable', undefined, dsReferencesMapping), | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |           sort: transformSortVariableToEnum(variable.state.sort), | 
					
						
							|  |  |  |           refresh: transformVariableRefreshToEnum(variable.state.refresh), | 
					
						
							|  |  |  |           regex: variable.state.regex, | 
					
						
							|  |  |  |           allValue: variable.state.allValue, | 
					
						
							|  |  |  |           includeAll: variable.state.includeAll || false, | 
					
						
							|  |  |  |           multi: variable.state.isMulti || false, | 
					
						
							|  |  |  |           skipUrlSync: variable.state.skipUrlSync || false, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       variables.push(queryVariable); | 
					
						
							|  |  |  |     } else if (sceneUtils.isCustomVariable(variable)) { | 
					
						
							|  |  |  |       options = variableValueOptionsToVariableOptions(variable.state); | 
					
						
							|  |  |  |       const customVariable: CustomVariableKind = { | 
					
						
							|  |  |  |         kind: 'CustomVariable', | 
					
						
							|  |  |  |         spec: { | 
					
						
							|  |  |  |           ...commonProperties, | 
					
						
							|  |  |  |           current: currentVariableOption, | 
					
						
							|  |  |  |           options, | 
					
						
							|  |  |  |           query: variable.state.query, | 
					
						
							|  |  |  |           multi: variable.state.isMulti || false, | 
					
						
							|  |  |  |           allValue: variable.state.allValue, | 
					
						
							|  |  |  |           includeAll: variable.state.includeAll ?? false, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       variables.push(customVariable); | 
					
						
							|  |  |  |     } else if (sceneUtils.isDataSourceVariable(variable)) { | 
					
						
							|  |  |  |       const datasourceVariable: DatasourceVariableKind = { | 
					
						
							|  |  |  |         kind: 'DatasourceVariable', | 
					
						
							|  |  |  |         spec: { | 
					
						
							|  |  |  |           ...commonProperties, | 
					
						
							|  |  |  |           current: currentVariableOption, | 
					
						
							|  |  |  |           options: [], | 
					
						
							|  |  |  |           regex: variable.state.regex, | 
					
						
							| 
									
										
										
										
											2024-11-28 17:45:31 +08:00
										 |  |  |           refresh: 'onDashboardLoad', | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |           pluginId: variable.state.pluginId, | 
					
						
							|  |  |  |           multi: variable.state.isMulti || false, | 
					
						
							|  |  |  |           includeAll: variable.state.includeAll || false, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2025-01-08 01:54:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (variable.state.allValue !== undefined) { | 
					
						
							|  |  |  |         datasourceVariable.spec.allValue = variable.state.allValue; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |       variables.push(datasourceVariable); | 
					
						
							|  |  |  |     } else if (sceneUtils.isConstantVariable(variable)) { | 
					
						
							|  |  |  |       const constantVariable: ConstantVariableKind = { | 
					
						
							|  |  |  |         kind: 'ConstantVariable', | 
					
						
							|  |  |  |         spec: { | 
					
						
							|  |  |  |           ...commonProperties, | 
					
						
							| 
									
										
										
										
											2024-11-20 20:53:58 +08:00
										 |  |  |           current: { | 
					
						
							|  |  |  |             ...currentVariableOption, | 
					
						
							|  |  |  |             // Constant variable doesn't use text state
 | 
					
						
							|  |  |  |             text: String(variable.state.value), | 
					
						
							|  |  |  |           }, | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |           // @ts-expect-error
 | 
					
						
							|  |  |  |           query: variable.state.value, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       variables.push(constantVariable); | 
					
						
							|  |  |  |     } else if (sceneUtils.isIntervalVariable(variable)) { | 
					
						
							|  |  |  |       const intervals = getIntervalsQueryFromNewIntervalModel(variable.state.intervals); | 
					
						
							|  |  |  |       const intervalVariable: IntervalVariableKind = { | 
					
						
							|  |  |  |         kind: 'IntervalVariable', | 
					
						
							|  |  |  |         spec: { | 
					
						
							|  |  |  |           ...commonProperties, | 
					
						
							| 
									
										
										
										
											2024-11-20 20:53:58 +08:00
										 |  |  |           current: { | 
					
						
							|  |  |  |             ...currentVariableOption, | 
					
						
							|  |  |  |             // Interval variable doesn't use text state
 | 
					
						
							|  |  |  |             text: variable.state.value, | 
					
						
							|  |  |  |           }, | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |           query: intervals, | 
					
						
							| 
									
										
										
										
											2024-11-28 17:45:31 +08:00
										 |  |  |           refresh: 'onTimeRangeChanged', | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |           options: variable.state.intervals.map((interval) => ({ | 
					
						
							|  |  |  |             value: interval, | 
					
						
							|  |  |  |             text: interval, | 
					
						
							|  |  |  |             selected: interval === variable.state.value, | 
					
						
							|  |  |  |           })), | 
					
						
							|  |  |  |           auto: variable.state.autoEnabled, | 
					
						
							|  |  |  |           auto_min: variable.state.autoMinInterval, | 
					
						
							|  |  |  |           auto_count: variable.state.autoStepCount, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       variables.push(intervalVariable); | 
					
						
							|  |  |  |     } else if (sceneUtils.isTextBoxVariable(variable)) { | 
					
						
							|  |  |  |       const current = { | 
					
						
							|  |  |  |         text: variable.state.value, | 
					
						
							|  |  |  |         value: variable.state.value, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const textBoxVariable: TextVariableKind = { | 
					
						
							|  |  |  |         kind: 'TextVariable', | 
					
						
							|  |  |  |         spec: { | 
					
						
							|  |  |  |           ...commonProperties, | 
					
						
							|  |  |  |           current, | 
					
						
							|  |  |  |           query: variable.state.value, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       variables.push(textBoxVariable); | 
					
						
							|  |  |  |     } else if (sceneUtils.isGroupByVariable(variable) && config.featureToggles.groupByVariable) { | 
					
						
							|  |  |  |       options = variableValueOptionsToVariableOptions(variable.state); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const groupVariable: GroupByVariableKind = { | 
					
						
							|  |  |  |         kind: 'GroupByVariable', | 
					
						
							|  |  |  |         spec: { | 
					
						
							|  |  |  |           ...commonProperties, | 
					
						
							|  |  |  |           datasource: variable.state.datasource || {}, // FIXME what is the default value?,
 | 
					
						
							|  |  |  |           // Only persist the statically defined options
 | 
					
						
							|  |  |  |           options: | 
					
						
							|  |  |  |             variable.state.defaultOptions?.map((option) => ({ | 
					
						
							|  |  |  |               text: option.text, | 
					
						
							|  |  |  |               value: String(option.value), | 
					
						
							|  |  |  |             })) || [], | 
					
						
							|  |  |  |           current: currentVariableOption, | 
					
						
							|  |  |  |           multi: variable.state.isMulti || false, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       variables.push(groupVariable); | 
					
						
							|  |  |  |     } else if (sceneUtils.isAdHocVariable(variable)) { | 
					
						
							|  |  |  |       const adhocVariable: AdhocVariableKind = { | 
					
						
							|  |  |  |         kind: 'AdhocVariable', | 
					
						
							|  |  |  |         spec: { | 
					
						
							|  |  |  |           ...commonProperties, | 
					
						
							|  |  |  |           name: variable.state.name, | 
					
						
							|  |  |  |           datasource: variable.state.datasource || {}, //FIXME what is the default value?
 | 
					
						
							|  |  |  |           baseFilters: variable.state.baseFilters || [], | 
					
						
							|  |  |  |           filters: variable.state.filters, | 
					
						
							|  |  |  |           defaultKeys: variable.state.defaultKeys || [], //FIXME what is the default value?
 | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       variables.push(adhocVariable); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2024-11-20 20:53:58 +08:00
										 |  |  |       throw new Error('Unsupported variable type: ' + variable.state.type); | 
					
						
							| 
									
										
										
										
											2024-11-19 20:01:40 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return variables; | 
					
						
							|  |  |  | } |