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
…) (#8825)

* Fix workspace id missing in sample data filter meta index



* Changeset file for PR #8786 created/updated

---------



(cherry picked from commit af4943d)

Signed-off-by: Lin Wang <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 8, 2024
1 parent 0d691dc commit 302a04a
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 302a04a

Please sign in to comment.