Skip to content
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

[Backport 2.x] [MDS] Fix sample data to use datasources for TSVB visualizations #6943

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/6940.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Add TSVB Support for adding sample data ([#6940](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6940))
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ describe('getSavedObjectsWithDataSource()', () => {
return visualizationObjects.saved_objects;
};

const TSVBVisualizationSavedObject = {
type: 'visualization',
id: 'some-id',
attributes: {
title: 'some-title',
visState: JSON.stringify({
type: 'metrics',
params: {},
}),
},
references: [],
};

test('when processing Vega Visualization saved objects, it should attach data_source_name to each OpenSearch query', () => {
const dataSourceId = 'some-datasource-id';
const dataSourceName = 'Data Source Name';
Expand Down Expand Up @@ -127,6 +140,35 @@ describe('getSavedObjectsWithDataSource()', () => {
},
]);
});

test('when processing TSVB Visualization saved objects, it should attach data_source_id to the visState and add datasource reference', () => {
const dataSourceId = 'some-datasource-id';
const dataSourceTitle = 'Data Source Name';
const expectedTSVBVisualizationSavedObject = {
...TSVBVisualizationSavedObject,
id: `${dataSourceId}_some-id`,
attributes: {
title: `some-title_${dataSourceTitle}`,
visState: JSON.stringify({
type: 'metrics',
params: {
data_source_id: dataSourceId,
},
}),
},
references: [
{
id: dataSourceId,
type: 'data-source',
name: 'dataSource',
},
],
};

expect(
getSavedObjectsWithDataSource([TSVBVisualizationSavedObject], dataSourceId, dataSourceTitle)
).toMatchObject([expectedTSVBVisualizationSavedObject]);
});
});

describe('getFinalSavedObjects()', () => {
Expand Down
18 changes: 16 additions & 2 deletions src/plugins/home/server/services/sample_data/data_sets/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export const getSavedObjectsWithDataSource = (
if (saveObject.type === 'visualization') {
const vegaSpec = extractVegaSpecFromSavedObject(saveObject);

const visualizationSavedObject = saveObject as SavedObject & {
attributes: { visState: string };
};
const visStateObject = JSON.parse(visualizationSavedObject.attributes.visState);

if (!!vegaSpec) {
const updatedVegaSpec = updateDataSourceNameInVegaSpec({
spec: vegaSpec,
Expand All @@ -104,8 +109,6 @@ export const getSavedObjectsWithDataSource = (
spacing: 1,
});

// @ts-expect-error
const visStateObject = JSON.parse(saveObject.attributes?.visState);
visStateObject.params.spec = updatedVegaSpec;

// @ts-expect-error
Expand All @@ -131,6 +134,17 @@ export const getSavedObjectsWithDataSource = (
// @ts-expect-error
saveObject.attributes.visState = JSON.stringify(timelineStateObject);
}

if (visStateObject.type === 'metrics') {
visStateObject.params.data_source_id = dataSourceId;

visualizationSavedObject.attributes.visState = JSON.stringify(visStateObject);
visualizationSavedObject.references.push({
id: `${dataSourceId}`,
type: 'data-source',
name: 'dataSource',
});
}
}
}

Expand Down
Loading