From e66005187df022c1d4770dfb6af13083c757f887 Mon Sep 17 00:00:00 2001 From: Ramakrishna Chilaka Date: Mon, 25 Mar 2024 10:52:12 +0530 Subject: [PATCH] refactoring types & fixing tests Signed-off-by: Ramakrishna Chilaka --- .../Aliases/containers/Aliases/Aliases.tsx | 3 ++- .../ComposableTemplates.tsx | 4 ++-- .../CreateComposableTemplate.tsx | 2 +- .../CreateDataStream/CreateDataStream.tsx | 2 +- .../containers/CreateIndex/CreateIndex.tsx | 8 ++++---- .../CreateIndex/containers/IndexForm/index.tsx | 4 +++- .../CreateIndexTemplate.tsx | 9 +++++++-- .../containers/DataStreams/DataStreams.tsx | 4 ++-- .../Indices/containers/Indices/Indices.tsx | 18 ++++++++++++------ public/pages/Indices/models/interfaces.ts | 2 +- public/pages/Main/Main.tsx | 10 +++++----- .../containers/Templates/Templates.tsx | 6 +++--- public/services/CommonService.test.ts | 1 + public/services/CommonService.ts | 2 +- public/services/IndexService.ts | 4 ++-- public/services/MDSEnabledClientService.ts | 12 ++++++++---- server/services/MDSEnabledClientService.ts | 2 +- 17 files changed, 56 insertions(+), 37 deletions(-) diff --git a/public/pages/Aliases/containers/Aliases/Aliases.tsx b/public/pages/Aliases/containers/Aliases/Aliases.tsx index b63f51a62..7e447fa49 100644 --- a/public/pages/Aliases/containers/Aliases/Aliases.tsx +++ b/public/pages/Aliases/containers/Aliases/Aliases.tsx @@ -115,6 +115,7 @@ const defaultFilter = { class Aliases extends MDSEnabledComponent { static contextType = CoreServicesContext; + constructor(props: AliasesProps) { super(props); const { @@ -482,7 +483,7 @@ class Aliases extends MDSEnabledComponent { } } -export default function AliasContainer(props: Omit) { +export default function AliasContainer(props: Omit) { const context = useContext(ServicesContext); const dataSourceMenuProps = useContext(DataSourceMenuContext); return ; diff --git a/public/pages/ComposableTemplates/containers/ComposableTemplates/ComposableTemplates.tsx b/public/pages/ComposableTemplates/containers/ComposableTemplates/ComposableTemplates.tsx index 066f63ac5..368f85d87 100644 --- a/public/pages/ComposableTemplates/containers/ComposableTemplates/ComposableTemplates.tsx +++ b/public/pages/ComposableTemplates/containers/ComposableTemplates/ComposableTemplates.tsx @@ -4,7 +4,7 @@ */ import React, { useContext } from "react"; -import { debounce, isEqual, get } from "lodash"; +import _, { debounce, isEqual, get } from "lodash"; import { Link, RouteComponentProps } from "react-router-dom"; import queryString from "query-string"; import { @@ -461,7 +461,7 @@ class ComposableTemplates extends MDSEnabledComponent + props: Omit ) { const context = useContext(ServicesContext); const dataSourceMenuProps = useContext(DataSourceMenuContext); diff --git a/public/pages/CreateComposableTemplate/containers/CreateComposableTemplate/CreateComposableTemplate.tsx b/public/pages/CreateComposableTemplate/containers/CreateComposableTemplate/CreateComposableTemplate.tsx index 2d31b6c0d..462dfec12 100644 --- a/public/pages/CreateComposableTemplate/containers/CreateComposableTemplate/CreateComposableTemplate.tsx +++ b/public/pages/CreateComposableTemplate/containers/CreateComposableTemplate/CreateComposableTemplate.tsx @@ -75,7 +75,7 @@ class CreateComposableTemplate extends Component } } -export default function (props: CreateComposableTemplateProps) { +export default function (props: Omit) { const dataSourceMenuProps = useContext(DataSourceMenuContext); useUpdateUrlWithDataSourceProperties(); return ; diff --git a/public/pages/CreateDataStream/containers/CreateDataStream/CreateDataStream.tsx b/public/pages/CreateDataStream/containers/CreateDataStream/CreateDataStream.tsx index fda04b2ce..9a075d855 100644 --- a/public/pages/CreateDataStream/containers/CreateDataStream/CreateDataStream.tsx +++ b/public/pages/CreateDataStream/containers/CreateDataStream/CreateDataStream.tsx @@ -64,7 +64,7 @@ class CreateDataStream extends Component { } } -export default function (props: CreateDataStreamProps) { +export default function (props: Omit) { const dataSourceMenuProps = useContext(DataSourceMenuContext); useUpdateUrlWithDataSourceProperties(); return ; diff --git a/public/pages/CreateIndex/containers/CreateIndex/CreateIndex.tsx b/public/pages/CreateIndex/containers/CreateIndex/CreateIndex.tsx index f39eca7a3..4f64e7f39 100644 --- a/public/pages/CreateIndex/containers/CreateIndex/CreateIndex.tsx +++ b/public/pages/CreateIndex/containers/CreateIndex/CreateIndex.tsx @@ -9,15 +9,15 @@ import { RouteComponentProps } from "react-router-dom"; import IndexForm from "../IndexForm"; import { BREADCRUMBS, IndicesUpdateMode, ROUTES } from "../../../../utils/constants"; import { CoreServicesContext } from "../../../../components/core_services"; -import { CommonService } from "../../../../services/index"; import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext"; import { useUpdateUrlWithDataSourceProperties } from "../../../../components/MDSEnabledComponent"; -interface CreateIndexProps extends RouteComponentProps<{ index?: string; mode?: IndicesUpdateMode }>, DataSourceMenuProperties { +interface CreateIndexPropsBase extends RouteComponentProps<{ index?: string; mode?: IndicesUpdateMode }> { isEdit?: boolean; - commonService: CommonService; } +interface CreateIndexProps extends CreateIndexPropsBase, DataSourceMenuProperties {} + export class CreateIndex extends Component { static contextType = CoreServicesContext; @@ -63,7 +63,7 @@ export class CreateIndex extends Component { } } -export default function (props: CreateIndexProps) { +export default function (props: CreateIndexPropsBase) { const dataSourceMenuProperties = useContext(DataSourceMenuContext); useUpdateUrlWithDataSourceProperties(); return ; diff --git a/public/pages/CreateIndex/containers/IndexForm/index.tsx b/public/pages/CreateIndex/containers/IndexForm/index.tsx index 46300c96b..e4bdd0ede 100644 --- a/public/pages/CreateIndex/containers/IndexForm/index.tsx +++ b/public/pages/CreateIndex/containers/IndexForm/index.tsx @@ -46,7 +46,9 @@ export const getAliasActionsByDiffArray = ( }, [] as IAliasAction[]); }; -export interface IndexFormProps extends Pick, DataSourceMenuProperties { +export interface IndexFormProps + extends Pick, + Pick { index?: string; value?: Partial; mode?: IndicesUpdateMode; diff --git a/public/pages/CreateIndexTemplate/containers/CreateIndexTemplate/CreateIndexTemplate.tsx b/public/pages/CreateIndexTemplate/containers/CreateIndexTemplate/CreateIndexTemplate.tsx index 44c693074..6ad7638a5 100644 --- a/public/pages/CreateIndexTemplate/containers/CreateIndexTemplate/CreateIndexTemplate.tsx +++ b/public/pages/CreateIndexTemplate/containers/CreateIndexTemplate/CreateIndexTemplate.tsx @@ -12,7 +12,12 @@ import { CoreServicesContext } from "../../../../components/core_services"; import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext"; import { useUpdateUrlWithDataSourceProperties } from "../../../../components/MDSEnabledComponent"; -interface CreateIndexTemplateProps extends RouteComponentProps<{ template?: string; mode?: string }>, DataSourceMenuProperties {} +interface CreateIndexTemplateProps + extends RouteComponentProps<{ + template?: string; + mode?: string; + }>, + DataSourceMenuProperties {} class CreateIndexTemplate extends Component { static contextType = CoreServicesContext; @@ -74,7 +79,7 @@ class CreateIndexTemplate extends Component { } } -export default function (props: CreateIndexTemplateProps) { +export default function (props: Omit) { const dataSourceMenuProps = useContext(DataSourceMenuContext); useUpdateUrlWithDataSourceProperties(); return ; diff --git a/public/pages/DataStreams/containers/DataStreams/DataStreams.tsx b/public/pages/DataStreams/containers/DataStreams/DataStreams.tsx index eb33cc573..3fbb7cbfe 100644 --- a/public/pages/DataStreams/containers/DataStreams/DataStreams.tsx +++ b/public/pages/DataStreams/containers/DataStreams/DataStreams.tsx @@ -4,7 +4,7 @@ */ import React, { Component, useContext } from "react"; -import { debounce, isEqual } from "lodash"; +import _, { debounce, isEqual } from "lodash"; import { Link, RouteComponentProps } from "react-router-dom"; import queryString from "query-string"; import { @@ -428,7 +428,7 @@ class DataStreams extends MDSEnabledComponent) { +export default function DataStreamsContainer(props: Omit) { const context = useContext(ServicesContext); const dataSourceMenuProperties = useContext(DataSourceMenuContext); return ; diff --git a/public/pages/Indices/containers/Indices/Indices.tsx b/public/pages/Indices/containers/Indices/Indices.tsx index 3955fc507..21c04f967 100644 --- a/public/pages/Indices/containers/Indices/Indices.tsx +++ b/public/pages/Indices/containers/Indices/Indices.tsx @@ -62,6 +62,7 @@ interface IndicesState extends DataSourceMenuProperties { export class Indices extends MDSEnabledComponent { static contextType = CoreServicesContext; + constructor(props: IndicesProps) { super(props); const { from, size, search, sortField, sortDirection, showDataStreams } = getURLQueryParams(this.props.location); @@ -101,14 +102,14 @@ export class Indices extends MDSEnabledComponent { } async componentDidUpdate(prevProps: IndicesProps, prevState: IndicesState) { - const prevQuery = Indices.getQueryObjectFromState(prevState); - const currQuery = Indices.getQueryObjectFromState(this.state); + const prevQuery = this.getQueryObjectFromState(prevState); + const currQuery = this.getQueryObjectFromState(this.state); if (!_.isEqual(prevQuery, currQuery)) { await this.getIndices(); } } - static getQueryObjectFromState({ + getQueryObjectFromState({ from, size, search, @@ -117,14 +118,19 @@ export class Indices extends MDSEnabledComponent { showDataStreams, dataSourceId, }: IndicesState): IndicesQueryParams { - return { from, size, search, sortField, sortDirection, showDataStreams, dataSourceId }; + const queryObj = { from, size, search, sortField, sortDirection, showDataStreams }; + if (!this.props.multiDataSourceEnabled) { + // don't send dataSourceId, if MDS is not enabled + return queryObj; + } + return { ...queryObj, dataSourceId }; } getIndices = async (): Promise => { this.setState({ loadingIndices: true }); try { const { indexService, history } = this.props; - const queryObject = Indices.getQueryObjectFromState(this.state); + const queryObject = this.getQueryObjectFromState(this.state); const queryParamsString = queryString.stringify({ ...queryObject, dataSourceLabel: this.state.dataSourceLabel }); history.replace({ ...this.props.location, search: queryParamsString }); @@ -313,7 +319,7 @@ export class Indices extends MDSEnabledComponent { } } -export default function (props: IndicesProps) { +export default function (props: Omit) { const dataSourceMenuProps = useContext(DataSourceMenuContext); return ; } diff --git a/public/pages/Indices/models/interfaces.ts b/public/pages/Indices/models/interfaces.ts index 7bd32d299..59cafc716 100644 --- a/public/pages/Indices/models/interfaces.ts +++ b/public/pages/Indices/models/interfaces.ts @@ -19,5 +19,5 @@ export interface IndicesQueryParams { sortField: keyof ManagedCatIndex; sortDirection: Direction; showDataStreams: boolean; - dataSourceId: string; + dataSourceId?: string; } diff --git a/public/pages/Main/Main.tsx b/public/pages/Main/Main.tsx index 9f89c5c68..319567772 100644 --- a/public/pages/Main/Main.tsx +++ b/public/pages/Main/Main.tsx @@ -201,8 +201,8 @@ export default class Main extends Component { }; if (this.props.multiDataSourceEnabled && this.isDataSourceEnabledForPath(pathname)) { - services.indexService = new IndexService(http, this.state.dataSourceId); - services.commonService = new CommonService(http, this.state.dataSourceId); + services.indexService = new IndexService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled); + services.commonService = new CommonService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled); } return services; } @@ -311,7 +311,7 @@ export default class Main extends Component { const { landingPage } = this.props; const ROUTE_STYLE = { padding: "25px 25px" }; - const DataSourceMenu = this.props.dataSourceManagement.ui.DataSourceMenu; + const DataSourceMenu = this.props.dataSourceManagement?.ui?.DataSourceMenu; return ( @@ -626,7 +626,7 @@ export default class Main extends Component { path={`${ROUTES.CREATE_INDEX}/:index/:mode`} render={(props: RouteComponentProps) => (
- +
)} /> @@ -642,7 +642,7 @@ export default class Main extends Component { path={ROUTES.CREATE_INDEX} render={(props: RouteComponentProps) => (
- +
)} /> diff --git a/public/pages/Templates/containers/Templates/Templates.tsx b/public/pages/Templates/containers/Templates/Templates.tsx index 47d3ef8b1..737e8c9ab 100644 --- a/public/pages/Templates/containers/Templates/Templates.tsx +++ b/public/pages/Templates/containers/Templates/Templates.tsx @@ -3,8 +3,8 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { Component, useContext } from "react"; -import { debounce, isEqual } from "lodash"; +import React, { useContext } from "react"; +import _, { debounce, isEqual } from "lodash"; import { Link, RouteComponentProps } from "react-router-dom"; import queryString from "query-string"; import { @@ -417,7 +417,7 @@ class Templates extends MDSEnabledComponent { } } -export default function TemplatesContainer(props: Omit) { +export default function TemplatesContainer(props: Omit) { const context = useContext(ServicesContext); const dataSourceMenuProps = useContext(DataSourceMenuContext); return ; diff --git a/public/services/CommonService.test.ts b/public/services/CommonService.test.ts index f332de572..320d6e51a 100644 --- a/public/services/CommonService.test.ts +++ b/public/services/CommonService.test.ts @@ -19,6 +19,7 @@ describe("CommonService spec", () => { expect(httpClientMock.fetch).toHaveBeenCalledWith(`${NODE_API.API_CALLER}`, { method: "POST", body: JSON.stringify(queryObject), + queryObject: undefined, }); }); }); diff --git a/public/services/CommonService.ts b/public/services/CommonService.ts index 333df4ab0..a0247d355 100644 --- a/public/services/CommonService.ts +++ b/public/services/CommonService.ts @@ -13,7 +13,7 @@ export default class CommonService extends MDSEnabledClientService { apiCaller = async (params: IAPICaller, queryObject?: HttpFetchQuery): Promise> => { let url = `${NODE_API.API_CALLER}`; const payload: HttpFetchOptions = {}; - queryObject = this.patchQueryObjectWithDataSourceId(queryObject || {}); + queryObject = this.patchQueryObjectWithDataSourceId(queryObject); payload.method = "POST"; payload.body = JSON.stringify({ data: params.data, diff --git a/public/services/IndexService.ts b/public/services/IndexService.ts index e5565f6a4..49e8baae9 100644 --- a/public/services/IndexService.ts +++ b/public/services/IndexService.ts @@ -88,7 +88,7 @@ export default class IndexService extends MDSEnabledClientService { applyPolicy = async (indices: string[], policyId: string, queryObject: HttpFetchQuery): Promise> => { const body = { indices, policyId }; - queryObject = this.patchQueryObjectWithDataSourceId(body); + queryObject = this.patchQueryObjectWithDataSourceId(queryObject); const url = `..${NODE_API.APPLY_POLICY}`; return (await this.httpClient.post(url, { body: JSON.stringify(body), @@ -98,7 +98,7 @@ export default class IndexService extends MDSEnabledClientService { editRolloverAlias = async (index: string, alias: string, queryObject: HttpFetchQuery): Promise> => { const body = { index, alias }; - queryObject = this.patchQueryObjectWithDataSourceId(body); + queryObject = this.patchQueryObjectWithDataSourceId(queryObject); const url = `..${NODE_API.EDIT_ROLLOVER_ALIAS}`; return (await this.httpClient.post(url, { body: JSON.stringify(body), diff --git a/public/services/MDSEnabledClientService.ts b/public/services/MDSEnabledClientService.ts index 2b7c012d9..14a4f60f0 100644 --- a/public/services/MDSEnabledClientService.ts +++ b/public/services/MDSEnabledClientService.ts @@ -3,15 +3,19 @@ import { HttpFetchQuery, HttpSetup } from "opensearch-dashboards/public"; export abstract class MDSEnabledClientService { httpClient: HttpSetup; dataSourceId: string; + mdsEnabled: boolean; - constructor(httpClient: HttpSetup, dataSourceId: string = "") { + constructor(httpClient: HttpSetup, dataSourceId: string = "", mdsEnabled: boolean = false) { this.httpClient = httpClient; this.dataSourceId = dataSourceId; + this.mdsEnabled = mdsEnabled; } - patchQueryObjectWithDataSourceId(queryObject: HttpFetchQuery) { - queryObject = queryObject || {}; - queryObject.dataSourceId = this.dataSourceId; + patchQueryObjectWithDataSourceId(queryObject?: HttpFetchQuery) { + if (this.mdsEnabled) { + queryObject = queryObject || {}; + queryObject.dataSourceId = this.dataSourceId; + } return queryObject; } } diff --git a/server/services/MDSEnabledClientService.ts b/server/services/MDSEnabledClientService.ts index 8ddfe5fe0..c5bb3404c 100644 --- a/server/services/MDSEnabledClientService.ts +++ b/server/services/MDSEnabledClientService.ts @@ -18,7 +18,7 @@ export abstract class MDSEnabledClientService { context: RequestHandlerContext, request: OpenSearchDashboardsRequest ): (endpoint: string, clientParams: Record, options?: LegacyCallAPIOptions | undefined) => Promise { - const { dataSourceId = "" } = request.query as { dataSourceId?: string }; + const { dataSourceId = "" } = (request.query || {}) as { dataSourceId?: string }; if (this.dataSourceEnabled && dataSourceId && dataSourceId.trim().length != 0) { // non-zero data source id return context.dataSource.opensearch.legacy.getClient(dataSourceId).callAPI;