Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dashboard: Migration - Dashboard Settings Variables (List, Duplicate, Delete) #78917

Merged
merged 28 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bdc6235
First attempt: List variables using SceneVariablesSet and also variab…
axelavargas Nov 30, 2023
bbf200b
Merge remote-tracking branch 'origin' into axelav/settings-variables
axelavargas Dec 6, 2023
16016d6
Merge remote-tracking branch 'origin' into axelav/settings-variables
axelavargas Dec 6, 2023
129527d
List variables and create Scaffold for Change order, duplicate and de…
axelavargas Dec 6, 2023
3fe0edf
Refactor: use right methods to get variables and implement onDelete
axelavargas Dec 7, 2023
cb1ce5a
Implement duplicate variables and clean up code
axelavargas Dec 8, 2023
8d92f02
Modify duplicated function to keep the same format as core grafana
axelavargas Dec 8, 2023
474e46b
Fix betterer and linter issues
axelavargas Dec 8, 2023
640e818
Add confirmation delete modal
axelavargas Dec 8, 2023
87c2297
Fix linter
axelavargas Dec 8, 2023
9e19e38
More linter...
axelavargas Dec 8, 2023
33d3040
Fix React removal
axelavargas Dec 8, 2023
55d1822
Merge remote-tracking branch 'origin' into axelav/settings-variables
axelavargas Dec 8, 2023
8b42494
Attempt to fix scenes types
axelavargas Dec 8, 2023
0be281c
Fix typescript error, pass variableScene to the component
axelavargas Dec 8, 2023
5db6c48
Disable create button and empty variable set
axelavargas Dec 12, 2023
aca9c65
Move methods to VariableEditView model
axelavargas Dec 12, 2023
f99ea12
Add unit test
axelavargas Dec 13, 2023
3052d19
Merge remote-tracking branch 'origin' into axelav/settings-variables
axelavargas Dec 13, 2023
db3e7a9
Fix linter
axelavargas Dec 13, 2023
db8ed9d
Use getDashboardSceneFor method in all DashboardSettings
axelavargas Dec 13, 2023
dfea723
Remove dashboardRef param from Dashboard Scene Url Sync
axelavargas Dec 13, 2023
bcd2a52
Add missing changes
axelavargas Dec 13, 2023
dccd570
Apply suggestions from code review
axelavargas Dec 13, 2023
3d835c2
Fix references of PR suggestion
axelavargas Dec 13, 2023
811347a
Add unit test for default variable set
axelavargas Dec 13, 2023
0c4f50d
Remove unnecesary function
axelavargas Dec 13, 2023
7edc34a
Remove unnecesary get _dashboard function
axelavargas Dec 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class DashboardSceneUrlSync implements SceneObjectUrlSyncHandler {
const update: Partial<DashboardSceneState> = {};

if (typeof values.editview === 'string' && meta.canEdit) {
update.editview = createDashboardEditViewFor(values.editview, this._scene.getRef());
update.editview = createDashboardEditViewFor(values.editview);

// If we are not in editing (for example after full page reload)
if (!isEditing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ export function createDashboardSceneFromDashboardModel(oldModel: DashboardModel)
variables = new SceneVariableSet({
variables: variableObjects,
});
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is mandatory to get always a valid value in variables, probably it is a good idea to add a test for it. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I have added the test :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, +1 to ivan's. Overall what we see is it's better to attach even empty objects rather than conditonally adding theme. It simplifies downstream code a lot.

// Create empty variable set
variables = new SceneVariableSet({
variables: [],
});
}

if (oldModel.annotations?.list?.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Page } from 'app/core/components/Page/Page';

import { DashboardScene } from '../scene/DashboardScene';
import { NavToolbarActions } from '../scene/NavToolbarActions';
import { getDashboardSceneFor } from '../utils/utils';

import { EditListViewSceneUrlSync } from './EditListViewSceneUrlSync';
import { DashboardEditView, DashboardEditListViewState, useDashboardEditPageNav } from './utils';
Expand All @@ -26,8 +27,8 @@ export class DashboardLinksEditView extends SceneObjectBase<DashboardLinksEditVi
}

function DashboardLinksEditViewRenderer({ model }: SceneComponentProps<DashboardLinksEditView>) {
const { dashboardRef, editIndex } = model.useState();
const dashboard = dashboardRef.resolve();
const { editIndex } = model.useState();
const dashboard = getDashboardSceneFor(model);
const links = dashboard.state.links || [];
const { navModel, pageNav } = useDashboardEditPageNav(dashboard, model.getUrlKey());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ describe('GeneralSettingsEditView', () => {
});

async function buildTestScene() {
const settings = new GeneralSettingsEditView({});
const dashboard = new DashboardScene({
$timeRange: new SceneTimeRange({}),
$behaviors: [new behaviors.CursorSync({ sync: DashboardCursorSync.Off })],
Expand Down Expand Up @@ -143,10 +144,7 @@ async function buildTestScene() {
}),
],
}),
});

const settings = new GeneralSettingsEditView({
dashboardRef: dashboard.getRef(),
editview: settings,
});

activateFullSceneTree(dashboard);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import React, { ChangeEvent } from 'react';

