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';
|
|
|
|
|
import { VariableHide, VariableModel, VariableOption, VariableRefresh, VariableSort } from '@grafana/schema';
|
2023-10-06 20:32:29 +08:00
|
|
|
|
2023-10-16 23:34:09 +08:00
|
|
|
import { getIntervalsQueryFromNewIntervalModel } from '../utils/utils';
|
|
|
|
|
|
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),
|
|
|
|
|
hide: variable.state.hide || VariableHide.dontHide,
|
2023-11-22 16:47:44 +08:00
|
|
|
type: variable.state.type,
|
2023-10-06 20:32:29 +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-10-05 01:19:18 +08:00
|
|
|
if (variable.state.refresh === VariableRefresh.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,
|
2023-11-22 16:47:44 +08:00
|
|
|
datasource: variable.state.datasource,
|
|
|
|
|
sort: variable.state.sort,
|
|
|
|
|
refresh: variable.state.refresh,
|
|
|
|
|
regex: variable.state.regex,
|
|
|
|
|
allValue: variable.state.allValue,
|
|
|
|
|
includeAll: variable.state.includeAll,
|
|
|
|
|
multi: variable.state.isMulti,
|
|
|
|
|
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,
|
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,
|
2023-10-11 19:24:18 +08:00
|
|
|
refresh: VariableRefresh.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,
|
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,
|
2023-10-06 20:32:29 +08:00
|
|
|
hide: VariableHide.hideVariable,
|
|
|
|
|
});
|
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-02-14 04:38:26 +08:00
|
|
|
} else if (sceneUtils.isAdHocVariable(variable)) {
|
|
|
|
|
variables.push({
|
|
|
|
|
...commonProperties,
|
2024-03-19 00:12:00 +08:00
|
|
|
name: variable.state.name,
|
2024-02-14 04:38:26 +08:00
|
|
|
type: 'adhoc',
|
|
|
|
|
datasource: variable.state.datasource,
|
|
|
|
|
// @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) {
|
|
|
|
|
if (variable.hide === VariableHide.dontHide) {
|
|
|
|
|
delete variable.hide;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!variable.skipUrlSync) {
|
|
|
|
|
delete variable.skipUrlSync;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (variable.label === '') {
|
|
|
|
|
delete variable.label;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!variable.multi) {
|
|
|
|
|
delete variable.multi;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (variable.sort === VariableSort.disabled) {
|
|
|
|
|
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,
|
|
|
|
|
}));
|
|
|
|
|
}
|