diff --git a/common/constants/index.ts b/common/constants/index.ts index db990084..0f78c810 100644 --- a/common/constants/index.ts +++ b/common/constants/index.ts @@ -16,7 +16,7 @@ export const TREE_ITEM_MATERIALIZED_VIEW_DEFAULT_NAME = `materialized_view`; export const TREE_ITEM_DATABASE_NAME_DEFAULT_NAME = `database`; export const TREE_ITEM_TABLE_NAME_DEFAULT_NAME = `table`; export const TREE_ITEM_LOAD_MATERIALIZED_BADGE_NAME = `Load Materialized View`; -export const TREE_ITEM_BADGE_NAME =`badge` +export const TREE_ITEM_BADGE_NAME = `badge`; export const LOAD_OPENSEARCH_INDICES_QUERY = `SHOW tables LIKE '%';`; export const SKIPPING_INDEX_QUERY = `CREATE SKIPPING INDEX ON myS3.logs_db.http_logs (status VALUE_SET) @@ -89,6 +89,8 @@ export const ACCELERATION_INDEX_NAME_INFO = `All OpenSearch acceleration indices - All user given index names must be in lowercase letters. Index name cannot begin with underscores. Spaces, commas, and characters -, :, ", *, +, /, \, |, ?, #, >, or < are not allowed. `; -export const SIDEBAR_POLL_INTERVAL_MS = 5000; - -export const FETCH_OPENSEARCH_INDICES_PATH = '/api/sql_console/sqlquery' \ No newline at end of file +export const FETCH_OPENSEARCH_INDICES_PATH = '/api/sql_console/sqlquery'; +export const POLL_INTERVAL_MS = 2000; +export const ASYNC_QUERY_ENDPOINT = '/api/spark_sql_console'; +export const ASYNC_QUERY_JOB_ENDPOINT = ASYNC_QUERY_ENDPOINT + '/job/'; +export const ASYNC_QUERY_SESSION_ID = 'async-query-session-id'; diff --git a/common/utils/async_query_helpers.ts b/common/utils/async_query_helpers.ts new file mode 100644 index 00000000..689727b3 --- /dev/null +++ b/common/utils/async_query_helpers.ts @@ -0,0 +1,62 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import _ from 'lodash'; +import { CoreStart } from '../../../../src/core/public'; +import { + ASYNC_QUERY_ENDPOINT, + ASYNC_QUERY_JOB_ENDPOINT, + ASYNC_QUERY_SESSION_ID, + POLL_INTERVAL_MS, +} from '../constants'; + +export const setAsyncSessionId = (value: string | null) => { + if (value === null) sessionStorage.removeItem(ASYNC_QUERY_SESSION_ID); + else sessionStorage.setItem(ASYNC_QUERY_SESSION_ID, value); +}; + +export const getAsyncSessionId = () => { + return sessionStorage.getItem(ASYNC_QUERY_SESSION_ID); +}; + +export const getJobId = (query: {}, http: CoreStart['http'], callback) => { + http + .post(ASYNC_QUERY_ENDPOINT, { + body: JSON.stringify({ ...query, sessionId: getAsyncSessionId() ?? undefined }), + }) + .then((res) => { + const id = res.data.resp.queryId; + setAsyncSessionId(_.get(res.data.resp, 'sessionId', null)); + callback(id); + }) + .catch((err) => { + console.error(err); + }); +}; + +export const pollQueryStatus = (id: string, http: CoreStart['http'], callback) => { + http + .get(ASYNC_QUERY_JOB_ENDPOINT + id) + .then((res) => { + const status = res.data.resp.status.toLowerCase(); + if ( + status === 'pending' || + status === 'running' || + status === 'scheduled' || + status === 'waiting' + ) { + setTimeout(() => pollQueryStatus(id, http, callback), POLL_INTERVAL_MS); + } else if (status === 'failed') { + callback([]); + } else if (status === 'success') { + const results = _.get(res.data.resp, 'datarows'); + callback(results); + } + }) + .catch((err) => { + console.error(err); + callback([]); + }); +}; diff --git a/public/ace-themes/sql_console.js b/public/ace-themes/sql_console.js deleted file mode 100644 index b0d4898c..00000000 --- a/public/ace-themes/sql_console.js +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -import * as ace from 'brace'; - -ace.define('ace/theme/sql_console', ['require', 'exports', 'module', 'ace/lib/dom'], function ( - acequire, - exports, - module -) { - exports.isDark = false; - exports.cssClass = 'ace-sql-console'; - exports.cssText = require('../index.scss'); - - const dom = acequire('../lib/dom'); - dom.importCssString(exports.cssText, exports.cssClass); -}); diff --git a/public/components/Main/__snapshots__/main.test.tsx.snap b/public/components/Main/__snapshots__/main.test.tsx.snap index 6d65fd66..f34fea4e 100644 --- a/public/components/Main/__snapshots__/main.test.tsx.snap +++ b/public/components/Main/__snapshots__/main.test.tsx.snap @@ -296,7 +296,7 @@ exports[` spec click clear button 1`] = ` />