-
Notifications
You must be signed in to change notification settings - Fork 916
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
[Workspace]Import sample data to current workspace #6105
Changes from 3 commits
4321cb4
d29f691
54adb59
5240160
ee39682
b1da07e
b4c0c27
0d44b0f
e4a6c74
9b3be8f
5098c09
58adba2
0e7c5e1
f11844f
efc5b1c
8b67b94
e87cb72
799f475
fb4c3a2
5767ffc
eda80ff
dd998f3
7d7a831
a7e8c72
9769622
28d1f74
6162c2d
b82fcec
5f8f9f4
3f27d71
4d560c6
9a2bd3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ import { i18n } from '@osd/i18n'; | |
import { getSavedObjects } from './saved_objects'; | ||
import { fieldMappings } from './field_mappings'; | ||
import { SampleDatasetSchema, AppLinkSchema } from '../../lib/sample_dataset_registry_types'; | ||
import { getSavedObjectsWithDataSource, appendDataSourceId } from '../util'; | ||
import { addPrefixTo } from '../util'; | ||
|
||
const ecommerceName = i18n.translate('home.sampleData.ecommerceSpecTitle', { | ||
defaultMessage: 'Sample eCommerce orders', | ||
|
@@ -55,13 +55,11 @@ export const ecommerceSpecProvider = function (): SampleDatasetSchema { | |
darkPreviewImagePath: '/plugins/home/assets/sample_data_resources/ecommerce/dashboard_dark.png', | ||
hasNewThemeImages: true, | ||
overviewDashboard: DASHBOARD_ID, | ||
getDataSourceIntegratedDashboard: appendDataSourceId(DASHBOARD_ID), | ||
getDashboardWithPrefix: addPrefixTo(DASHBOARD_ID), | ||
appLinks: initialAppLinks, | ||
defaultIndex: DEFAULT_INDEX, | ||
getDataSourceIntegratedDefaultIndex: appendDataSourceId(DEFAULT_INDEX), | ||
getDataSourceIntegratedDefaultIndex: addPrefixTo(DEFAULT_INDEX), | ||
savedObjects: getSavedObjects(), | ||
getDataSourceIntegratedSavedObjects: (dataSourceId?: string, dataSourceTitle?: string) => | ||
getSavedObjectsWithDataSource(getSavedObjects(), dataSourceId, dataSourceTitle), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it save to delete this field? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method has been moved to |
||
dataIndices: [ | ||
{ | ||
id: 'ecommerce', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,59 +5,74 @@ | |
|
||
import { SavedObject } from 'opensearch-dashboards/server'; | ||
|
||
export const appendDataSourceId = (id: string) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems this logic has gone? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. This function will be refactor to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any updates on this comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
return (dataSourceId?: string) => (dataSourceId ? `${dataSourceId}_` + id : id); | ||
const cloneDeep = <T extends unknown>(payload: T): T => JSON.parse(JSON.stringify(payload)); | ||
|
||
const withPrefix = (...args: Array<string | undefined>) => (id: string) => { | ||
const prefix = args.filter(Boolean).join('_'); | ||
if (prefix) { | ||
return `${prefix}_${id}`; | ||
} | ||
return id; | ||
}; | ||
|
||
export const getSavedObjectsWithDataSource = ( | ||
saveObjectList: SavedObject[], | ||
dataSourceId?: string, | ||
dataSourceTitle?: string | ||
): SavedObject[] => { | ||
if (dataSourceId) { | ||
return saveObjectList.map((saveObject) => { | ||
saveObject.id = `${dataSourceId}_` + saveObject.id; | ||
// update reference | ||
if (saveObject.type === 'dashboard') { | ||
saveObject.references.map((reference) => { | ||
if (reference.id) { | ||
reference.id = `${dataSourceId}_` + reference.id; | ||
} | ||
}); | ||
export const addPrefixTo = (id: string) => (...args: Array<string | undefined>) => { | ||
return withPrefix(...args)(id); | ||
}; | ||
|
||
const overrideSavedObjectId = (savedObject: SavedObject, idGenerator: (id: string) => string) => { | ||
savedObject.id = idGenerator(savedObject.id); | ||
// update reference | ||
if (savedObject.type === 'dashboard') { | ||
savedObject.references.map((reference) => { | ||
if (reference.id) { | ||
reference.id = idGenerator(reference.id); | ||
} | ||
}); | ||
} | ||
|
||
// update reference | ||
if (saveObject.type === 'visualization' || saveObject.type === 'search') { | ||
const searchSourceString = saveObject.attributes?.kibanaSavedObjectMeta?.searchSourceJSON; | ||
const visStateString = saveObject.attributes?.visState; | ||
// update reference | ||
if (savedObject.type === 'visualization' || savedObject.type === 'search') { | ||
const searchSourceString = savedObject.attributes?.kibanaSavedObjectMeta?.searchSourceJSON; | ||
const visStateString = savedObject.attributes?.visState; | ||
|
||
if (searchSourceString) { | ||
const searchSource = JSON.parse(searchSourceString); | ||
if (searchSource.index) { | ||
searchSource.index = `${dataSourceId}_` + searchSource.index; | ||
saveObject.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.stringify( | ||
searchSource | ||
); | ||
} | ||
} | ||
if (searchSourceString) { | ||
const searchSource = JSON.parse(searchSourceString); | ||
if (searchSource.index) { | ||
searchSource.index = idGenerator(searchSource.index); | ||
savedObject.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.stringify( | ||
searchSource | ||
); | ||
} | ||
} | ||
|
||
if (visStateString) { | ||
const visState = JSON.parse(visStateString); | ||
const controlList = visState.params?.controls; | ||
if (controlList) { | ||
controlList.map((control) => { | ||
if (control.indexPattern) { | ||
control.indexPattern = `${dataSourceId}_` + control.indexPattern; | ||
} | ||
}); | ||
if (visStateString) { | ||
const visState = JSON.parse(visStateString); | ||
const controlList = visState.params?.controls; | ||
if (controlList) { | ||
controlList.map((control) => { | ||
if (control.indexPattern) { | ||
control.indexPattern = idGenerator(control.indexPattern); | ||
} | ||
saveObject.attributes.visState = JSON.stringify(visState); | ||
} | ||
}); | ||
} | ||
savedObject.attributes.visState = JSON.stringify(visState); | ||
} | ||
} | ||
}; | ||
|
||
export const getDataSourceIntegratedSavedObjects = ( | ||
savedObjectList: SavedObject[], | ||
dataSourceId?: string, | ||
dataSourceTitle?: string | ||
): SavedObject[] => { | ||
savedObjectList = cloneDeep(savedObjectList); | ||
if (dataSourceId) { | ||
return savedObjectList.map((savedObject) => { | ||
overrideSavedObjectId(savedObject, withPrefix(dataSourceId)); | ||
|
||
// update reference | ||
if (saveObject.type === 'index-pattern') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why removing the data source reference for index pattern? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's removed by accident. Will add them back and add some unit tests. |
||
saveObject.references = [ | ||
if (savedObject.type === 'index-pattern') { | ||
savedObject.references = [ | ||
{ | ||
id: `${dataSourceId}`, | ||
type: 'data-source', | ||
|
@@ -68,17 +83,29 @@ | |
|
||
if (dataSourceTitle) { | ||
if ( | ||
saveObject.type === 'dashboard' || | ||
saveObject.type === 'visualization' || | ||
saveObject.type === 'search' | ||
savedObject.type === 'dashboard' || | ||
savedObject.type === 'visualization' || | ||
savedObject.type === 'search' | ||
) { | ||
saveObject.attributes.title = saveObject.attributes.title + `_${dataSourceTitle}`; | ||
savedObject.attributes.title = savedObject.attributes.title + `_${dataSourceTitle}`; | ||
} | ||
} | ||
|
||
return saveObject; | ||
return savedObject; | ||
}); | ||
} | ||
|
||
return saveObjectList; | ||
return savedObjectList; | ||
}; | ||
|
||
export const getWorkspaceIntegratedSavedObjects = ( | ||
savedObjectList: SavedObject[], | ||
workspaceId?: string | ||
) => { | ||
const savedObjectListCopy = cloneDeep(savedObjectList); | ||
|
||
savedObjectListCopy.forEach((savedObject) => { | ||
overrideSavedObjectId(savedObject, withPrefix(workspaceId)); | ||
}); | ||
return savedObjectListCopy; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we are adding workspaceId into the parameters of listSampleDataSets/install/uninstall but not in loadSampleDataSets, any reason for such difference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
loadSampleDataSets
function calllistSampleDataSets
below. It will read the current workspace id from the workspace service directly. So we don't add the parameter for workspace id.