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

[MD] Concatenate data source name with index pattern name and change delimiter to double colon #5907

Merged
merged 5 commits into from
Feb 23, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Multiple Datasource] Add interfaces to register add-on authentication method from plug-in module ([#5851](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5851))
- [Multiple Datasource] Able to Hide "Local Cluster" option from datasource DropDown ([#5827](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5827))
- [Multiple Datasource] Add api registry and allow it to be added into client config in data source plugin ([#5895](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5895))
- [Multiple Datasource] Concatenate data source name with index pattern name and change delimiter to double colon ([#5907](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5907))

### 🐛 Bug Fixes

Expand Down
54 changes: 54 additions & 0 deletions src/plugins/data/common/index_patterns/lib/get_title.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { SavedObjectsClientContract } from '../../../../../core/public';
import { getTitle } from './get_title';

describe('test getTitle', () => {
let client: SavedObjectsClientContract;

it('with dataSourceId match', async () => {
const dataSourceIdToTitle = new Map();
dataSourceIdToTitle.set('dataSourceId', 'dataSourceTitle');
client = {
get: jest.fn().mockResolvedValue({
attributes: { title: 'indexTitle' },
references: [{ type: 'data-source', id: 'dataSourceId' }],
}),
} as any;
const title = await getTitle(client, 'indexPatternId', dataSourceIdToTitle);
expect(title).toEqual('dataSourceTitle::indexTitle');
});

it('with no dataSourceId match and error to get data source', async () => {
const dataSourceIdToTitle = new Map();
client = {
get: jest
.fn()
.mockResolvedValueOnce({
attributes: { title: 'indexTitle' },
references: [{ type: 'data-source', id: 'dataSourceId' }],
})
.mockRejectedValue(new Error('error')),
} as any;
const title = await getTitle(client, 'indexPatternId', dataSourceIdToTitle);
expect(title).toEqual('dataSourceId::indexTitle');
});

it('with no dataSourceId match and success to get data source', async () => {
const dataSourceIdToTitle = new Map();
client = {
get: jest
.fn()
.mockResolvedValueOnce({
attributes: { title: 'indexTitle' },
references: [{ type: 'data-source', id: 'dataSourceId' }],
})
.mockResolvedValue({ attributes: { title: 'acquiredDataSourceTitle' } }),
} as any;
const title = await getTitle(client, 'indexPatternId', dataSourceIdToTitle);
expect(title).toEqual('acquiredDataSourceTitle::indexTitle');
});
});
28 changes: 25 additions & 3 deletions src/plugins/data/common/index_patterns/lib/get_title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,39 @@
* under the License.
*/

import { DataSourceAttributes } from 'src/plugins/data_source/common/data_sources';
import { SavedObjectsClientContract, SimpleSavedObject } from '../../../../../core/public';
import {
concatDataSourceWithIndexPattern,
getIndexPatternTitle,
getDataSourceReference,
} from '../utils';

export async function getTitle(
client: SavedObjectsClientContract,
indexPatternId: string
): Promise<SimpleSavedObject<any>> {
indexPatternId: string,
dataSourceIdToTitle: Map<string, string>
): Promise<string> {
const savedObject = (await client.get('index-pattern', indexPatternId)) as SimpleSavedObject<any>;

if (savedObject.error) {
throw new Error(`Unable to get index-pattern title: ${savedObject.error.message}`);
}

return savedObject.attributes.title;
const dataSourceReference = getDataSourceReference(savedObject.references);

Check warning on line 50 in src/plugins/data/common/index_patterns/lib/get_title.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/lib/get_title.ts#L50

Added line #L50 was not covered by tests

if (dataSourceReference) {
const dataSourceId = dataSourceReference.id;

Check warning on line 53 in src/plugins/data/common/index_patterns/lib/get_title.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/lib/get_title.ts#L53

Added line #L53 was not covered by tests
if (dataSourceIdToTitle.has(dataSourceId)) {
return concatDataSourceWithIndexPattern(

Check warning on line 55 in src/plugins/data/common/index_patterns/lib/get_title.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/lib/get_title.ts#L55

Added line #L55 was not covered by tests
dataSourceIdToTitle.get(dataSourceId)!,
savedObject.attributes.title
);
}
}

const getDataSource = async (id: string) =>
await client.get<DataSourceAttributes>('data-source', id);

Check warning on line 63 in src/plugins/data/common/index_patterns/lib/get_title.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/lib/get_title.ts#L62-L63

Added lines #L62 - L63 were not covered by tests

return getIndexPatternTitle(savedObject.attributes.title, savedObject.references, getDataSource);

Check warning on line 65 in src/plugins/data/common/index_patterns/lib/get_title.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/lib/get_title.ts#L65

Added line #L65 was not covered by tests
}
4 changes: 2 additions & 2 deletions src/plugins/data/common/index_patterns/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('test getIndexPatternTitle', () => {
referencesMock,
getDataSourceMock
);
expect(res).toEqual('dataSourceMockTitle.indexPatternMockTitle');
expect(res).toEqual('dataSourceMockTitle::indexPatternMockTitle');
});

test('getIndexPatternTitle should return index pattern title, when index-pattern is not referenced to any datasource', async () => {
Expand All @@ -87,6 +87,6 @@ describe('test getIndexPatternTitle', () => {
referencesMock,
getDataSourceMock
);
expect(res).toEqual('dataSourceId.indexPatternMockTitle');
expect(res).toEqual('dataSourceId::indexPatternMockTitle');
});
});
19 changes: 15 additions & 4 deletions src/plugins/data/common/index_patterns/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@
references: SavedObjectReference[],
getDataSource: (id: string) => Promise<SavedObject<DataSourceAttributes>>
): Promise<string> => {
const DATA_SOURCE_INDEX_PATTERN_DELIMITER = '.';
let dataSourceTitle;
const dataSourceReference = references.find((ref) => ref.type === 'data-source');
const dataSourceReference = getDataSourceReference(references);

Check warning on line 86 in src/plugins/data/common/index_patterns/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/utils.ts#L86

Added line #L86 was not covered by tests

// If an index-pattern references datasource, prepend data source name with index pattern name for display purpose
if (dataSourceReference) {
Expand All @@ -99,10 +98,22 @@
// use datasource id as title when failing to fetch datasource
dataSourceTitle = dataSourceId;
}

return dataSourceTitle.concat(DATA_SOURCE_INDEX_PATTERN_DELIMITER).concat(indexPatternTitle);
return concatDataSourceWithIndexPattern(dataSourceTitle, indexPatternTitle);

Check warning on line 101 in src/plugins/data/common/index_patterns/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/utils.ts#L101

Added line #L101 was not covered by tests
} else {
// if index pattern doesn't reference datasource, return as it is.
return indexPatternTitle;
}
};

export const concatDataSourceWithIndexPattern = (
dataSourceTitle: string,
indexPatternTitle: string
) => {
const DATA_SOURCE_INDEX_PATTERN_DELIMITER = '::';

Check warning on line 112 in src/plugins/data/common/index_patterns/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/utils.ts#L112

Added line #L112 was not covered by tests

return dataSourceTitle.concat(DATA_SOURCE_INDEX_PATTERN_DELIMITER).concat(indexPatternTitle);

Check warning on line 114 in src/plugins/data/common/index_patterns/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/utils.ts#L114

Added line #L114 was not covered by tests
};

export const getDataSourceReference = (references: SavedObjectReference[]) => {
return references.find((ref) => ref.type === 'data-source');

Check warning on line 118 in src/plugins/data/common/index_patterns/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/utils.ts#L118

Added line #L118 was not covered by tests
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ import { EuiComboBox, EuiComboBoxProps } from '@elastic/eui';

import { SavedObjectsClientContract, SimpleSavedObject } from 'src/core/public';
import { getTitle } from '../../../common/index_patterns/lib';
import {
getDataSourceReference,
concatDataSourceWithIndexPattern,
} from '../../../common/index_patterns/utils';

export type IndexPatternSelectProps = Required<
Omit<EuiComboBoxProps<any>, 'isLoading' | 'onSearchChange' | 'options' | 'selectedOptions'>,
Expand All @@ -52,6 +56,7 @@ interface IndexPatternSelectState {
options: [];
selectedIndexPattern: { value: string; label: string } | undefined;
searchValue: string | undefined;
dataSourceIdToTitle: Map<string, string>;
}

const getIndexPatterns = async (
Expand Down Expand Up @@ -80,6 +85,7 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectProp

this.state = {
isLoading: false,
dataSourceIdToTitle: new Map(),
options: [],
selectedIndexPattern: undefined,
searchValue: undefined,
Expand Down Expand Up @@ -113,7 +119,11 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectProp

let indexPatternTitle;
try {
indexPatternTitle = await getTitle(this.props.savedObjectsClient, indexPatternId);
indexPatternTitle = await getTitle(
this.props.savedObjectsClient,
indexPatternId,
this.state.dataSourceIdToTitle
);
} catch (err) {
// index pattern no longer exists
return;
Expand Down Expand Up @@ -161,16 +171,66 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectProp
// We need this check to handle the case where search results come back in a different
// order than they were sent out. Only load results for the most recent search.
if (searchValue === this.state.searchValue) {
const dataSourcesToFetch: Array<{ type: string; id: string }> = [];
savedObjects.map((indexPatternSavedObject: SimpleSavedObject<any>) => {
const dataSourceReference = getDataSourceReference(indexPatternSavedObject.references);
if (dataSourceReference && !this.state.dataSourceIdToTitle.has(dataSourceReference.id)) {
dataSourcesToFetch.push({ type: 'data-source', id: dataSourceReference.id });
}
});

const dataSourceIdToTitleToUpdate = new Map();

if (dataSourcesToFetch.length > 0) {
const resp = await savedObjectsClient.bulkGet(dataSourcesToFetch);
resp.savedObjects.map((dataSourceSavedObject: SimpleSavedObject<any>) => {
dataSourceIdToTitleToUpdate.set(
dataSourceSavedObject.id,
dataSourceSavedObject.attributes.title
);
});
}

Comment on lines +174 to +193
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this code to getTitle? We have index pattern id, saved object client to find reference, right? We don't have to pass third parameter. Otherwise consumer of getTitle always needs to pass map with data source id and title? Instead can getTitle function handle that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of code would perform bulk get index patterns(existing behavior), the added behavior is if the referred data source doesn't exist in the current state, it will fetch the data sources in bulk. Note there is only 2 requests here. The getTitle function is only used for selected index pattern which is a single index pattern, the existing behavior is it use an index pattern id to make individual call to saved object API to get the index pattern title, and if we use this function to handle all index patterns, this would multiple by number of index patterns, then to add data source title would double the requests.
If we want to refactor this, I can extract the logic here to a function to perform bulk operation and to produce the idToTitle map

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. No need to extract for now. We need to unify the pickers from all screen and that would be another effort. Thanks for the change.

const options = savedObjects.map((indexPatternSavedObject: SimpleSavedObject<any>) => {
const dataSourceReference = getDataSourceReference(indexPatternSavedObject.references);
if (dataSourceReference) {
const dataSourceTitle =
this.state.dataSourceIdToTitle.get(dataSourceReference.id) ||
dataSourceIdToTitleToUpdate.get(dataSourceReference.id) ||
dataSourceReference.id;
return {
label: `${concatDataSourceWithIndexPattern(
dataSourceTitle,
indexPatternSavedObject.attributes.title
)}`,
value: indexPatternSavedObject.id,
};
}
return {
label: indexPatternSavedObject.attributes.title,
value: indexPatternSavedObject.id,
};
});
this.setState({
isLoading: false,
options,
});

if (dataSourceIdToTitleToUpdate.size > 0) {
const mergedDataSourceIdToTitle = new Map();
this.state.dataSourceIdToTitle.forEach((k, v) => {
mergedDataSourceIdToTitle.set(k, v);
});
dataSourceIdToTitleToUpdate.forEach((k, v) => {
mergedDataSourceIdToTitle.set(k, v);
});
this.setState({
dataSourceIdToTitle: mergedDataSourceIdToTitle,
isLoading: false,
options,
});
} else {
this.setState({
isLoading: false,
options,
});
}

if (onNoIndexPatterns && searchValue === '' && options.length === 0) {
onNoIndexPatterns();
Expand Down
Loading