Skip to content

Commit d0541ff

Browse files
authoredJun 28, 2024
Merge pull request opensearch-project#23 from kavilla/asynccleanup
[Async] Some minor clean ups plus lint
2 parents 9daa636 + 045af8e commit d0541ff

File tree

7 files changed

+43
-74
lines changed

7 files changed

+43
-74
lines changed
 

‎common/utils.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class DataFramePolling<T, P = void> {
4848
private interval: number = 5000,
4949
private onPollingSuccess?: (data: T) => boolean,
5050
private onPollingError?: (error: Error) => boolean
51-
) { }
51+
) {}
5252

5353
fetchData(params?: P) {
5454
this.loading = true;
@@ -126,17 +126,14 @@ export const fetchDataFrame = (
126126
);
127127
};
128128

129-
export const fetchDataFramePolling = (
130-
context: FetchDataFrameContext,
131-
df: IDataFrame
132-
) => {
129+
export const fetchDataFramePolling = (context: FetchDataFrameContext, df: IDataFrame) => {
133130
const { http, path, signal } = context;
134131
const queryId = df.meta?.queryId;
135132
return from(
136133
http.fetch({
137134
method: 'GET',
138135
path: `${path}/${queryId}`,
139-
signal
136+
signal,
140137
})
141138
);
142-
}
139+
};

‎public/plugin.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import { CoreSetup, CoreStart, Plugin, PluginInitializerContext } from '../../..
88
import { IStorageWrapper, Storage } from '../../../src/plugins/opensearch_dashboards_utils/public';
99
import { ConfigSchema } from '../common/config';
1010
import { createQueryAssistExtension } from './query_assist';
11-
import { PPLSearchInterceptor, SQLSearchInterceptor } from './search';
11+
import { PPLSearchInterceptor, SQLSearchInterceptor, SQLAsyncSearchInterceptor } from './search';
1212
import { setData, setStorage } from './services';
1313
import {
1414
QueryEnhancementsPluginSetup,
1515
QueryEnhancementsPluginSetupDependencies,
1616
QueryEnhancementsPluginStart,
1717
QueryEnhancementsPluginStartDependencies,
1818
} from './types';
19-
import { SQLAsyncQlSearchInterceptor } from './search/sql_async_search_interceptor';
2019

2120
export type PublicConfig = Pick<ConfigSchema, 'queryAssist'>;
2221

@@ -50,15 +49,14 @@ export class QueryEnhancementsPlugin
5049
usageCollector: data.search.usageCollector,
5150
});
5251

