Skip to content

Commit

Permalink
bug fix to switch to default datasource instead of local cluster when…
Browse files Browse the repository at this point in the history
… initial loading (#290)

* bug fix to switch to default datasource instead of local cluster when initial loading

Signed-off-by: Riya Saxena <[email protected]>

* notifications bug fix

Signed-off-by: Riya Saxena <[email protected]>

* notif fixes

Signed-off-by: Riya Saxena <[email protected]>

---------

Signed-off-by: Riya Saxena <[email protected]>
  • Loading branch information
riysaxen-amzn authored Oct 28, 2024
1 parent 1772628 commit 2315c54
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
24 changes: 16 additions & 8 deletions public/pages/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ import {
DataSourceSelectableConfig,
DataSourceViewConfig,
} from "../../../../../src/plugins/data_source_management/public";
import { DataSourceMenuProps, DataSourceOption } from "../../../../../src/plugins/data_source_management/public/components/data_source_menu/types";
import {
DataSourceMenuProps,
DataSourceOption,
} from '../../../../../src/plugins/data_source_management/public/components/data_source_menu/types';
import _ from "lodash";
import { NotificationService } from '../../services';
import * as pluginManifest from "../../../opensearch_dashboards.json";
import { DataSourceAttributes } from "../../../../../src/plugins/data_source/common/data_sources";
import semver from "semver";
import { BehaviorSubject } from 'rxjs';

enum Navigation {
Notifications = 'Notifications',
Expand Down Expand Up @@ -76,20 +80,17 @@ export default class Main extends Component<MainProps, MainState> {

if (props.multiDataSourceEnabled) {
const {
dataSourceId = "",
dataSourceId,
dataSourceLabel = ""
} = queryString.parse(this.props.location.search) as {
dataSourceId?: string;
dataSourceLabel?: string;
};

if (dataSourceId) {
dataSourceObservable.next({ id: dataSourceId, label: dataSourceLabel });
}
this.state = {
...initialState,
dataSourceId: dataSourceId,
dataSourceLabel: dataSourceLabel,
dataSourceId,
dataSourceLabel,
dataSourceReadOnly: false,
/**
* undefined: need data source picker to help to determine which data source to use.
Expand All @@ -108,7 +109,10 @@ export default class Main extends Component<MainProps, MainState> {
}

componentDidUpdate(prevProps: any, prevState: { dataSourceId: string; }) {
if (this.props.multiDataSourceEnabled && (prevState.dataSourceId !== this.state.dataSourceId)) {
if (
this.props.multiDataSourceEnabled &&
prevState.dataSourceId !== this.state.dataSourceId
) {
// Call setServerFeatures when dataSourceId is updated or dataSourceComponent is loaded
this.setServerFeatures();
}
Expand Down Expand Up @@ -153,6 +157,10 @@ export default class Main extends Component<MainProps, MainState> {
}

onSelectedDataSources = (dataSources: DataSourceOption[]) => {
if (dataSources.length == 0) {
//No datasource selected
return;
}
const { id = "", label = "" } = dataSources[0] || {};
if (this.state.dataSourceId !== id || this.state.dataSourceLabel !== label) {
this.setState({
Expand Down
11 changes: 10 additions & 1 deletion public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ export class notificationsDashboardsPlugin
});

private updateDefaultRouteOfManagementApplications: AppUpdater = () => {
const hash = `#/?dataSourceId=${dataSourceObservable.value?.id || ""}`;
const dataSourceValue = dataSourceObservable.value?.id;
let hash = `#/`;
/***
When data source value is undefined,
it means the data source picker has not determined which data source to use(local or default data source)
so we should not append any data source id into hash to avoid impacting the data source picker.
**/
if (dataSourceValue !== undefined) {
hash = `#/?dataSourceId=${dataSourceValue}`;
}
return {
defaultPath: hash,
};
Expand Down
3 changes: 2 additions & 1 deletion public/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ const LocalCluster: DataSourceOption = {
id: "",
};

export const dataSourceObservable = new BehaviorSubject<DataSourceOption>(LocalCluster);
// We should use empty object for default value as local cluster may be disabled
export const dataSourceObservable = new BehaviorSubject<DataSourceOption>({});
10 changes: 7 additions & 3 deletions server/routes/configRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function configRoutes(router: IRouter, dataSourceEnabled: boolean) {
return response.ok({ body: resp });
} catch (error) {
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode || 404,
body: error.message,
});
}
Expand Down Expand Up @@ -244,7 +244,11 @@ export function configRoutes(router: IRouter, dataSourceEnabled: boolean) {
router.get(
{
path: NODE_API.GET_AVAILABLE_FEATURES,
validate: false,
validate: dataSourceEnabled ? {
query: schema.object({
dataSourceId: schema.string(),
}),
} : false,
},
async (context, request, response) => {
const client = MDSEnabledClientService.getClient(request, context, dataSourceEnabled);
Expand Down Expand Up @@ -273,7 +277,7 @@ export function configRoutes(router: IRouter, dataSourceEnabled: boolean) {
return response.ok({ body: availableFeature });
} catch (error) {
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode || 404,
body: error.message,
});
}
Expand Down

0 comments on commit 2315c54

Please sign in to comment.