From 626946cfb0c5ed7860def4d8a259d86a2c2d1300 Mon Sep 17 00:00:00 2001 From: Shenoy Pratik Date: Fri, 26 May 2023 16:36:12 -0700 Subject: [PATCH] updating trace DSL request handler (#494) Signed-off-by: Shenoy Pratik (cherry picked from commit d282d40f9d0a6475889f70c7733426cf23bbaeb6) --- .../requests/request_handler.ts | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/public/components/trace_analytics/requests/request_handler.ts b/public/components/trace_analytics/requests/request_handler.ts index 494aa9b09b..2d3191a028 100644 --- a/public/components/trace_analytics/requests/request_handler.ts +++ b/public/components/trace_analytics/requests/request_handler.ts @@ -2,7 +2,6 @@ * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ -/* eslint-disable no-console */ import { CoreStart } from '../../../../../../src/core/public'; import { @@ -10,11 +9,18 @@ import { TRACE_ANALYTICS_DATA_PREPPER_INDICES_ROUTE, TRACE_ANALYTICS_JAEGER_INDICES_ROUTE, JAEGER_INDEX_NAME, - DATA_PREPPER_INDEX_NAME + DATA_PREPPER_INDEX_NAME, } from '../../../../common/constants/trace_analytics'; import { TraceAnalyticsMode } from '../home'; -export function handleDslRequest(http: CoreStart['http'], DSL: any, bodyQuery: any, mode: TraceAnalyticsMode, timeout?: boolean, setShowTimeoutToast?: () => void) { +export async function handleDslRequest( + http: CoreStart['http'], + DSL: any, + bodyQuery: any, + mode: TraceAnalyticsMode, + timeout?: boolean, + setShowTimeoutToast?: () => void +) { if (DSL?.query) { bodyQuery.query.bool.must.push(...DSL.query.bool.must); bodyQuery.query.bool.filter.push(...DSL.query.bool.filter); @@ -25,35 +31,45 @@ export function handleDslRequest(http: CoreStart['http'], DSL: any, bodyQuery: a } let body = bodyQuery; if (!bodyQuery.index) { - body = {...bodyQuery, index: ((mode === 'jaeger' ? JAEGER_INDEX_NAME : DATA_PREPPER_INDEX_NAME)) } + body = { ...bodyQuery, index: mode === 'jaeger' ? JAEGER_INDEX_NAME : DATA_PREPPER_INDEX_NAME }; } if (timeout) { const id = setTimeout(() => setShowTimeoutToast!(), 30000); - return http - .post(TRACE_ANALYTICS_DSL_ROUTE, { + + try { + return await http.post(TRACE_ANALYTICS_DSL_ROUTE, { body: JSON.stringify(body), - }) - .catch((error) => { - console.error(error) - }) - .finally(() => clearTimeout(id)); + }); + } catch (error) { + console.error(error); + } finally { + clearTimeout(id); + } } - return http - .post(TRACE_ANALYTICS_DSL_ROUTE, { + try { + return await http.post(TRACE_ANALYTICS_DSL_ROUTE, { body: JSON.stringify(body), - }) - .catch((error) => console.error(error)); + }); + } catch (error_1) { + console.error(error_1); + } } -export async function handleJaegerIndicesExistRequest(http: CoreStart['http'], setJaegerIndicesExist) { +export async function handleJaegerIndicesExistRequest( + http: CoreStart['http'], + setJaegerIndicesExist +) { http .post(TRACE_ANALYTICS_JAEGER_INDICES_ROUTE) .then((exists) => setJaegerIndicesExist(exists)) .catch(() => setJaegerIndicesExist(false)); } -export async function handleDataPrepperIndicesExistRequest(http: CoreStart['http'], setDataPrepperIndicesExist) { +export async function handleDataPrepperIndicesExistRequest( + http: CoreStart['http'], + setDataPrepperIndicesExist +) { http .post(TRACE_ANALYTICS_DATA_PREPPER_INDICES_ROUTE) .then((exists) => setDataPrepperIndicesExist(exists))