53-
const sqlAsyncSearchInterceptor = new SQLAsyncQlSearchInterceptor({
52+
const sqlAsyncSearchInterceptor = new SQLAsyncSearchInterceptor({
5453
toasts: core.notifications.toasts,
5554
http: core.http,
5655
uiSettings: core.uiSettings,
5756
startServices: core.getStartServices(),
5857
usageCollector: data.search.usageCollector,
5958
});
6059

61-
6260
data.__enhance({
6361
ui: {
6462
query: {

‎public/search/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
export { PPLSearchInterceptor } from './ppl_search_interceptor';
77
export { SQLSearchInterceptor } from './sql_search_interceptor';
8+
export { SQLAsyncSearchInterceptor } from './sql_async_search_interceptor';

‎public/search/sql_async_search_interceptor.ts

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { trimEnd } from 'lodash';
2-
import { BehaviorSubject, Observable, from, throwError } from 'rxjs';
2+
import { BehaviorSubject, Observable, throwError } from 'rxjs';
33
import { i18n } from '@osd/i18n';
4+
import { concatMap } from 'rxjs/operators';
45
import {
56
DataPublicPluginStart,
67
IOpenSearchDashboardsSearchRequest,
@@ -9,17 +10,26 @@ import {
910
SearchInterceptor,
1011
SearchInterceptorDeps,
1112
} from '../../../../src/plugins/data/public';
12-
import { getRawDataFrame, getRawQueryString, IDataFrameResponse } from '../../../../src/plugins/data/common';
13-
import { API, DataFramePolling, FetchDataFrameContext, SEARCH_STRATEGY, fetchDataFrame, fetchDataFramePolling } from '../../common';
13+
import {
14+
getRawDataFrame,
15+
getRawQueryString,
16+
IDataFrameResponse,
17+
} from '../../../../src/plugins/data/common';
18+
import {
19+
API,
20+
DataFramePolling,
21+
FetchDataFrameContext,
22+
SEARCH_STRATEGY,
23+
fetchDataFrame,
24+
fetchDataFramePolling,
25+
} from '../../common';
1426
import { QueryEnhancementsPluginStartDependencies } from '../types';
15-
import { concatMap } from 'rxjs/operators';
1627

17-
export class SQLAsyncQlSearchInterceptor extends SearchInterceptor {
28+
export class SQLAsyncSearchInterceptor extends SearchInterceptor {
1829
protected queryService!: DataPublicPluginStart['query'];
1930
protected aggsService!: DataPublicPluginStart['search']['aggs'];
2031
protected dataFrame$ = new BehaviorSubject<IDataFrameResponse | undefined>(undefined);
2132

22-
2333
constructor(deps: SearchInterceptorDeps) {
2434
super(deps);
2535

@@ -47,8 +57,8 @@ export class SQLAsyncQlSearchInterceptor extends SearchInterceptor {
4757
return throwError(this.handleSearchError('DataFrame is not defined', request, signal!));
4858
}
4959

50-
const queryString = dataFrame.meta?.queryConfig?.formattedQs() ?? getRawQueryString(searchRequest) ?? '';
51-
60+
const queryString =
61+
dataFrame.meta?.queryConfig?.formattedQs() ?? getRawQueryString(searchRequest) ?? '';
5262

5363
const onPollingSuccess = (pollingResult: any) => {
5464
if (pollingResult && pollingResult.body.meta.status === 'SUCCESS') {
@@ -65,12 +75,11 @@ export class SQLAsyncQlSearchInterceptor extends SearchInterceptor {
6575
return true;
6676
}
6777
return false;
68-
}
78+
};
6979

7080
const onPollingError = (error: Error) => {
71-
console.error('Polling error:', error);
7281
throw new Error();
73-
}
82+
};
7483

7584
return fetchDataFrame(dfContext, queryString, dataFrame).pipe(
7685
concatMap((jobResponse) => {

‎server/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export function plugin(initializerContext: PluginInitializerContext) {
2020

2121
export {
2222
Facet,
23+
JobsFacet,
2324
OpenSearchPPLPlugin,
2425
OpenSearchObservabilityPlugin,
2526
shimStats,

‎server/routes/index.ts

-38
Original file line numberDiff line numberDiff line change
@@ -104,42 +104,4 @@ export function defineRoutes(
104104
defineRoute(logger, router, searchStrategies, SEARCH_STRATEGY.SQL);
105105
defineRoute(logger, router, searchStrategies, SEARCH_STRATEGY.SQLAsync);
106106
registerQueryAssistRoutes(router);
107-
// defineRoute(logger, router, searchStrategies, SEARCH_STRATEGY.SQLAsync);
108-
// sql async jobs
109-
router.post(
110-
{
111-
path: `/api/sqlasyncql/jobs`,
112-
validate: {
113-
body: schema.object({
114-
query: schema.object({
115-
qs: schema.string(),
116-
format: schema.string(),
117-
}),
118-
df: schema.any(),
119-
dataSource: schema.nullable(schema.string()),
120-
}),
121-
},
122-
},
123-
async (context, req, res): Promise<any> => {
124-
try {
125-
const queryRes: IDataFrameResponse = await searchStrategies.sqlasync.search(
126-
context,
127-
req as any,
128-
{}
129-
);
130-
const result: any = {
131-
body: {
132-
...queryRes,
133-
},
134-
};
135-
return res.ok(result);
136-
} catch (err) {
137-
logger.error(err);
138-
return res.custom({
139-
statusCode: 500,
140-
body: err,
141-
});
142-
}
143-
}
144-
);
145107
}

‎server/search/sql_async_search_strategy.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import { SharedGlobalConfig, Logger, ILegacyClusterClient } from 'opensearch-das
77
import { Observable } from 'rxjs';
88
import { ISearchStrategy, SearchUsage } from '../../../../src/plugins/data/server';
99
import {
10+
DATA_FRAME_TYPES,
11+
IDataFrameError,
1012
IDataFrameResponse,
1113
IOpenSearchDashboardsSearchRequest,
1214
PartialDataFrame,
1315
createDataFrame,
1416
} from '../../../../src/plugins/data/common';
15-
import { Facet } from '../utils';
16-
import { JobsFacet } from '../utils';
17+
import { Facet, JobsFacet } from '../utils';
1718

1819
export const sqlAsyncSearchStrategyProvider = (
1920
config$: Observable<SharedGlobalConfig>,
@@ -35,15 +36,15 @@ export const sqlAsyncSearchStrategyProvider = (
3536
datasource: df?.meta?.queryConfig?.dataSource,
3637
lang: 'sql',
3738
sessionId: df?.meta?.sessionId,
38-
}
39+
};
3940
const rawResponse = await sqlAsyncFacet.describeQuery(context, request);
4041
// handles failure
4142
if (!rawResponse.success) {
4243
return {
43-
type: 'data_frame_polling',
44+
type: DATA_FRAME_TYPES.POLLING,
4445
body: { error: rawResponse.data },
4546
took: rawResponse.took,
46-
};
47+
} as IDataFrameError;
4748
}
4849
const queryId = rawResponse.data?.queryId;
4950
const sessionId = rawResponse.data?.sessionId;
@@ -60,13 +61,13 @@ export const sqlAsyncSearchStrategyProvider = (
6061
};
6162
dataFrame.name = request.body?.datasource;
6263
return {
63-
type: 'data_frame_polling',
64+
type: DATA_FRAME_TYPES.POLLING,
6465
body: dataFrame,
6566
took: rawResponse.took,
6667
};
6768
} else {
6869
const queryId = request.params.queryId;
69-
request.params = { queryId }
70+
request.params = { queryId };
7071
const asyncResponse = await sqlAsyncJobsFacet.describeQuery(request);
7172
const status = asyncResponse.data.status;
7273
const partial: PartialDataFrame = {
@@ -77,21 +78,21 @@ export const sqlAsyncSearchStrategyProvider = (
7778
dataFrame.fields.forEach((field, index) => {
7879
field.values = asyncResponse?.data.datarows.map((row: any) => row[index]);
7980
});
80-
81+
8182
dataFrame.size = asyncResponse?.data?.datarows?.length || 0;
82-
83+
8384
dataFrame.meta = {
8485
status,
8586
queryId,
86-
error: status === 'FAILED' && asyncResponse.data?.error
87+
error: status === 'FAILED' && asyncResponse.data?.error,
8788
};
8889
dataFrame.name = request.body?.datasource;
89-
90+
9091
// TODO: MQL should this be the time for polling or the time for job creation?
9192
if (usage) usage.trackSuccess(asyncResponse.took);
92-
93+
9394
return {
94-
type: 'data_frame_polling',
95+
type: DATA_FRAME_TYPES.POLLING,
9596
body: dataFrame,
9697
took: asyncResponse.took,
9798
};
@@ -103,4 +104,4 @@ export const sqlAsyncSearchStrategyProvider = (
103104
}
104105
},
105106
};
106-
};
107+
};

0 commit comments

Comments
 (0)
Please sign in to comment.