Skip to content

Commit

Permalink
refactoring types & fixing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ramakrishna Chilaka <[email protected]>
  • Loading branch information
RamakrishnaChilaka committed Mar 26, 2024
1 parent 0adbe58 commit e660051
Show file tree
Hide file tree
Showing 17 changed files with 56 additions and 37 deletions.
3 changes: 2 additions & 1 deletion public/pages/Aliases/containers/Aliases/Aliases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const defaultFilter = {

class Aliases extends MDSEnabledComponent<AliasesProps, AliasesState> {
static contextType = CoreServicesContext;

constructor(props: AliasesProps) {
super(props);
const {
Expand Down Expand Up @@ -482,7 +483,7 @@ class Aliases extends MDSEnabledComponent<AliasesProps, AliasesState> {
}
}

export default function AliasContainer(props: Omit<AliasesProps, "commonService">) {
export default function AliasContainer(props: Omit<AliasesProps, "commonService" | keyof DataSourceMenuProperties>) {
const context = useContext(ServicesContext);
const dataSourceMenuProps = useContext(DataSourceMenuContext);
return <Aliases {...props} commonService={context?.commonService as CommonService} {...dataSourceMenuProps} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -461,7 +461,7 @@ class ComposableTemplates extends MDSEnabledComponent<ComposableTemplatesProps,
}

export default function ComposableTemplatesContainer(
props: Omit<ComposableTemplatesProps, "commonService" | "loading" | "componentMapTemplate">
props: Omit<ComposableTemplatesProps, "commonService" | "loading" | "componentMapTemplate" | keyof DataSourceMenuProperties>
) {
const context = useContext(ServicesContext);
const dataSourceMenuProps = useContext(DataSourceMenuContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class CreateComposableTemplate extends Component<CreateComposableTemplateProps>
}
}

export default function (props: CreateComposableTemplateProps) {
export default function (props: Omit<CreateComposableTemplateProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProps = useContext(DataSourceMenuContext);
useUpdateUrlWithDataSourceProperties();
return <CreateComposableTemplate {...props} {...dataSourceMenuProps} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CreateDataStream extends Component<CreateDataStreamProps> {
}
}

export default function (props: CreateDataStreamProps) {
export default function (props: Omit<CreateDataStreamProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProps = useContext(DataSourceMenuContext);
useUpdateUrlWithDataSourceProperties();
return <CreateDataStream {...props} {...dataSourceMenuProps} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateIndexProps> {
static contextType = CoreServicesContext;

Expand Down Expand Up @@ -63,7 +63,7 @@ export class CreateIndex extends Component<CreateIndexProps> {
}
}

export default function (props: CreateIndexProps) {
export default function (props: CreateIndexPropsBase) {
const dataSourceMenuProperties = useContext(DataSourceMenuContext);
useUpdateUrlWithDataSourceProperties();
return <CreateIndex {...props} {...dataSourceMenuProperties} />;
Expand Down
4 changes: 3 additions & 1 deletion public/pages/CreateIndex/containers/IndexForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export const getAliasActionsByDiffArray = (
}, [] as IAliasAction[]);
};

export interface IndexFormProps extends Pick<IndexDetailProps, "readonly" | "sourceIndices">, DataSourceMenuProperties {
export interface IndexFormProps
extends Pick<IndexDetailProps, "readonly" | "sourceIndices">,
Pick<DataSourceMenuProperties, "dataSourceId"> {
index?: string;
value?: Partial<IndexItemRemote>;
mode?: IndicesUpdateMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateIndexTemplateProps> {
static contextType = CoreServicesContext;
Expand Down Expand Up @@ -74,7 +79,7 @@ class CreateIndexTemplate extends Component<CreateIndexTemplateProps> {
}
}

export default function (props: CreateIndexTemplateProps) {
export default function (props: Omit<CreateIndexTemplateProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProps = useContext(DataSourceMenuContext);
useUpdateUrlWithDataSourceProperties();
return <CreateIndexTemplate {...props} {...dataSourceMenuProps} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -428,7 +428,7 @@ class DataStreams extends MDSEnabledComponent<DataStreamsProps, DataStreamsState
}
}

export default function DataStreamsContainer(props: Omit<DataStreamsProps, "commonService">) {
export default function DataStreamsContainer(props: Omit<DataStreamsProps, "commonService" | keyof DataSourceMenuProperties>) {
const context = useContext(ServicesContext);
const dataSourceMenuProperties = useContext(DataSourceMenuContext);
return <DataStreams {...props} commonService={context?.commonService as CommonService} {...dataSourceMenuProperties} />;
Expand Down
18 changes: 12 additions & 6 deletions public/pages/Indices/containers/Indices/Indices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ interface IndicesState extends DataSourceMenuProperties {

export class Indices extends MDSEnabledComponent<IndicesProps, IndicesState> {
static contextType = CoreServicesContext;

constructor(props: IndicesProps) {
super(props);
const { from, size, search, sortField, sortDirection, showDataStreams } = getURLQueryParams(this.props.location);
Expand Down Expand Up @@ -101,14 +102,14 @@ export class Indices extends MDSEnabledComponent<IndicesProps, IndicesState> {
}

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,
Expand All @@ -117,14 +118,19 @@ export class Indices extends MDSEnabledComponent<IndicesProps, IndicesState> {
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<void> => {
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 });

Expand Down Expand Up @@ -313,7 +319,7 @@ export class Indices extends MDSEnabledComponent<IndicesProps, IndicesState> {
}
}

export default function (props: IndicesProps) {
export default function (props: Omit<IndicesProps, keyof DataSourceMenuProperties>) {
const dataSourceMenuProps = useContext(DataSourceMenuContext);
return <Indices {...props} {...dataSourceMenuProps} />;
}
2 changes: 1 addition & 1 deletion public/pages/Indices/models/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export interface IndicesQueryParams {
sortField: keyof ManagedCatIndex;
sortDirection: Direction;
showDataStreams: boolean;
dataSourceId: string;
dataSourceId?: string;
}
10 changes: 5 additions & 5 deletions public/pages/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ export default class Main extends Component<MainProps, MainState> {
};

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;
}
Expand Down Expand Up @@ -311,7 +311,7 @@ export default class Main extends Component<MainProps, MainState> {
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 (
<CoreServicesConsumer>
Expand Down Expand Up @@ -626,7 +626,7 @@ export default class Main extends Component<MainProps, MainState> {
path={`${ROUTES.CREATE_INDEX}/:index/:mode`}
render={(props: RouteComponentProps) => (
<div style={ROUTE_STYLE}>
<CreateIndex {...props} commonService={services.commonService} />
<CreateIndex {...props} />
</div>
)}
/>
Expand All @@ -642,7 +642,7 @@ export default class Main extends Component<MainProps, MainState> {
path={ROUTES.CREATE_INDEX}
render={(props: RouteComponentProps) => (
<div style={ROUTE_STYLE}>
<CreateIndex {...props} commonService={services.commonService} />
<CreateIndex {...props} />
</div>
)}
/>
Expand Down
6 changes: 3 additions & 3 deletions public/pages/Templates/containers/Templates/Templates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -417,7 +417,7 @@ class Templates extends MDSEnabledComponent<TemplatesProps, TemplatesState> {
}
}

export default function TemplatesContainer(props: Omit<TemplatesProps, "commonService">) {
export default function TemplatesContainer(props: Omit<TemplatesProps, "commonService" | keyof DataSourceMenuProperties>) {
const context = useContext(ServicesContext);
const dataSourceMenuProps = useContext(DataSourceMenuContext);
return <Templates {...props} commonService={context?.commonService as CommonService} {...dataSourceMenuProps} />;
Expand Down
1 change: 1 addition & 0 deletions public/services/CommonService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("CommonService spec", () => {
expect(httpClientMock.fetch).toHaveBeenCalledWith(`${NODE_API.API_CALLER}`, {
method: "POST",
body: JSON.stringify(queryObject),
queryObject: undefined,
});
});
});
2 changes: 1 addition & 1 deletion public/services/CommonService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class CommonService extends MDSEnabledClientService {
apiCaller = async <T>(params: IAPICaller, queryObject?: HttpFetchQuery): Promise<ServerResponse<T>> => {
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,
Expand Down
4 changes: 2 additions & 2 deletions public/services/IndexService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default class IndexService extends MDSEnabledClientService {

applyPolicy = async (indices: string[], policyId: string, queryObject: HttpFetchQuery): Promise<ServerResponse<ApplyPolicyResponse>> => {
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),
Expand All @@ -98,7 +98,7 @@ export default class IndexService extends MDSEnabledClientService {

editRolloverAlias = async (index: string, alias: string, queryObject: HttpFetchQuery): Promise<ServerResponse<AcknowledgedResponse>> => {
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),
Expand Down
12 changes: 8 additions & 4 deletions public/services/MDSEnabledClientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion server/services/MDSEnabledClientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export abstract class MDSEnabledClientService {
context: RequestHandlerContext,
request: OpenSearchDashboardsRequest
): (endpoint: string, clientParams: Record<string, any>, options?: LegacyCallAPIOptions | undefined) => Promise<unknown> {
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;
Expand Down

0 comments on commit e660051

Please sign in to comment.