-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add data source service #191
Merged
wanglam
merged 8 commits into
opensearch-project:main
from
wanglam:feat-add-data-source-service
May 29, 2024
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
21da8be
Add data source service
wanglam 01be6eb
Add changelog for data source service
wanglam bd0ebe8
Rename to getSingleSelectedDataSourceOption
wanglam 1249852
Update data source init logic
wanglam faff442
Move setup and start result under dataSource
wanglam 8ce2d9d
Refactor data source with getDataSourceId$
wanglam 218bcc9
Return default data source for multi and empty data selection
wanglam 9caec03
Update dataSourceSelection to optional
wanglam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -31,6 +31,7 @@ import { | |||||
setIncontextInsightRegistry, | ||||||
} from './services'; | ||||||
import { ConfigSchema } from '../common/types/config'; | ||||||
import { DataSourceService } from './services/data_source_service'; | ||||||
|
||||||
export const [getCoreStart, setCoreStart] = createGetterSetter<CoreStart>('CoreStart'); | ||||||
|
||||||
|
@@ -58,9 +59,11 @@ export class AssistantPlugin | |||||
> { | ||||||
private config: ConfigSchema; | ||||||
incontextInsightRegistry: IncontextInsightRegistry | undefined; | ||||||
private dataSourceService: DataSourceService; | ||||||
|
||||||
constructor(initializerContext: PluginInitializerContext) { | ||||||
this.config = initializerContext.config.get<ConfigSchema>(); | ||||||
this.dataSourceService = new DataSourceService(); | ||||||
} | ||||||
|
||||||
public setup( | ||||||
|
@@ -95,6 +98,11 @@ export class AssistantPlugin | |||||
const checkAccess = (account: Awaited<ReturnType<typeof getAccount>>) => | ||||||
account.data.roles.some((role) => ['all_access', 'assistant_user'].includes(role)); | ||||||
|
||||||
const dataSourceSetupResult = this.dataSourceService.setup({ | ||||||
uiSettings: core.uiSettings, | ||||||
dataSourceManagement: setupDeps.dataSourceManagement, | ||||||
}); | ||||||
|
||||||
if (this.config.chat.enabled) { | ||||||
const setupChat = async () => { | ||||||
const [coreStart, startDeps] = await core.getStartServices(); | ||||||
|
@@ -105,6 +113,7 @@ export class AssistantPlugin | |||||
startDeps, | ||||||
conversationLoad: new ConversationLoadService(coreStart.http), | ||||||
conversations: new ConversationsService(coreStart.http), | ||||||
dataSource: this.dataSourceService, | ||||||
}); | ||||||
const account = await getAccount(); | ||||||
const username = account.data.user_name; | ||||||
|
@@ -131,6 +140,7 @@ export class AssistantPlugin | |||||
} | ||||||
|
||||||
return { | ||||||
...dataSourceSetupResult, | ||||||
registerMessageRenderer: (contentType, render) => { | ||||||
if (contentType in messageRenderers) | ||||||
console.warn(`Content renderer type ${contentType} is already registered.`); | ||||||
|
@@ -160,8 +170,12 @@ export class AssistantPlugin | |||||
setChrome(core.chrome); | ||||||
setNotifications(core.notifications); | ||||||
|
||||||
return {}; | ||||||
return { | ||||||
...this.dataSourceService.start(), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Same here, I'd vote for a single clear entry. |
||||||
}; | ||||||
} | ||||||
|
||||||
public stop() {} | ||||||
public stop() { | ||||||
this.dataSourceService.stop(); | ||||||
} | ||||||
} |
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,213 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { BehaviorSubject, Subject } from 'rxjs'; | ||
import { uiSettingsServiceMock } from '../../../../../src/core/public/mocks'; | ||
import { DataSourceOption } from '../../../../../src/plugins/data_source_management/public/components/data_source_menu/types'; | ||
import { DataSourceManagementPluginSetup } from '../../types'; | ||
import { DataSourceService } from '../data_source_service'; | ||
|
||
const setup = (options?: { dataSourceManagement?: DataSourceManagementPluginSetup }) => { | ||
const dataSourceSelection$ = new BehaviorSubject<Map<string, DataSourceOption[]>>(new Map()); | ||
const uiSettings = uiSettingsServiceMock.createSetupContract(); | ||
const dataSourceManagement: DataSourceManagementPluginSetup = { | ||
dataSourceSelection: { | ||
getSelection$: () => dataSourceSelection$, | ||
}, | ||
}; | ||
const dataSource = new DataSourceService(); | ||
const defaultDataSourceSelection$ = new Subject(); | ||
uiSettings.get$.mockReturnValueOnce(defaultDataSourceSelection$); | ||
const setupResult = dataSource.setup({ | ||
uiSettings, | ||
dataSourceManagement: | ||
options && 'dataSourceManagement' in options | ||
? options.dataSourceManagement | ||
: dataSourceManagement, | ||
}); | ||
|
||
return { | ||
dataSource, | ||
uiSettings, | ||
dataSourceSelection$, | ||
defaultDataSourceSelection$, | ||
setupResult, | ||
}; | ||
}; | ||
|
||
describe('DataSourceService', () => { | ||
it('should return data source selection provided data source id', () => { | ||
const { dataSource, dataSourceSelection$ } = setup(); | ||
|
||
expect(dataSource.getDataSourceId()).toBe(null); | ||
|
||
dataSourceSelection$.next(new Map([['test', [{ label: 'Foo', id: 'foo' }]]])); | ||
|
||
expect(dataSource.getDataSourceId()).toBe('foo'); | ||
}); | ||
describe('initDefaultDataSourceIdIfNeed', () => { | ||
it('should return ui settings provided data source id', () => { | ||
const { dataSource, uiSettings } = setup(); | ||
|
||
uiSettings.get.mockReturnValueOnce('foo'); | ||
|
||
expect(dataSource.getDataSourceId()).toBe(null); | ||
|
||
dataSource.initDefaultDataSourceIdIfNeed(); | ||
|
||
expect(dataSource.getDataSourceId()).toBe('foo'); | ||
}); | ||
it('should return data source selection provided data source', () => { | ||
const { dataSource, dataSourceSelection$, uiSettings } = setup(); | ||
|
||
uiSettings.get.mockReturnValueOnce('bar'); | ||
|
||
expect(dataSource.getDataSourceId()).toBe(null); | ||
|
||
dataSourceSelection$.next(new Map([['test', [{ label: 'Foo', id: 'foo' }]]])); | ||
|
||
expect(dataSource.getDataSourceId()).toBe('foo'); | ||
|
||
dataSource.initDefaultDataSourceIdIfNeed(); | ||
expect(dataSource.getDataSourceId()).toBe('foo'); | ||
}); | ||
}); | ||
it('should update data source id after default data source id changed', () => { | ||
const { dataSource, defaultDataSourceSelection$, uiSettings } = setup(); | ||
|
||
uiSettings.get.mockReturnValueOnce('foo'); | ||
dataSource.initDefaultDataSourceIdIfNeed(); | ||
expect(dataSource.getDataSourceId()).toBe('foo'); | ||
defaultDataSourceSelection$.next('bar'); | ||
expect(dataSource.getDataSourceId()).toBe('bar'); | ||
}); | ||
it('should not update data source id when data source id not from ui settings', () => { | ||
const { dataSource, dataSourceSelection$, defaultDataSourceSelection$ } = setup(); | ||
|
||
expect(dataSource.getDataSourceId()).toBe(null); | ||
|
||
dataSourceSelection$.next(new Map([['test', [{ label: 'Foo', id: 'foo' }]]])); | ||
defaultDataSourceSelection$.next('bar'); | ||
expect(dataSource.getDataSourceId()).toBe('foo'); | ||
}); | ||
it('should return null for multi data source selection', () => { | ||
const { dataSource, dataSourceSelection$ } = setup(); | ||
|
||
expect(dataSource.getDataSourceId()).toBe(null); | ||
|
||
dataSourceSelection$.next( | ||
new Map([ | ||
[ | ||
'test', | ||
[ | ||
{ label: 'Foo', id: 'foo' }, | ||
{ label: 'Bar', id: 'bar' }, | ||
], | ||
], | ||
]) | ||
); | ||
expect(dataSource.getDataSourceId()).toBe(null); | ||
|
||
dataSourceSelection$.next( | ||
new Map([ | ||
['component1', [{ label: 'Foo', id: 'foo' }]], | ||
['component2', [{ label: 'Bar', id: 'bar' }]], | ||
]) | ||
); | ||
expect(dataSource.getDataSourceId()).toBe(null); | ||
}); | ||
it('should return null for empty data source selection', () => { | ||
const { dataSource, dataSourceSelection$ } = setup(); | ||
|
||
expect(dataSource.getDataSourceId()).toBe(null); | ||
|
||
dataSourceSelection$.next(new Map()); | ||
expect(dataSource.getDataSourceId()).toBe(null); | ||
}); | ||
it('should able to subscribe data source id changes', () => { | ||
const { dataSource } = setup(); | ||
const mockFn = jest.fn(); | ||
dataSource.subscribeDataSourceId({ next: mockFn }); | ||
|
||
dataSource.setDataSourceId('foo', undefined); | ||
expect(mockFn).toHaveBeenCalledWith('foo'); | ||
expect(mockFn).toHaveBeenCalledTimes(2); | ||
|
||
dataSource.setDataSourceId('foo', undefined); | ||
expect(mockFn).toHaveBeenCalledTimes(2); | ||
|
||
dataSource.setDataSourceId('bar', undefined); | ||
expect(mockFn).toHaveBeenCalledWith('bar'); | ||
expect(mockFn).toHaveBeenCalledTimes(3); | ||
}); | ||
describe('isMDSEnabled', () => { | ||
it('should return true if multi data source provided', () => { | ||
const { dataSource } = setup(); | ||
expect(dataSource.isMDSEnabled()).toBe(true); | ||
}); | ||
it('should return false if multi data source not provided', () => { | ||
const { dataSource } = setup({ dataSourceManagement: undefined }); | ||
expect(dataSource.isMDSEnabled()).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('getDataSourceQuery', () => { | ||
it('should return empty object if MDS not enabled', () => { | ||
const { dataSource } = setup({ dataSourceManagement: undefined }); | ||
expect(dataSource.getDataSourceQuery()).toEqual({}); | ||
}); | ||
it('should return empty object if data source id is empty', () => { | ||
const { dataSource, dataSourceSelection$ } = setup(); | ||
dataSourceSelection$.next(new Map([['test', [{ label: '', id: '' }]]])); | ||
expect(dataSource.getDataSourceQuery()).toEqual({}); | ||
}); | ||
it('should return query object with provided data source id', () => { | ||
const { dataSource, dataSourceSelection$ } = setup(); | ||
dataSourceSelection$.next(new Map([['test', [{ label: 'Foo', id: 'foo' }]]])); | ||
expect(dataSource.getDataSourceQuery()).toEqual({ dataSourceId: 'foo' }); | ||
}); | ||
it('should throw error if data source id not exists', () => { | ||
const { dataSource } = setup(); | ||
let error; | ||
try { | ||
dataSource.getDataSourceQuery(); | ||
} catch (e) { | ||
error = e; | ||
} | ||
expect(error).toBeTruthy(); | ||
}); | ||
}); | ||
it('should clear data source id', () => { | ||
const { dataSource, dataSourceSelection$ } = setup(); | ||
dataSourceSelection$.next(new Map([['test', [{ label: 'Foo', id: 'foo' }]]])); | ||
expect(dataSource.getDataSourceId()).toEqual('foo'); | ||
dataSource.clearDataSourceId(); | ||
expect(dataSource.getDataSourceId()).toEqual(null); | ||
}); | ||
it('should able to change data source id from outside', () => { | ||
const { dataSource, setupResult } = setup(); | ||
setupResult.setDataSourceId('foo'); | ||
expect(dataSource.getDataSourceId()).toBe('foo'); | ||
dataSource.start().setDataSourceId('bar'); | ||
expect(dataSource.getDataSourceId()).toBe('bar'); | ||
}); | ||
describe('stop', () => { | ||
it('should unsubscribe data source selection', () => { | ||
const { dataSource, dataSourceSelection$ } = setup(); | ||
dataSource.stop(); | ||
dataSourceSelection$.next(new Map([['test', [{ label: 'Foo', id: 'foo' }]]])); | ||
expect(dataSource.getDataSourceId()).toBe(null); | ||
}); | ||
it('should complete data source id', () => { | ||
const { dataSource } = setup(); | ||
const mockFn = jest.fn(); | ||
dataSource.subscribeDataSourceId({ | ||
complete: mockFn, | ||
}); | ||
dataSource.stop(); | ||
expect(mockFn).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any place that will use
dataSourceSetupResult
? And if so, can we make it a single entry to keep the export interface clean?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about just export a
setDataSourceId
method? For now, this method not been used in any place.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that.