From 2a0e77303953c0fb4832c47903c93f1ea89b05d6 Mon Sep 17 00:00:00 2001 From: Joshua Li Date: Fri, 15 Oct 2021 17:20:39 -0700 Subject: [PATCH 1/2] Redirect legacy notebooks URL to current one Signed-off-by: Joshua Li --- .../notebooks/components/legacy_route.tsx | 35 +++++++++++++++++++ public/plugin.ts | 10 ++++++ 2 files changed, 45 insertions(+) create mode 100644 public/components/notebooks/components/legacy_route.tsx diff --git a/public/components/notebooks/components/legacy_route.tsx b/public/components/notebooks/components/legacy_route.tsx new file mode 100644 index 000000000..7303d5063 --- /dev/null +++ b/public/components/notebooks/components/legacy_route.tsx @@ -0,0 +1,35 @@ +/* + * 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'; + +export const LegacyRoute = (AppMountParameters: AppMountParameters) => { + ReactDOM.render( + + + + + , + AppMountParameters.element + ); + return () => ReactDOM.unmountComponentAtNode(AppMountParameters.element); +}; diff --git a/public/plugin.ts b/public/plugin.ts index aea8f4660..cc6004770 100644 --- a/public/plugin.ts +++ b/public/plugin.ts @@ -50,6 +50,16 @@ export class ObservabilityPlugin implements Plugin Date: Mon, 18 Oct 2021 13:40:22 -0700 Subject: [PATCH 2/2] Add unit test Signed-off-by: Joshua Li --- .../__test__/legacy_route_helpers.test.ts | 50 +++++++++++++++++++ .../helpers/legacy_route_helpers.ts | 20 ++++++++ .../notebooks/components/legacy_route.tsx | 11 +--- 3 files changed, 72 insertions(+), 9 deletions(-) create mode 100644 public/components/notebooks/components/__test__/legacy_route_helpers.test.ts create mode 100644 public/components/notebooks/components/helpers/legacy_route_helpers.ts diff --git a/public/components/notebooks/components/__test__/legacy_route_helpers.test.ts b/public/components/notebooks/components/__test__/legacy_route_helpers.test.ts new file mode 100644 index 000000000..9e2fc78ae --- /dev/null +++ b/public/components/notebooks/components/__test__/legacy_route_helpers.test.ts @@ -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); + }); +}); diff --git a/public/components/notebooks/components/helpers/legacy_route_helpers.ts b/public/components/notebooks/components/helpers/legacy_route_helpers.ts new file mode 100644 index 000000000..39cb455af --- /dev/null +++ b/public/components/notebooks/components/helpers/legacy_route_helpers.ts @@ -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 }; +}; diff --git a/public/components/notebooks/components/legacy_route.tsx b/public/components/notebooks/components/legacy_route.tsx index 7303d5063..678ed5864 100644 --- a/public/components/notebooks/components/legacy_route.tsx +++ b/public/components/notebooks/components/legacy_route.tsx @@ -13,20 +13,13 @@ 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( - + , AppMountParameters.element