mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
GroupBy variable: Allow detecting static dimensions changes (#84715)
* GroupBy variable: Allow detecting static dimensions changes * Review * Lint
This commit is contained in:
parent
77b1c97482
commit
d5e293c18d
@ -69,8 +69,10 @@ export function applyVariableChanges(saveModel: Dashboard, originalSaveModel: Da
|
|||||||
if (typed.type === 'adhoc') {
|
if (typed.type === 'adhoc') {
|
||||||
typed.filters = (original as AdHocVariableModel).filters;
|
typed.filters = (original as AdHocVariableModel).filters;
|
||||||
} else {
|
} else {
|
||||||
variable.current = original.current;
|
if (typed.type !== 'groupby') {
|
||||||
variable.options = original.options;
|
variable.current = original.current;
|
||||||
|
variable.options = original.options;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import { MultiValueVariable, sceneGraph } from '@grafana/scenes';
|
import { config } from '@grafana/runtime';
|
||||||
|
import { AdHocFiltersVariable, GroupByVariable, MultiValueVariable, sceneGraph } from '@grafana/scenes';
|
||||||
|
import { VariableModel } from '@grafana/schema';
|
||||||
|
|
||||||
import { buildPanelEditScene } from '../panel-edit/PanelEditor';
|
import { buildPanelEditScene } from '../panel-edit/PanelEditor';
|
||||||
import { transformSaveModelToScene } from '../serialization/transformSaveModelToScene';
|
import { transformSaveModelToScene } from '../serialization/transformSaveModelToScene';
|
||||||
@ -36,30 +38,150 @@ describe('getDashboardChangesFromScene', () => {
|
|||||||
expect(result.diffCount).toBe(1);
|
expect(result.diffCount).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can detect variable change', () => {
|
describe('variable changes', () => {
|
||||||
const dashboard = setup();
|
it('Can detect variable change', () => {
|
||||||
|
const dashboard = setup();
|
||||||
|
|
||||||
const appVar = sceneGraph.lookupVariable('app', dashboard) as MultiValueVariable;
|
const appVar = sceneGraph.lookupVariable('app', dashboard) as MultiValueVariable;
|
||||||
appVar.changeValueTo('app2');
|
appVar.changeValueTo('app2');
|
||||||
|
|
||||||
const result = getDashboardChangesFromScene(dashboard, false, false);
|
const result = getDashboardChangesFromScene(dashboard, false, false);
|
||||||
|
|
||||||
expect(result.hasVariableValueChanges).toBe(true);
|
expect(result.hasVariableValueChanges).toBe(true);
|
||||||
expect(result.hasChanges).toBe(false);
|
expect(result.hasChanges).toBe(false);
|
||||||
expect(result.diffCount).toBe(0);
|
expect(result.diffCount).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can save variable value change', () => {
|
it('Can save variable value change', () => {
|
||||||
const dashboard = setup();
|
const dashboard = setup();
|
||||||
|
|
||||||
const appVar = sceneGraph.lookupVariable('app', dashboard) as MultiValueVariable;
|
const appVar = sceneGraph.lookupVariable('app', dashboard) as MultiValueVariable;
|
||||||
appVar.changeValueTo('app2');
|
appVar.changeValueTo('app2');
|
||||||
|
|
||||||
const result = getDashboardChangesFromScene(dashboard, false, true);
|
const result = getDashboardChangesFromScene(dashboard, false, true);
|
||||||
|
|
||||||
expect(result.hasVariableValueChanges).toBe(true);
|
expect(result.hasVariableValueChanges).toBe(true);
|
||||||
expect(result.hasChanges).toBe(true);
|
expect(result.hasChanges).toBe(true);
|
||||||
expect(result.diffCount).toBe(2);
|
expect(result.diffCount).toBe(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Experimental variables', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
config.featureToggles.groupByVariable = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
config.featureToggles.groupByVariable = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Can detect group by static options change', () => {
|
||||||
|
const dashboard = transformSaveModelToScene({
|
||||||
|
dashboard: {
|
||||||
|
title: 'hello',
|
||||||
|
uid: 'my-uid',
|
||||||
|
schemaVersion: 30,
|
||||||
|
panels: [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
title: 'Panel 1',
|
||||||
|
type: 'text',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
version: 10,
|
||||||
|
templating: {
|
||||||
|
list: [
|
||||||
|
{
|
||||||
|
type: 'groupby',
|
||||||
|
datasource: {
|
||||||
|
type: 'ds',
|
||||||
|
uid: 'ds-uid',
|
||||||
|
},
|
||||||
|
name: 'GroupBy',
|
||||||
|
options: [
|
||||||
|
{
|
||||||
|
text: 'Host',
|
||||||
|
value: 'host',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: 'Region',
|
||||||
|
value: 'region',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
meta: {},
|
||||||
|
});
|
||||||
|
const initialSaveModel = transformSceneToSaveModel(dashboard);
|
||||||
|
dashboard.setInitialSaveModel(initialSaveModel);
|
||||||
|
|
||||||
|
const variable = sceneGraph.lookupVariable('GroupBy', dashboard) as GroupByVariable;
|
||||||
|
variable.setState({ defaultOptions: [{ text: 'Host', value: 'host' }] });
|
||||||
|
const result = getDashboardChangesFromScene(dashboard, false, false);
|
||||||
|
|
||||||
|
expect(result.hasVariableValueChanges).toBe(false);
|
||||||
|
expect(result.hasChanges).toBe(true);
|
||||||
|
expect(result.diffCount).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Can detect adhoc filter static options change', () => {
|
||||||
|
const adhocVar = {
|
||||||
|
id: 'adhoc',
|
||||||
|
name: 'adhoc',
|
||||||
|
label: 'Adhoc Label',
|
||||||
|
description: 'Adhoc Description',
|
||||||
|
type: 'adhoc',
|
||||||
|
datasource: {
|
||||||
|
uid: 'gdev-prometheus',
|
||||||
|
type: 'prometheus',
|
||||||
|
},
|
||||||
|
filters: [],
|
||||||
|
baseFilters: [],
|
||||||
|
defaultKeys: [
|
||||||
|
{
|
||||||
|
text: 'Host',
|
||||||
|
value: 'host',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: 'Region',
|
||||||
|
value: 'region',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
} as VariableModel;
|
||||||
|
|
||||||
|
const dashboard = transformSaveModelToScene({
|
||||||
|
dashboard: {
|
||||||
|
title: 'hello',
|
||||||
|
uid: 'my-uid',
|
||||||
|
schemaVersion: 30,
|
||||||
|
panels: [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
title: 'Panel 1',
|
||||||
|
type: 'text',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
version: 10,
|
||||||
|
templating: {
|
||||||
|
list: [adhocVar],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
meta: {},
|
||||||
|
});
|
||||||
|
|
||||||
|
const initialSaveModel = transformSceneToSaveModel(dashboard);
|
||||||
|
dashboard.setInitialSaveModel(initialSaveModel);
|
||||||
|
|
||||||
|
const variable = sceneGraph.lookupVariable('adhoc', dashboard) as AdHocFiltersVariable;
|
||||||
|
variable.setState({ defaultKeys: [{ text: 'Host', value: 'host' }] });
|
||||||
|
const result = getDashboardChangesFromScene(dashboard, false, false);
|
||||||
|
|
||||||
|
expect(result.hasVariableValueChanges).toBe(false);
|
||||||
|
expect(result.hasChanges).toBe(true);
|
||||||
|
expect(result.diffCount).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Saving from panel edit', () => {
|
describe('Saving from panel edit', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user