Skip to content

Commit

Permalink
Modify the adding sample data part for timeline
Browse files Browse the repository at this point in the history
Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>
  • Loading branch information
zhyuanqi committed Jun 5, 2024
1 parent 766a39a commit d8fc98e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ export {
SavedObjectsDeleteByWorkspaceOptions,
updateDataSourceNameInVegaSpec,
extractVegaSpecFromSavedObject,
extractTimelineExpression,
updateDataSourceNameInTimeline,
} from './saved_objects';

export {
Expand Down
7 changes: 6 additions & 1 deletion src/core/server/saved_objects/import/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ export {
SavedObjectsResolveImportErrorsOptions,
SavedObjectsImportRetry,
} from './types';
export { updateDataSourceNameInVegaSpec, extractVegaSpecFromSavedObject } from './utils';
export {
updateDataSourceNameInVegaSpec,
extractVegaSpecFromSavedObject,
extractTimelineExpression,
updateDataSourceNameInTimeline,
} from './utils';
25 changes: 25 additions & 0 deletions src/core/server/saved_objects/import/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getDataSourceTitleFromId,
getUpdatedTSVBVisState,
updateDataSourceNameInVegaSpec,
updateDataSourceNameInTimeline,
} from './utils';
import { parse } from 'hjson';
import { isEqual } from 'lodash';
Expand Down Expand Up @@ -199,6 +200,30 @@ describe('updateDataSourceNameInVegaSpec()', () => {
});
});

describe('updateDataSourceNameInTimeline()', () => {
test('When a timeline expression does not contain a data source name, modify the expression', () => {
const expression =
'.opensearch(opensearch_dashboards_sample_data_logs, metric=avg:bytes, timefield=@timestamp).lines(show=true).points(show=true).yaxis(label="Average bytes")';
const expectedExpression =
'.opensearch(opensearch_dashboards_sample_data_logs, metric=avg:bytes, timefield=@timestamp, data_source_name=newDataSource).lines(show=true).points(show=true).yaxis(label="Average bytes")';
expect(updateDataSourceNameInTimeline(expression, 'newDataSource')).toBe(expectedExpression);
});

test('When a timeline expression contains a data source name, then do nothing', () => {
const expression =
'.opensearch(opensearch_dashboards_sample_data_logs, metric=avg:bytes, timefield=@timestamp, data_source_name=newDataSource).lines(show=true).points(show=true).yaxis(label="Average bytes")';
expect(updateDataSourceNameInTimeline(expression, 'newDataSource')).toBe(expression);
});

test('When a timeline expression contains multiple timeline expression, modify each of them', () => {
const expression =
'.opensearch(opensearch_dashboards_sample_data_logs, metric=avg:bytes, timefield=@timestamp,data_source_name=aos211).lines(show=true).points(show=true).yaxis(label="Average bytes"),.opensearch(opensearch_dashboards_sample_data_logs, metric=avg:bytes, timefield=@timestamp).lines(show=true).points(show=true).yaxis(label="Average bytes")';
const expectedExpression =
'.opensearch(opensearch_dashboards_sample_data_logs, metric=avg:bytes, timefield=@timestamp,data_source_name=aos211).lines(show=true).points(show=true).yaxis(label="Average bytes"),.opensearch(opensearch_dashboards_sample_data_logs, metric=avg:bytes, timefield=@timestamp, data_source_name=aos211).lines(show=true).points(show=true).yaxis(label="Average bytes")';
expect(updateDataSourceNameInTimeline(expression, 'aos211')).toBe(expectedExpression);
});
});

