Skip to content

Commit

Permalink
Support for multiple datasource sessions (opensearch-project#251)
Browse files Browse the repository at this point in the history
* support for multiple sessions

Signed-off-by: Shenoy Pratik <[email protected]>

* lint fixes

Signed-off-by: Shenoy Pratik <[email protected]>

* async helperlint fix

Signed-off-by: Shenoy Pratik <[email protected]>

* update default import

Signed-off-by: Shenoy Pratik <[email protected]>

* restore snapshot

Signed-off-by: Shenoy Pratik <[email protected]>

* update snapshots

Signed-off-by: Shenoy Pratik <[email protected]>

* lint fix for prefer named exports

Signed-off-by: Shenoy Pratik <[email protected]>

* fix lint for main.test.tsx

Signed-off-by: Shenoy Pratik <[email protected]>

* remove eslintcache

Signed-off-by: Shenoy Pratik <[email protected]>

---------

Signed-off-by: Shenoy Pratik <[email protected]>
  • Loading branch information
ps48 authored Jan 22, 2024
1 parent 0709b43 commit 96d5644
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 123 deletions.
26 changes: 17 additions & 9 deletions common/utils/async_query_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@ import {
POLL_INTERVAL_MS,
} from '../constants';

export const setAsyncSessionId = (value: string | null) => {
export const setAsyncSessionId = (dataSource: string, value: string | null) => {
if (value !== null) {
sessionStorage.setItem(ASYNC_QUERY_SESSION_ID, value);
sessionStorage.setItem(`${ASYNC_QUERY_SESSION_ID}_${dataSource}`, value);
}
};

export const getAsyncSessionId = () => {
return sessionStorage.getItem(ASYNC_QUERY_SESSION_ID);
export const getAsyncSessionId = (dataSource: string) => {
return sessionStorage.getItem(`${ASYNC_QUERY_SESSION_ID}_${dataSource}`);
};

export const getJobId = (query: {}, http: CoreStart['http'], callback) => {
export const getJobId = (
currentDataSource: string,
query: {},
http: CoreStart['http'],
callback
) => {
http
.post(ASYNC_QUERY_ENDPOINT, {
body: JSON.stringify({ ...query, sessionId: getAsyncSessionId() ?? undefined }),
body: JSON.stringify({
...query,
sessionId: getAsyncSessionId(currentDataSource) ?? undefined,
}),
})
.then((res) => {
const id = res.data.resp.queryId;
setAsyncSessionId(_.get(res.data.resp, 'sessionId', null));
setAsyncSessionId(currentDataSource, _.get(res.data.resp, 'sessionId', null));
if (id === undefined) {
console.error(JSON.parse(res.data.body));
}
Expand All @@ -51,14 +59,14 @@ export const pollQueryStatus = (id: string, http: CoreStart['http'], callback) =
status === 'scheduled' ||
status === 'waiting'
) {
callback({ status: status });
callback({ status });
setTimeout(() => pollQueryStatus(id, http, callback), POLL_INTERVAL_MS);
} else if (status === 'failed') {
const results = res.data.resp;
callback({ status: 'FAILED', error: results.error });
} else if (status === 'success') {
const results = _.get(res.data.resp, 'datarows');
callback({ status: 'SUCCESS', results: results });
callback({ status: 'SUCCESS', results });
}
})
.catch((err) => {
Expand Down
3 changes: 1 addition & 2 deletions public/components/Main/main.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
mockQueryTranslationResponse,
mockResultWithNull,
} from '../../../test/mocks/mockData';
import Main from './main';
import { Main } from './main';

const setBreadcrumbsMock = jest.fn();

Expand Down Expand Up @@ -129,7 +129,6 @@ describe('<Main /> spec', () => {
});

it('click clear button', async () => {
let postRequestFlag = 0;
const client = httpClientMock;
client.get = jest.fn().mockResolvedValue(mockDatasourcesQuery);
client.post = jest.fn(() => {
Expand Down
Loading

0 comments on commit 96d5644

Please sign in to comment.