import { PageLayoutType } from '@grafana/data';
import {
behaviors,
SceneComponentProps,
SceneObjectBase,
SceneObjectRef,
SceneTimePicker,
sceneGraph,
} from '@grafana/scenes';
import { behaviors, SceneComponentProps, SceneObjectBase, SceneTimePicker, sceneGraph } from '@grafana/scenes';
import { TimeZone } from '@grafana/schema';
import {
Box,
Expand All @@ -31,12 +24,11 @@ import { DashboardControls } from '../scene/DashboardControls';
import { DashboardScene } from '../scene/DashboardScene';
import { NavToolbarActions } from '../scene/NavToolbarActions';
import { dashboardSceneGraph } from '../utils/dashboardSceneGraph';
import { getDashboardSceneFor } from '../utils/utils';

import { DashboardEditView, DashboardEditViewState, useDashboardEditPageNav } from './utils';

export interface GeneralSettingsEditViewState extends DashboardEditViewState {
dashboardRef: SceneObjectRef<DashboardScene>;
}
export interface GeneralSettingsEditViewState extends DashboardEditViewState {}

const EDITABLE_OPTIONS = [
{ label: 'Editable', value: true },
Expand All @@ -54,7 +46,7 @@ export class GeneralSettingsEditView
implements DashboardEditView
{
private get _dashboard(): DashboardScene {
return this.state.dashboardRef.resolve();
return getDashboardSceneFor(this);
}

public getUrlKey(): string {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { SceneVariableSet, CustomVariable, SceneGridItem, SceneGridLayout } from '@grafana/scenes';

import { DashboardScene } from '../scene/DashboardScene';
import { activateFullSceneTree } from '../utils/test-utils';

import { VariablesEditView } from './VariablesEditView';

describe('VariablesEditView', () => {
describe('Dashboard Variables state', () => {
let dashboard: DashboardScene;
let variableView: VariablesEditView;

beforeEach(async () => {
const result = await buildTestScene();
dashboard = result.dashboard;
variableView = result.variableView;
});

it('should return the correct urlKey', () => {
expect(variableView.getUrlKey()).toBe('variables');
});

it('should return the dashboard', () => {
expect(variableView.getDashboard()).toBe(dashboard);
});

it('should return the list of variables', () => {
const expectedVariables = [
{
type: 'custom',
name: 'customVar',
query: 'test, test2',
value: 'test',
},
{
type: 'custom',
name: 'customVar2',
query: 'test3, test4',
value: 'test3',
},
];
const variables = variableView.getVariables();
expect(variables).toHaveLength(2);
expect(variables[0].state).toMatchObject(expectedVariables[0]);
expect(variables[1].state).toMatchObject(expectedVariables[1]);
});
});

describe('Dashboard Variables actions', () => {
let variableView: VariablesEditView;

beforeEach(async () => {
const result = await buildTestScene();
variableView = result.variableView;
});

it('should duplicate a variable', () => {
const variables = variableView.getVariables();
const variable = variables[0];
variableView.onDuplicated(variable.state.name);
expect(variableView.getVariables()).toHaveLength(3);
expect(variableView.getVariables()[1].state.name).toBe('copy_of_customVar');
});

it('should handle name when duplicating a variable twice', () => {
const variableIdentifier = 'customVar';
variableView.onDuplicated(variableIdentifier);
variableView.onDuplicated(variableIdentifier);
expect(variableView.getVariables()).toHaveLength(4);
expect(variableView.getVariables()[1].state.name).toBe('copy_of_customVar_1');
expect(variableView.getVariables()[2].state.name).toBe('copy_of_customVar');
});

it('should delete a variable', () => {
const variableIdentifier = 'customVar';
variableView.onDelete(variableIdentifier);
expect(variableView.getVariables()).toHaveLength(1);
expect(variableView.getVariables()[0].state.name).toBe('customVar2');
});

it('should change order of variables', () => {
const fromIndex = 0; // customVar is first
const toIndex = 1;
variableView.onOrderChanged(fromIndex, toIndex);
expect(variableView.getVariables()[0].state.name).toBe('customVar2');
expect(variableView.getVariables()[1].state.name).toBe('customVar');
});

it('should keep the same order of variables with invalid indexes', () => {
const fromIndex = 0;
const toIndex = 2;

const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

variableView.onOrderChanged(fromIndex, toIndex);
expect(errorSpy).toHaveBeenCalledTimes(1);
expect(variableView.getVariables()[0].state.name).toBe('customVar');
expect(variableView.getVariables()[1].state.name).toBe('customVar2');

errorSpy.mockRestore();
});
});
});

async function buildTestScene() {
const variableView = new VariablesEditView({});
const dashboard = new DashboardScene({
title: 'Dashboard with variables',
uid: 'dash-variables',
meta: {
canEdit: true,
},
$variables: new SceneVariableSet({
variables: [
new CustomVariable({
name: 'customVar',
query: 'test, test2',
}),
new CustomVariable({
name: 'customVar2',
query: 'test3, test4',
}),
],
}),
body: new SceneGridLayout({
children: [
new SceneGridItem({
key: 'griditem-1',
x: 0,
y: 0,
width: 10,
height: 12,
body: undefined,
}),
],
}),
editview: variableView,
});

activateFullSceneTree(dashboard);

await new Promise((r) => setTimeout(r, 1));

dashboard.onEnterEditMode();
variableView.activate();

return { dashboard, variableView };
}
Loading
Loading