describe('extractVegaSpecFromSavedObject()', () => {
test('For a Vega visualization saved object, return its spec', () => {
const spec = 'some-vega-spec';
Expand Down
35 changes: 32 additions & 3 deletions src/core/server/saved_objects/import/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ export const updateDataSourceNameInVegaSpec = (
});
};

export const updateDataSourceNameInTimeline = (
timelineExpression: string,
dataSourceTitle: string
) => {
const expressionRegex = /\.(opensearch|es|elasticsearch)\(([^)]*)\)/g;

const replaceCallback = (match: string, funcName: string, args: string) => {
if (!args.includes('data_source_name')) {
args = args.trim();
args = `${args}, data_source_name=${dataSourceTitle}`;
return `.${funcName}(${args})`;
}
return match;
};

const modifiedExpression = timelineExpression.replace(expressionRegex, replaceCallback);
return modifiedExpression;
};

export const getDataSourceTitleFromId = async (
dataSourceId: string,
savedObjectsClient: SavedObjectsClientContract
Expand All @@ -102,7 +121,7 @@ export const getDataSourceTitleFromId = async (
};

export const extractVegaSpecFromSavedObject = (savedObject: SavedObject) => {
if (isVegaVisualization(savedObject)) {
if (checkVisualization(savedObject, 'vega')) {
// @ts-expect-error
const visStateObject = JSON.parse(savedObject.attributes?.visState);
return visStateObject.params.spec;
Expand All @@ -111,12 +130,22 @@ export const extractVegaSpecFromSavedObject = (savedObject: SavedObject) => {
return undefined;
};

const isVegaVisualization = (savedObject: SavedObject) => {
export const extractTimelineExpression = (savedObject: SavedObject) => {
if (checkVisualization(savedObject, 'timelion')) {
// @ts-expect-error
const visStateObject = JSON.parse(savedObject.attributes?.visState);
return visStateObject.params.expression;

Check warning on line 137 in src/core/server/saved_objects/import/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/core/server/saved_objects/import/utils.ts#L136-L137

Added lines #L136 - L137 were not covered by tests
}

return undefined;
};

const checkVisualization = (savedObject: SavedObject, visualizationType: string) => {
// @ts-expect-error
const visState = savedObject.attributes?.visState;
if (!!visState) {
const visStateObject = JSON.parse(visState);
return !!visStateObject.type && visStateObject.type === 'vega';
return !!visStateObject.type && visStateObject.type === visualizationType;
}
return false;
};
Expand Down
17 changes: 17 additions & 0 deletions src/plugins/home/server/services/sample_data/data_sets/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import { SavedObject } from 'opensearch-dashboards/server';
import {
extractVegaSpecFromSavedObject,
extractTimelineExpression,
updateDataSourceNameInVegaSpec,
updateDataSourceNameInTimeline,
} from '../../../../../../core/server';
import { SampleDatasetSchema } from '../lib/sample_dataset_registry_types';

Expand Down Expand Up @@ -114,6 +116,21 @@ export const getSavedObjectsWithDataSource = (
name: 'dataSource',
});
}

const timelineExpression = extractTimelineExpression(saveObject);
if (!!timelineExpression) {
// Get the timeline expression with the updated data source name
const modifiedExpression = updateDataSourceNameInTimeline(

Check warning on line 123 in src/plugins/home/server/services/sample_data/data_sets/util.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/server/services/sample_data/data_sets/util.ts#L123

Added line #L123 was not covered by tests
timelineExpression,
dataSourceTitle
);

// @ts-expect-error
const timelineStateObject = JSON.parse(saveObject.attributes?.visState);
timelineStateObject.params.expression = modifiedExpression;

Check warning on line 130 in src/plugins/home/server/services/sample_data/data_sets/util.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/server/services/sample_data/data_sets/util.ts#L129-L130

Added lines #L129 - L130 were not covered by tests
// @ts-expect-error
saveObject.attributes.visState = JSON.stringify(timelineStateObject);

Check warning on line 132 in src/plugins/home/server/services/sample_data/data_sets/util.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/home/server/services/sample_data/data_sets/util.ts#L132

Added line #L132 was not covered by tests
}
}
}

Expand Down

0 comments on commit d8fc98e

Please sign in to comment.