-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add MDS support to Timeline Signed-off-by: Huy Nguyen <[email protected]> * Refactor to function and add unit tests Signed-off-by: Huy Nguyen <[email protected]> * Fix typo in args Signed-off-by: Huy Nguyen <[email protected]> * Refactor build request to pass unit tests Signed-off-by: Huy Nguyen <[email protected]> * Add to CHANGELOG Signed-off-by: Huy Nguyen <[email protected]> * Refactor error messages + address comments Signed-off-by: Huy Nguyen <[email protected]> * Fix ut Signed-off-by: Huy Nguyen <[email protected]> * Change to data source feature Signed-off-by: Huy Nguyen <[email protected]> * Fix UT Signed-off-by: Huy Nguyen <[email protected]> * Address comments Signed-off-by: Huy Nguyen <[email protected]> --------- Signed-off-by: Huy Nguyen <[email protected]> (cherry picked from commit 73e5d78) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: ZilongX <[email protected]>
- Loading branch information
1 parent
368a17b
commit 64af4b5
Showing
11 changed files
with
259 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
src/plugins/vis_type_timeline/server/lib/fetch_data_source_id.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { savedObjectsClientMock } from '../../../../core/server/mocks'; | ||
import { fetchDataSourceIdByName } from './fetch_data_source_id'; | ||
import { OpenSearchFunctionConfig } from '../types'; | ||
|
||
jest.mock('./services', () => ({ | ||
getDataSourceEnabled: jest | ||
.fn() | ||
.mockReturnValueOnce({ enabled: false }) | ||
.mockReturnValue({ enabled: true }), | ||
})); | ||
|
||
describe('fetchDataSourceIdByName()', () => { | ||
const validId = 'some-valid-id'; | ||
const config: OpenSearchFunctionConfig = { | ||
q: null, | ||
metric: null, | ||
split: null, | ||
index: null, | ||
timefield: null, | ||
kibana: null, | ||
opensearchDashboards: null, | ||
interval: null, | ||
}; | ||
const client = savedObjectsClientMock.create(); | ||
client.find = jest.fn().mockImplementation((props) => { | ||
if (props.search === '"No Results With Filter"') { | ||
return Promise.resolve({ | ||
saved_objects: [ | ||
{ | ||
id: 'some-non-matching-id', | ||
attributes: { | ||
title: 'No Results With Filter Some Suffix', | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
if (props.search === '"Duplicate Title"') { | ||
return Promise.resolve({ | ||
saved_objects: [ | ||
{ | ||
id: 'duplicate-id-1', | ||
attributes: { | ||
title: 'Duplicate Title', | ||
}, | ||
}, | ||
{ | ||
id: 'duplicate-id-2', | ||
attributes: { | ||
title: 'Duplicate Title', | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
if (props.search === '"Some Data Source"') { | ||
return Promise.resolve({ | ||
saved_objects: [ | ||
{ | ||
id: validId, | ||
attributes: { | ||
title: 'Some Data Source', | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
if (props.search === '"Some Prefix"') { | ||
return Promise.resolve({ | ||
saved_objects: [ | ||
{ | ||
id: 'some-id-2', | ||
attributes: { | ||
title: 'Some Prefix B', | ||
}, | ||
}, | ||
{ | ||
id: validId, | ||
attributes: { | ||
title: 'Some Prefix', | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
return Promise.resolve({ saved_objects: [] }); | ||
}); | ||
|
||
it('should return undefined if data_source_name is not present', async () => { | ||
expect(await fetchDataSourceIdByName(config, client)).toBe(undefined); | ||
}); | ||
|
||
it('should return undefined if data_source_name is an empty string', async () => { | ||
expect(await fetchDataSourceIdByName({ ...config, data_source_name: '' }, client)).toBe( | ||
undefined | ||
); | ||
}); | ||
|
||
it('should throw errors when MDS is disabled', async () => { | ||
await expect( | ||
fetchDataSourceIdByName({ ...config, data_source_name: 'Some Data Source' }, client) | ||
).rejects.toThrowError( | ||
'To query from multiple data sources, first enable the data source feature' | ||
); | ||
}); | ||
|
||
it.each([ | ||
{ | ||
dataSourceName: 'Non-existent Data Source', | ||
expectedResultCount: 0, | ||
}, | ||
{ | ||
dataSourceName: 'No Results With Filter', | ||
expectedResultCount: 0, | ||
}, | ||
{ | ||
dataSourceName: 'Duplicate Title', | ||
expectedResultCount: 2, | ||
}, | ||
])( | ||
'should throw errors when non-existent or duplicate data_source_name is provided', | ||
async ({ dataSourceName, expectedResultCount }) => { | ||
await expect( | ||
fetchDataSourceIdByName({ ...config, data_source_name: dataSourceName }, client) | ||
).rejects.toThrowError( | ||
`Expected exactly 1 result for data_source_name "${dataSourceName}" but got ${expectedResultCount} results` | ||
); | ||
} | ||
); | ||
|
||
it.each([ | ||
{ | ||
dataSourceName: 'Some Data Source', | ||
}, | ||
{ | ||
dataSourceName: 'Some Prefix', | ||
}, | ||
])( | ||
'should return valid id when data_source_name exists and is unique', | ||
async ({ dataSourceName }) => { | ||
expect( | ||
await fetchDataSourceIdByName({ ...config, data_source_name: dataSourceName }, client) | ||
).toBe(validId); | ||
} | ||
); | ||
}); |
42 changes: 42 additions & 0 deletions
42
src/plugins/vis_type_timeline/server/lib/fetch_data_source_id.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { SavedObjectsClientContract } from 'src/core/server'; | ||
import { DataSourceAttributes } from 'src/plugins/data_source/common/data_sources'; | ||
import { getDataSourceEnabled } from './services'; | ||
import { OpenSearchFunctionConfig } from '../types'; | ||
|
||
export const fetchDataSourceIdByName = async ( | ||
config: OpenSearchFunctionConfig, | ||
client: SavedObjectsClientContract | ||
) => { | ||
if (!config.data_source_name) { | ||
return undefined; | ||
} | ||
|
||
if (!getDataSourceEnabled().enabled) { | ||
throw new Error('To query from multiple data sources, first enable the data source feature'); | ||
} | ||
|
||
const dataSources = await client.find<DataSourceAttributes>({ | ||
type: 'data-source', | ||
perPage: 100, | ||
search: `"${config.data_source_name}"`, | ||
searchFields: ['title'], | ||
fields: ['id', 'title'], | ||
}); | ||
|
||
const possibleDataSourceIds = dataSources.saved_objects.filter( | ||
(obj) => obj.attributes.title === config.data_source_name | ||
); | ||
|
||
if (possibleDataSourceIds.length !== 1) { | ||
throw new Error( | ||
`Expected exactly 1 result for data_source_name "${config.data_source_name}" but got ${possibleDataSourceIds.length} results` | ||
); | ||
} | ||
|
||
return possibleDataSourceIds.pop()?.id; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { createGetterSetter } from '../../../opensearch_dashboards_utils/common'; | ||
|
||
export const [getDataSourceEnabled, setDataSourceEnabled] = createGetterSetter<{ | ||
enabled: boolean; | ||
}>('DataSource'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters