Skip to content

Commit

Permalink
Redirect legacy notebooks URL to current observability one (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuali925 authored Oct 18, 2021
1 parent 6a06e24 commit d360344
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

import { RedirectProps } from "react-router-dom";
import { convertLegacyNotebooksUrl } from "../helpers/legacy_route_helpers";

describe('Test legacy route helpers', () => {
it('converts legacy notebooks url', () => {
const locations = [
{
pathname: '/app/notebooks-dashboards',
search: '',
hash: '#/GQ5icXwBJCegTOBKO4Um',
},
{
pathname: '/app/notebooks-dashboards',
search: '?view=view_both',
hash: '#/clPiPXwBEM7l9gC0xTpA',
},
{
pathname: '/testBasePath/app/notebooks-dashboards',
search: '?view=output_only&security_tenant=global',
hash: `#/GQ5icXwBJCegTOBKO4Um?_g=(time:(from:'2021-10-15T20:25:09.556Z',to:'2021-10-15T20:55:09.556Z'))`,
},
] as Location[];
const expected = [
{
pathname: '/app/observability',
hash: '#/notebooks/GQ5icXwBJCegTOBKO4Um'
},
{
pathname: '/app/observability',
hash: '#/notebooks/clPiPXwBEM7l9gC0xTpA?view=view_both'
},
{
pathname: '/testBasePath/app/observability',
hash: `#/notebooks/GQ5icXwBJCegTOBKO4Um?_g=(time:(from:'2021-10-15T20:25:09.556Z',to:'2021-10-15T20:55:09.556Z'))&view=output_only&security_tenant=global`
},
] as RedirectProps['to'][]
expect(locations.map((location) => convertLegacyNotebooksUrl(location))).toEqual(expected);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

import { RedirectProps } from 'react-router-dom';

export const convertLegacyNotebooksUrl = (location: Location): RedirectProps['to'] => {
const pathname = location.pathname.replace('notebooks-dashboards', 'observability');
const hash = `#/notebooks${location.hash.replace(/^#/, '')}${
location.hash.includes('?') ? location.search.replace(/^\?/, '&') : location.search
}`;
return { pathname, hash };
};
28 changes: 28 additions & 0 deletions public/components/notebooks/components/legacy_route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Redirect, Switch } from 'react-router-dom';
import { AppMountParameters } from '../../../../../../src/core/public';
import { convertLegacyNotebooksUrl } from './helpers/legacy_route_helpers';

export const LegacyRoute = (AppMountParameters: AppMountParameters) => {
ReactDOM.render(
<BrowserRouter forceRefresh={true}>
<Switch>
<Redirect from="/" to={convertLegacyNotebooksUrl(window.location)} />
</Switch>
</BrowserRouter>,
AppMountParameters.element
);
return () => ReactDOM.unmountComponentAtNode(AppMountParameters.element);
};
10 changes: 10 additions & 0 deletions public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ export class ObservabilityPlugin implements Plugin<ObservabilitySetup, Observabi
},
});

// redirect legacy notebooks URL to current URL under observability
core.application.register({
id: "notebooks-dashboards",
title: "",
async mount(params: AppMountParameters) {
const { LegacyRoute } = await import('./components/notebooks/components/legacy_route');
return LegacyRoute(params)
}
});

// Return methods that should be available to other plugins
return {};
}
Expand Down

0 comments on commit d360344

Please sign in to comment.