forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] [Dashboards] Add getSerializedState method to Dashboard API (el…
…astic#204140) (elastic#205010) # Backport This will backport the following commits from `main` to `8.x`: - [[Dashboards] Add getSerializedState method to Dashboard API (elastic#204140)](elastic#204140) <!--- Backport version: 8.9.8 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Nick Peihl","email":"nick.peihl@elastic.co"},"sourceCommit":{"committedDate":"2024-12-19T20:24:49Z","message":"[Dashboards] Add getSerializedState method to Dashboard API (elastic#204140)\n\nAdds a `getSerializedState` method to the Dashboard API.","sha":"d7280a1380d66107a6818b1b84358c1762c20bb9","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Presentation","release_note:skip","v9.0.0","backport:prev-minor","v8.18.0"],"number":204140,"url":"https://github.com/elastic/kibana/pull/204140","mergeCommit":{"message":"[Dashboards] Add getSerializedState method to Dashboard API (elastic#204140)\n\nAdds a `getSerializedState` method to the Dashboard API.","sha":"d7280a1380d66107a6818b1b84358c1762c20bb9"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/204140","number":204140,"mergeCommit":{"message":"[Dashboards] Add getSerializedState method to Dashboard API (elastic#204140)\n\nAdds a `getSerializedState` method to the Dashboard API.","sha":"d7280a1380d66107a6818b1b84358c1762c20bb9"}},{"branch":"8.x","label":"v8.18.0","labelRegex":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT-->
- Loading branch information
Showing
10 changed files
with
397 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
src/plugins/dashboard/public/dashboard_api/get_serialized_state.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import type { DashboardPanelState } from '../../common'; | ||
|
||
import { | ||
dataService, | ||
embeddableService, | ||
savedObjectsTaggingService, | ||
} from '../services/kibana_services'; | ||
import { getSampleDashboardState } from '../mocks'; | ||
import { getSerializedState } from './get_serialized_state'; | ||
|
||
dataService.search.searchSource.create = jest.fn().mockResolvedValue({ | ||
setField: jest.fn(), | ||
getSerializedFields: jest.fn().mockReturnValue({}), | ||
}); | ||
|
||
dataService.query.timefilter.timefilter.getTime = jest | ||
.fn() | ||
.mockReturnValue({ from: 'now-15m', to: 'now' }); | ||
|
||
dataService.query.timefilter.timefilter.getRefreshInterval = jest | ||
.fn() | ||
.mockReturnValue({ pause: true, value: 0 }); | ||
|
||
embeddableService.extract = jest | ||
.fn() | ||
.mockImplementation((attributes) => ({ state: attributes, references: [] })); | ||
|
||
if (savedObjectsTaggingService) { | ||
savedObjectsTaggingService.getTaggingApi = jest.fn().mockReturnValue({ | ||
ui: { | ||
updateTagsReferences: jest.fn((references, tags) => references), | ||
}, | ||
}); | ||
} | ||
|
||
jest.mock('uuid', () => ({ | ||
v4: jest.fn().mockReturnValue('54321'), | ||
})); | ||
|
||
describe('getSerializedState', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return the current state attributes and references', () => { | ||
const dashboardState = getSampleDashboardState(); | ||
const result = getSerializedState({ | ||
controlGroupReferences: [], | ||
generateNewIds: false, | ||
dashboardState, | ||
panelReferences: [], | ||
searchSourceReferences: [], | ||
}); | ||
|
||
expect(result.attributes).toMatchInlineSnapshot(` | ||
Object { | ||
"controlGroupInput": undefined, | ||
"description": "", | ||
"kibanaSavedObjectMeta": Object { | ||
"searchSource": Object { | ||
"filter": Array [], | ||
"query": Object { | ||
"language": "kuery", | ||
"query": "hi", | ||
}, | ||
}, | ||
}, | ||
"options": Object { | ||
"hidePanelTitles": false, | ||
"syncColors": false, | ||
"syncCursor": true, | ||
"syncTooltips": false, | ||
"useMargins": true, | ||
}, | ||
"panels": Array [], | ||
"refreshInterval": undefined, | ||
"timeFrom": undefined, | ||
"timeRestore": false, | ||
"timeTo": undefined, | ||
"title": "My Dashboard", | ||
"version": 3, | ||
} | ||
`); | ||
expect(result.references).toEqual([]); | ||
}); | ||
|
||
it('should generate new IDs for panels and references when generateNewIds is true', () => { | ||
const dashboardState = { | ||
...getSampleDashboardState(), | ||
panels: { oldPanelId: { type: 'visualization' } as unknown as DashboardPanelState }, | ||
}; | ||
const result = getSerializedState({ | ||
controlGroupReferences: [], | ||
generateNewIds: true, | ||
dashboardState, | ||
panelReferences: [ | ||
{ | ||
name: 'oldPanelId:indexpattern_foobar', | ||
type: 'index-pattern', | ||
id: 'bizzbuzz', | ||
}, | ||
], | ||
searchSourceReferences: [], | ||
}); | ||
|
||
expect(result.attributes.panels).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"gridData": Object { | ||
"i": "54321", | ||
}, | ||
"panelConfig": Object {}, | ||
"panelIndex": "54321", | ||
"type": "visualization", | ||
"version": undefined, | ||
}, | ||
] | ||
`); | ||
expect(result.references).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"id": "bizzbuzz", | ||
"name": "54321:indexpattern_foobar", | ||
"type": "index-pattern", | ||
}, | ||
] | ||
`); | ||
}); | ||
|
||
it('should include control group references', () => { | ||
const dashboardState = getSampleDashboardState(); | ||
const controlGroupReferences = [ | ||
{ name: 'control1:indexpattern', type: 'index-pattern', id: 'foobar' }, | ||
]; | ||
const result = getSerializedState({ | ||
controlGroupReferences, | ||
generateNewIds: false, | ||
dashboardState, | ||
panelReferences: [], | ||
searchSourceReferences: [], | ||
}); | ||
|
||
expect(result.references).toEqual(controlGroupReferences); | ||
}); | ||
|
||
it('should include panel references', () => { | ||
const dashboardState = getSampleDashboardState(); | ||
const panelReferences = [ | ||
{ name: 'panel1:boogiewoogie', type: 'index-pattern', id: 'fizzbuzz' }, | ||
]; | ||
const result = getSerializedState({ | ||
controlGroupReferences: [], | ||
generateNewIds: false, | ||
dashboardState, | ||
panelReferences, | ||
searchSourceReferences: [], | ||
}); | ||
|
||
expect(result.references).toEqual(panelReferences); | ||
}); | ||
}); |
150 changes: 150 additions & 0 deletions
150
src/plugins/dashboard/public/dashboard_api/get_serialized_state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { pick } from 'lodash'; | ||
import moment, { Moment } from 'moment'; | ||
import { RefreshInterval } from '@kbn/data-plugin/public'; | ||
|
||
import type { Reference } from '@kbn/content-management-utils'; | ||
import { convertPanelMapToPanelsArray, extractReferences, generateNewPanelIds } from '../../common'; | ||
import type { DashboardAttributes } from '../../server'; | ||
|
||
import { convertDashboardVersionToNumber } from '../services/dashboard_content_management_service/lib/dashboard_versioning'; | ||
import { | ||
dataService, | ||
embeddableService, | ||
savedObjectsTaggingService, | ||
} from '../services/kibana_services'; | ||
import { LATEST_DASHBOARD_CONTAINER_VERSION } from '../dashboard_container'; | ||
import { DashboardState } from './types'; | ||
|
||
export const convertTimeToUTCString = (time?: string | Moment): undefined | string => { | ||
if (moment(time).isValid()) { | ||
return moment(time).utc().format('YYYY-MM-DDTHH:mm:ss.SSS[Z]'); | ||
} else { | ||
// If it's not a valid moment date, then it should be a string representing a relative time | ||
// like 'now' or 'now-15m'. | ||
return time as string; | ||
} | ||
}; | ||
|
||
export const getSerializedState = ({ | ||
controlGroupReferences, | ||
generateNewIds, | ||
dashboardState, | ||
panelReferences, | ||
searchSourceReferences, | ||
}: { | ||
controlGroupReferences?: Reference[]; | ||
generateNewIds?: boolean; | ||
dashboardState: DashboardState; | ||
panelReferences?: Reference[]; | ||
searchSourceReferences?: Reference[]; | ||
}) => { | ||
const { | ||
query: { | ||
timefilter: { timefilter }, | ||
}, | ||
} = dataService; | ||
|
||
const { | ||
tags, | ||
query, | ||
title, | ||
filters, | ||
timeRestore, | ||
description, | ||
|
||
// Dashboard options | ||
useMargins, | ||
syncColors, | ||
syncCursor, | ||
syncTooltips, | ||
hidePanelTitles, | ||
controlGroupInput, | ||
} = dashboardState; | ||
|
||
let { panels } = dashboardState; | ||
let prefixedPanelReferences = panelReferences; | ||
if (generateNewIds) { | ||
const { panels: newPanels, references: newPanelReferences } = generateNewPanelIds( | ||
panels, | ||
panelReferences | ||
); | ||
panels = newPanels; | ||
prefixedPanelReferences = newPanelReferences; | ||
// | ||
// do not need to generate new ids for controls. | ||
// ControlGroup Component is keyed on dashboard id so changing dashboard id mounts new ControlGroup Component. | ||
// | ||
} | ||
|
||
const searchSource = { filter: filters, query }; | ||
const options = { | ||
useMargins, | ||
syncColors, | ||
syncCursor, | ||
syncTooltips, | ||
hidePanelTitles, | ||
}; | ||
const savedPanels = convertPanelMapToPanelsArray(panels, true); | ||
|
||
/** | ||
* Parse global time filter settings | ||
*/ | ||
const { from, to } = timefilter.getTime(); | ||
const timeFrom = timeRestore ? convertTimeToUTCString(from) : undefined; | ||
const timeTo = timeRestore ? convertTimeToUTCString(to) : undefined; | ||
const refreshInterval = timeRestore | ||
? (pick(timefilter.getRefreshInterval(), [ | ||
'display', | ||
'pause', | ||
'section', | ||
'value', | ||
]) as RefreshInterval) | ||
: undefined; | ||
|
||
const rawDashboardAttributes: DashboardAttributes = { | ||
version: convertDashboardVersionToNumber(LATEST_DASHBOARD_CONTAINER_VERSION), | ||
controlGroupInput: controlGroupInput as DashboardAttributes['controlGroupInput'], | ||
kibanaSavedObjectMeta: { searchSource }, | ||
description: description ?? '', | ||
refreshInterval, | ||
timeRestore, | ||
options, | ||
panels: savedPanels, | ||
timeFrom, | ||
title, | ||
timeTo, | ||
}; | ||
|
||
/** | ||
* Extract references from raw attributes and tags into the references array. | ||
*/ | ||
const { attributes, references: dashboardReferences } = extractReferences( | ||
{ | ||
attributes: rawDashboardAttributes, | ||
references: searchSourceReferences ?? [], | ||
}, | ||
{ embeddablePersistableStateService: embeddableService } | ||
); | ||
|
||
const savedObjectsTaggingApi = savedObjectsTaggingService?.getTaggingApi(); | ||
const references = savedObjectsTaggingApi?.ui.updateTagsReferences | ||
? savedObjectsTaggingApi?.ui.updateTagsReferences(dashboardReferences, tags) | ||
: dashboardReferences; | ||
|
||
const allReferences = [ | ||
...references, | ||
...(prefixedPanelReferences ?? []), | ||
...(controlGroupReferences ?? []), | ||
...(searchSourceReferences ?? []), | ||
]; | ||
return { attributes, references: allReferences }; | ||
}; |
Oops, something went wrong.