From 54febd4b0df5abdc2546b5c4350d8af4b262d021 Mon Sep 17 00:00:00 2001 From: Huy Nguyen <73027756+huyaboo@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:09:53 +0000 Subject: [PATCH] Refactor build request to pass unit tests Signed-off-by: Huy Nguyen <73027756+huyaboo@users.noreply.github.com> --- .../server/series_functions/opensearch/index.js | 11 ++++++++++- .../series_functions/opensearch/lib/build_request.js | 5 +---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/plugins/vis_type_timeline/server/series_functions/opensearch/index.js b/src/plugins/vis_type_timeline/server/series_functions/opensearch/index.js index 290459cdf85a..5192059f3e6d 100644 --- a/src/plugins/vis_type_timeline/server/series_functions/opensearch/index.js +++ b/src/plugins/vis_type_timeline/server/series_functions/opensearch/index.js @@ -34,6 +34,7 @@ import { OPENSEARCH_SEARCH_STRATEGY } from '../../../../data/server'; import Datasource from '../../lib/classes/datasource'; import buildRequest from './lib/build_request'; import toSeriesList from './lib/agg_response_to_series_list'; +import { fetchDataSourceIdByName } from '../../lib/fetch_data_source_id'; export default new Datasource('es', { args: [ @@ -156,7 +157,15 @@ export default new Datasource('es', { const opensearchShardTimeout = tlConfig.opensearchShardTimeout; - const body = await buildRequest(config, tlConfig, scriptedFields, opensearchShardTimeout); + const dataSourceId = await fetchDataSourceIdByName(config, tlConfig.savedObjectsClient); + + const body = buildRequest( + config, + tlConfig, + scriptedFields, + opensearchShardTimeout, + dataSourceId + ); const deps = (await tlConfig.getStartServices())[1]; diff --git a/src/plugins/vis_type_timeline/server/series_functions/opensearch/lib/build_request.js b/src/plugins/vis_type_timeline/server/series_functions/opensearch/lib/build_request.js index ce0acb580407..90fb7b819a08 100644 --- a/src/plugins/vis_type_timeline/server/series_functions/opensearch/lib/build_request.js +++ b/src/plugins/vis_type_timeline/server/series_functions/opensearch/lib/build_request.js @@ -33,9 +33,8 @@ import moment from 'moment'; import { buildAggBody } from './agg_body'; import createDateAgg from './create_date_agg'; import { UI_SETTINGS } from '../../../../../data/server'; -import { fetchDataSourceIdByName } from '../../../lib/fetch_data_source_id'; -export default async function buildRequest(config, tlConfig, scriptedFields, timeout) { +export default function buildRequest(config, tlConfig, scriptedFields, timeout, dataSourceId) { const bool = { must: [] }; const timeFilter = { @@ -49,8 +48,6 @@ export default async function buildRequest(config, tlConfig, scriptedFields, tim }; bool.must.push(timeFilter); - const dataSourceId = await fetchDataSourceIdByName(config, tlConfig.savedObjectsClient); - // Use the opensearchDashboards and kibana filter bar filters if (config.opensearchDashboards && config.kibana) { bool.filter = _.get(tlConfig, 'request.body.extended.es.filter');