Dashboards: Ensure panels have unique ids (#65468)

This commit is contained in:
Ryan McKinley 2023-03-29 07:47:13 -07:00 committed by GitHub
parent 07d960ac26
commit c0e7062eb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -66,6 +66,25 @@ describe('DashboardModel', () => {
});
});
describe('when initalized with duplicate panel ids', () => {
let model: DashboardModel;
beforeEach(() => {
model = createDashboardModelFixture({
panels: [
createPanelJSONFixture({ id: 6 }),
createPanelJSONFixture({ id: 2 }),
createPanelJSONFixture({}), // undefined
createPanelJSONFixture({ id: 2 }),
],
});
});
it('should ensure unique panel ids', () => {
expect(model.panels.map((p) => p.id)).toEqual([6, 2, 7, 8]);
});
});
describe('getSaveModelClone', () => {
it('should sort keys', () => {
const model = createDashboardModelFixture();

View File

@ -165,7 +165,7 @@ export class DashboardModel implements TimeModel {
this.links = data.links ?? [];
this.gnetId = data.gnetId || null;
this.panels = map(data.panels ?? [], (panelData: any) => new PanelModel(panelData));
this.ensurePanelsHaveIds();
this.ensurePanelsHaveUniqueIds();
this.formatDate = this.formatDate.bind(this);
this.resetOriginalVariables(true);
@ -447,10 +447,14 @@ export class DashboardModel implements TimeModel {
this.panelsAffectedByVariableChange = null;
}
private ensurePanelsHaveIds() {
private ensurePanelsHaveUniqueIds() {
const ids = new Set<number>();
let nextPanelId = this.getNextPanelId();
for (const panel of this.panelIterator()) {
panel.id ??= nextPanelId++;
if (!panel.id || ids.has(panel.id)) {
panel.id = nextPanelId++;
}
ids.add(panel.id);
}
}