Skip to content

Commit

Permalink
[Workspace]Fix flights sample data copy in workspace assets page (#8786)
Browse files Browse the repository at this point in the history
* Fix workspace id missing in sample data filter meta index

Signed-off-by: Lin Wang <[email protected]>

* Changeset file for PR #8786 created/updated

---------

Signed-off-by: Lin Wang <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
(cherry picked from commit af4943d)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 5437239 commit a5fdf72
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 17 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8786.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [Workspace]Fix flights sample data copy in workspace assets page ([#8786](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8786))
107 changes: 90 additions & 17 deletions src/plugins/home/server/services/sample_data/data_sets/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,25 @@ describe('getSavedObjectsWithDataSource()', () => {
});

describe('getFinalSavedObjects()', () => {
const savedObjects = [
{ id: 'saved-object-1', type: 'test', attributes: { title: 'Saved object 1' }, references: [] },
];
const generateTestDataSet = () => {
const generateTestDataSet = (
savedObjects: Array<
SavedObject<{
title: string;
kibanaSavedObjectMeta?: {
searchSourceJSON: Record<string, unknown>;
};
visState?: string;
}>
> = [
{
id: 'saved-object-1',
type: 'search',
attributes: { title: 'Saved object 1' },
references: [],
},
]
) => {
const getSavedObjects = () => JSON.parse(JSON.stringify(savedObjects));
return {
id: 'foo',
name: 'Foo',
Expand All @@ -192,20 +207,11 @@ describe('getFinalSavedObjects()', () => {
appLinks: [],
defaultIndex: '',
getDataSourceIntegratedDefaultIndex: () => '',
savedObjects,
savedObjects: getSavedObjects(),
getDataSourceIntegratedSavedObjects: (dataSourceId?: string, dataSourceTitle?: string) =>
savedObjects.map((item) => ({
...item,
...(dataSourceId ? { id: `${dataSourceId}_${item.id}` } : {}),
attributes: {
...item.attributes,
title: dataSourceTitle
? `${item.attributes.title}_${dataSourceTitle}`
: item.attributes.title,
},
})),
getSavedObjectsWithDataSource(getSavedObjects(), dataSourceId, dataSourceTitle),
getWorkspaceIntegratedSavedObjects: (workspaceId?: string) =>
savedObjects.map((item) => ({
getSavedObjects().map((item) => ({
...item,
...(workspaceId ? { id: `${workspaceId}_${item.id}` } : {}),
})),
Expand Down Expand Up @@ -269,7 +275,74 @@ describe('getFinalSavedObjects()', () => {
getFinalSavedObjects({
dataset,
})
).toBe(dataset.savedObjects);
).toEqual(dataset.savedObjects);
});

it('should update index in searchSource for visualization and search saved objects', () => {
const dataset = generateTestDataSet([
{
id: 'saved-object-1',
type: 'visualization',
attributes: {
title: 'Saved object 1',
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
index: 'index-pattern',
filter: [{ meta: { index: 'index-pattern' } }],
}),
},
visState: JSON.stringify({}),
},
references: [],
},
{
id: 'saved-object-2',
type: 'search',
attributes: {
title: 'Saved object 2',
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
index: 'index-pattern',
filter: [{ meta: { index: 'index-pattern' } }],
}),
},
},
references: [],
},
]);
const UPDATED_SEARCH_JSON = JSON.stringify({
index: 'workspace-1_datasource-1_index-pattern',
filter: [{ meta: { index: 'workspace-1_datasource-1_index-pattern' } }],
});
expect(
getFinalSavedObjects({
dataset,
workspaceId: 'workspace-1',
dataSourceId: 'datasource-1',
dataSourceTitle: 'data source 1',
})
).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: 'workspace-1_datasource-1_saved-object-1',
type: 'visualization',
attributes: expect.objectContaining({
kibanaSavedObjectMeta: expect.objectContaining({
searchSourceJSON: UPDATED_SEARCH_JSON,
}),
}),
}),
expect.objectContaining({
id: 'workspace-1_datasource-1_saved-object-2',
type: 'search',
attributes: expect.objectContaining({
kibanaSavedObjectMeta: expect.objectContaining({
searchSourceJSON: UPDATED_SEARCH_JSON,
}),
}),
}),
])
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ const overrideSavedObjectId = (savedObject: SavedObject, idGenerator: (id: strin
const searchSource = JSON.parse(searchSourceString);
if (searchSource.index) {
searchSource.index = idGenerator(searchSource.index);
if (Array.isArray(searchSource.filter)) {
searchSource.filter.forEach((item) => {
if (item.meta?.index) {
item.meta.index = idGenerator(item.meta.index);
}
});
}
savedObject.attributes.kibanaSavedObjectMeta.searchSourceJSON = JSON.stringify(
searchSource
);
Expand Down

0 comments on commit a5fdf72

Please sign in to comment.