Skip to content

Commit

Permalink
Persist datasource across Apps
Browse files Browse the repository at this point in the history
Signed-off-by: Prabhat Sharma <[email protected]>
  • Loading branch information
Prabhat Sharma committed Jul 16, 2024
1 parent f029f40 commit 0669e17
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
16 changes: 16 additions & 0 deletions public/pages/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ import {
import { DataSourceOption } from "../../../../../src/plugins/data_source_management/public/components/data_source_menu/types";
import * as pluginManifest from "../../../opensearch_dashboards.json";
import { DataSourceAttributes } from "../../../../../src/plugins/data_source/common/data_sources";
import { BehaviorSubject } from "rxjs";
import { i18n } from "@osd/i18n";

enum Navigation {
IndexManagement = "Index Management",
Expand Down Expand Up @@ -179,6 +181,15 @@ const dataSourceEnabledPaths: string[] = [
ROUTES.TRANSFORM_DETAILS,
];

const LocalCluster: DataSourceOption = {
label: i18n.translate("dataSource.localCluster", {
defaultMessage: "Local cluster",
}),
id: "",
};

export const dataSourceObservable = new BehaviorSubject<DataSourceOption>(LocalCluster);

export default class Main extends Component<MainProps, MainState> {
constructor(props: MainProps) {
super(props);
Expand All @@ -193,6 +204,10 @@ export default class Main extends Component<MainProps, MainState> {
};
dataSourceId = parsedDataSourceId || "";
dataSourceLabel = parsedDataSourceLabel || "";

if (dataSourceId) {
dataSourceObservable.next({ id: dataSourceId, label: dataSourceLabel });
}
}
this.state = {
dataSourceId,
Expand Down Expand Up @@ -252,6 +267,7 @@ export default class Main extends Component<MainProps, MainState> {
dataSourceId: id,
dataSourceLabel: label,
});
dataSourceObservable.next({ id, label });
}
if (this.state.dataSourceLoading) {
this.setState({
Expand Down
45 changes: 45 additions & 0 deletions public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IndexManagementPluginStart, IndexManagementPluginSetup } from ".";
import {
AppCategory,
AppMountParameters,
AppUpdater,
CoreSetup,
CoreStart,
DEFAULT_APP_CATEGORIES,
Expand All @@ -21,6 +22,10 @@ import { ROUTES } from "./utils/constants";
import { JobHandlerRegister } from "./JobHandler";
import { ManagementOverViewPluginSetup } from "../../../src/plugins/management_overview/public";
import { DataSourceManagementPluginSetup } from "../../../src/plugins/data_source_management/public";
import { dataSourceObservable } from "./pages/Main/Main";
import { BehaviorSubject } from "rxjs";
import { useLocation } from "react-router-dom";
import { DataSourceOption } from "src/plugins/data/public";

interface IndexManagementSetupDeps {
managementOverview?: ManagementOverViewPluginSetup;
Expand All @@ -47,6 +52,27 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
// can retrieve config from initializerContext
}

private updateDefaultRouteOfManagementApplications: AppUpdater = () => {
// let url = new URL(window.location.hash);
// console.log("current url", url);
// let params = new URLSearchParams(window.location.search);
// // replace dataSourceId with the selected data source
// // let dataSourceId = params.get("dataSourceId");
let dataSourceId = dataSourceObservable.value?.id;
console.log("updating dataSourceId", dataSourceId);
let hash = "";
if (dataSourceId) {
hash = `#/?dataSourceId=${dataSourceId}`;
// url.searchParams.set("dataSourceId", dataSourceId);
}
console.log("updated url", `${hash}`);
return {
defaultPath: hash,
};
};

private appStateUpdater = new BehaviorSubject<AppUpdater>(this.updateDefaultRouteOfManagementApplications);

public setup(core: CoreSetup, { managementOverview, dataSourceManagement }: IndexManagementSetupDeps): IndexManagementPluginSetup {
JobHandlerRegister(core);

Expand Down Expand Up @@ -130,6 +156,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.indexes,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.INDICES);
},
Expand All @@ -142,6 +169,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.indexes,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.MANAGED_INDICES);
},
Expand All @@ -154,6 +182,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.indexes,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.DATA_STREAMS);
},
Expand All @@ -166,6 +195,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.indexes,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.ALIASES);
},
Expand All @@ -178,6 +208,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.indexes,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.INDEX_POLICIES);
},
Expand All @@ -190,6 +221,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.indexes,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.TEMPLATES);
},
Expand All @@ -202,6 +234,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.indexes,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.NOTIFICATIONS);
},
Expand All @@ -214,6 +247,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.indexes,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.ROLLUPS);
},
Expand All @@ -226,6 +260,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.indexes,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.TRANSFORMS);
},
Expand All @@ -238,6 +273,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.index_backup_and_recovery,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.SNAPSHOTS);
},
Expand All @@ -250,6 +286,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.index_backup_and_recovery,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.SNAPSHOT_POLICIES);
},
Expand All @@ -262,12 +299,20 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
order: 8040,
category: ISM_CATEGORIES.index_backup_and_recovery,
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
updater$: this.appStateUpdater,
mount: async (params: AppMountParameters) => {
return mountWrapper(params, ROUTES.REPOSITORIES);
},
});
}

dataSourceObservable.subscribe((dataSourceOption) => {
if (dataSourceOption) {
console.log("dataSourceOption", dataSourceOption);
this.appStateUpdater.next(this.updateDefaultRouteOfManagementApplications);
}
});

core.chrome.navGroup.addNavLinksToGroup(DEFAULT_NAV_GROUPS.dataAdministration, [
{
id: `opensearch_index_management_dashboards_${encodeURIComponent(ROUTES.INDICES)}`,
Expand Down

0 comments on commit 0669e17

Please sign in to comment.