Compare commits

...

1 Commits

Author SHA1 Message Date
Dominik Prokop
d88d213ce7 Fix GroupBy variable multi property default and v2 conversion
- Default GroupBy variable multi to true in v1 to v2 conversions (frontend and backend)
- Add multi property to v2 to v1 conversion for GroupBy variables
- Add test case for GroupBy variable without explicit multi property
- Update test data and expected outputs
2025-12-09 09:32:20 +01:00
8 changed files with 110 additions and 4 deletions

View File

@@ -258,6 +258,23 @@
"multi": true, "multi": true,
"skipUrlSync": false "skipUrlSync": false
}, },
{
"name": "groupby_var_no_multi",
"type": "groupby",
"label": "Group By Without Multi",
"description": "GroupBy variable without explicit multi - should default to true",
"datasource": {
"type": "prometheus",
"uid": "existing-ref-uid"
},
"current": {
"text": [],
"value": []
},
"options": [],
"hide": 0,
"skipUrlSync": false
},
{ {
"name": "switch_var", "name": "switch_var",
"type": "switch", "type": "switch",
@@ -354,4 +371,4 @@
}, },
"links": [] "links": []
} }
} }

View File

@@ -277,6 +277,23 @@
"skipUrlSync": false, "skipUrlSync": false,
"type": "groupby" "type": "groupby"
}, },
{
"current": {
"text": [],
"value": []
},
"datasource": {
"type": "prometheus",
"uid": "existing-ref-uid"
},
"description": "GroupBy variable without explicit multi - should default to true",
"hide": 0,
"label": "Group By Without Multi",
"name": "groupby_var_no_multi",
"options": [],
"skipUrlSync": false,
"type": "groupby"
},
{ {
"current": { "current": {
"selected": false, "selected": false,

View File

@@ -317,6 +317,26 @@
"description": "Group results by field" "description": "Group results by field"
} }
}, },
{
"kind": "GroupByVariable",
"spec": {
"name": "groupby_var_no_multi",
"datasource": {
"type": "prometheus",
"uid": "existing-ref-uid"
},
"current": {
"text": [],
"value": []
},
"options": [],
"multi": true,
"label": "Group By Without Multi",
"hide": "dontHide",
"skipUrlSync": false,
"description": "GroupBy variable without explicit multi - should default to true"
}
},
{ {
"kind": "SwitchVariable", "kind": "SwitchVariable",
"spec": { "spec": {

View File

@@ -318,6 +318,26 @@
"description": "Group results by field" "description": "Group results by field"
} }
}, },
{
"kind": "GroupByVariable",
"group": "prometheus",
"datasource": {
"name": "existing-ref-uid"
},
"spec": {
"name": "groupby_var_no_multi",
"current": {
"text": [],
"value": []
},
"options": [],
"multi": true,
"label": "Group By Without Multi",
"hide": "dontHide",
"skipUrlSync": false,
"description": "GroupBy variable without explicit multi - should default to true"
}
},
{ {
"kind": "SwitchVariable", "kind": "SwitchVariable",
"spec": { "spec": {

View File

@@ -1713,7 +1713,8 @@ func buildGroupByVariable(ctx context.Context, varMap map[string]interface{}, co
Hide: commonProps.Hide, Hide: commonProps.Hide,
SkipUrlSync: commonProps.SkipUrlSync, SkipUrlSync: commonProps.SkipUrlSync,
Current: buildVariableCurrent(varMap["current"]), Current: buildVariableCurrent(varMap["current"]),
Multi: getBoolField(varMap, "multi", false), // GroupBy variables are multi-select by default in Grafana
Multi: getBoolField(varMap, "multi", true),
}, },
} }

View File

@@ -419,7 +419,7 @@ export const handyTestingSchema: Spec = {
description: 'A group by variable', description: 'A group by variable',
hide: 'dontHide', hide: 'dontHide',
label: 'Group By Variable', label: 'Group By Variable',
multi: false, multi: true,
name: 'groupByVar', name: 'groupByVar',
options: [ options: [
{ {

View File

@@ -305,6 +305,7 @@ describe('ResponseTransformers', () => {
description: 'groupby var description', description: 'groupby var description',
skipUrlSync: false, skipUrlSync: false,
hide: 0, hide: 0,
multi: true,
datasource: { datasource: {
type: 'prometheus', type: 'prometheus',
uid: 'abc', uid: 'abc',
@@ -315,6 +316,21 @@ describe('ResponseTransformers', () => {
], ],
current: { value: ['1'], text: ['1'] }, current: { value: ['1'], text: ['1'] },
}, },
// GroupBy variable without explicit multi - should default to true
{
type: 'groupby',
name: 'var8_no_multi',
label: 'groupby var without multi',
skipUrlSync: false,
hide: 0,
// Note: no multi property - GroupBy variables are multi by default
datasource: {
type: 'prometheus',
uid: 'abc',
},
options: [],
current: { value: [], text: [] },
},
// Query variable with minimal props and without current // Query variable with minimal props and without current
{ {
datasource: { type: 'prometheus', uid: 'abc' }, datasource: { type: 'prometheus', uid: 'abc' },
@@ -562,6 +578,17 @@ describe('ResponseTransformers', () => {
validateVariablesV1ToV2(spec.variables[7], dashboardV1.templating?.list?.[7]); validateVariablesV1ToV2(spec.variables[7], dashboardV1.templating?.list?.[7]);
validateVariablesV1ToV2(spec.variables[8], dashboardV1.templating?.list?.[8]); validateVariablesV1ToV2(spec.variables[8], dashboardV1.templating?.list?.[8]);
validateVariablesV1ToV2(spec.variables[9], dashboardV1.templating?.list?.[9]); validateVariablesV1ToV2(spec.variables[9], dashboardV1.templating?.list?.[9]);
validateVariablesV1ToV2(spec.variables[10], dashboardV1.templating?.list?.[10]);
// Verify GroupBy variable without explicit multi defaults to true
const groupByNoMulti = dashboardV1.templating?.list?.[8];
expect(groupByNoMulti?.type).toBe('groupby');
expect(groupByNoMulti?.multi).toBeUndefined();
const v2GroupByNoMulti = spec.variables[8];
expect(v2GroupByNoMulti.kind).toBe('GroupByVariable');
// @ts-expect-error
expect(v2GroupByNoMulti.spec.multi).toBe(true);
}); });
}); });
@@ -1224,6 +1251,8 @@ describe('ResponseTransformers', () => {
expect(v2.datasource?.name).toEqual(v1.datasource?.uid); expect(v2.datasource?.name).toEqual(v1.datasource?.uid);
expect(v2.group).toEqual(v1.datasource?.type); expect(v2.group).toEqual(v1.datasource?.type);
expect(v2.spec.options).toEqual(v1.options); expect(v2.spec.options).toEqual(v1.options);
// GroupBy variables default to multi: true when not specified
expect(v2.spec.multi).toEqual(v1.multi ?? true);
} }
if (v2.kind === 'SwitchVariable') { if (v2.kind === 'SwitchVariable') {

View File

@@ -812,7 +812,8 @@ function getVariables(vars: TypedVariableModel[]): DashboardV2Spec['variables']
value: v.current.value, value: v.current.value,
text: v.current.text, text: v.current.text,
}, },
multi: v.multi, // GroupBy variables are multi-select by default in Grafana
multi: v.multi ?? true,
}, },
}; };
@@ -1013,6 +1014,7 @@ function getVariablesV1(vars: DashboardV2Spec['variables']): VariableModel[] {
}, },
current: v.spec.current, current: v.spec.current,
options: v.spec.options, options: v.spec.options,
multi: v.spec.multi,
}; };
variables.push(gv); variables.push(gv);
break; break;