DashboardModel - Add fallback for variable current value fallback (#91833)

* add variable current value fallback when null

* refactor to avoid loosing reference
This commit is contained in:
Sergej-Vlasov 2024-08-20 11:55:04 +03:00 committed by GitHub
parent 2e9cbda448
commit 7c8184d5bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -19,7 +19,7 @@ import {
} from '@grafana/data'; } from '@grafana/data';
import { PromQuery } from '@grafana/prometheus'; import { PromQuery } from '@grafana/prometheus';
import { RefreshEvent, TimeRangeUpdatedEvent, config } from '@grafana/runtime'; import { RefreshEvent, TimeRangeUpdatedEvent, config } from '@grafana/runtime';
import { Dashboard, DashboardLink } from '@grafana/schema'; import { Dashboard, DashboardLink, VariableModel } from '@grafana/schema';
import { DEFAULT_ANNOTATION_COLOR } from '@grafana/ui'; import { DEFAULT_ANNOTATION_COLOR } from '@grafana/ui';
import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT, REPEAT_DIR_VERTICAL } from 'app/core/constants'; import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT, REPEAT_DIR_VERTICAL } from 'app/core/constants';
import { contextSrv } from 'app/core/services/context_srv'; import { contextSrv } from 'app/core/services/context_srv';
@ -145,7 +145,7 @@ export class DashboardModel implements TimeModel {
this.time = data.time ?? { from: 'now-6h', to: 'now' }; this.time = data.time ?? { from: 'now-6h', to: 'now' };
this.timepicker = data.timepicker ?? {}; this.timepicker = data.timepicker ?? {};
this.liveNow = data.liveNow; this.liveNow = data.liveNow;
this.templating = this.ensureListExist(data.templating); this.templating = this.removeNullValuesFromVariables(this.ensureListExist(data.templating));
this.annotations = this.ensureListExist(data.annotations); this.annotations = this.ensureListExist(data.annotations);
this.refresh = data.refresh; this.refresh = data.refresh;
this.snapshot = data.snapshot; this.snapshot = data.snapshot;
@ -471,6 +471,28 @@ export class DashboardModel implements TimeModel {
} }
} }
private removeNullValuesFromVariables(templating: { list: VariableModel[] }) {
if (!templating.list.length) {
return templating;
}
for (const variable of templating.list) {
if (variable.current) {
// this is a safeguard for null value that breaks scenes dashboards.
// expecting error at .includes(null) in order to not adjust
// VariableOption type to avoid breaking changes
if (
variable.current.value === null ||
//@ts-expect-error
(Array.isArray(variable.current.value) && variable.current.value.includes(null))
) {
variable.current = undefined;
}
}
}
return templating;
}
private ensureListExist(data: any = {}) { private ensureListExist(data: any = {}) {
data.list ??= []; data.list ??= [];
return data; return data;