Skip to content

Commit

Permalink
Fix bugs and add url redirection and testing
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Tackett <[email protected]>
  • Loading branch information
Adam Tackett committed Oct 3, 2024
1 parent e484e45 commit 32fcf79
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 112 deletions.
4 changes: 2 additions & 2 deletions common/constants/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ export const observabilityMetricsTitle = 'Metrics';
export const observabilityMetricsPluginOrder = 5092;

export const observabilityTracesNewNavID = 'observability-traces-nav';
export const observabilityTracesNewNavURL = observabilityTracesNewNavID + '#/traces';
export const observabilityTracesNewNavURL = observabilityTracesNewNavID;
export const observabilityTracesID = 'observability-traces';
export const observabilityTracesTitle = 'Traces';
export const observabilityTracesPluginOrder = 5093;

export const observabilityServicesNewNavID = 'observability-services-nav';
export const observabilityServicesNewNavURL = observabilityServicesNewNavID + '#/services';
export const observabilityServicesNewNavURL = observabilityServicesNewNavID;
export const observabilityServicesID = 'observability-services';
export const observabilityServicesTitle = 'Services';
export const observabilityServicesPluginOrder = 5092;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import {
convertLegacyTraceAnalyticsUrl,
convertTraceAnalyticsNewNavUrl,
} from '../legacy_route_helpers';
import {
observabilityTracesID,
observabilityTracesNewNavID,
observabilityServicesNewNavID,
} from '../../../../../../common/constants/shared';
import { coreRefs } from '../../../../../framework/core_refs';

describe('ConvertLegacyTraceAnalyticsUrl', () => {
it('should convert legacy URL correctly', () => {
const location = {
pathname: '/app/trace-analytics-dashboards',
hash: '#/traces',
search: '?param1=value1',
} as Location;

const result = convertLegacyTraceAnalyticsUrl(location);
expect(result).toBe(`/app/${observabilityTracesID}#/traces?param1=value1`);
});

it('should handle URL with existing hash parameters', () => {
const location = {
pathname: '/app/trace-analytics-dashboards',
hash: '#/traces?existing=param',
search: '?param1=value1',
} as Location;

const result = convertLegacyTraceAnalyticsUrl(location);
expect(result).toBe(`/app/${observabilityTracesID}#/traces?existing=param&param1=value1`);
});
});

describe('ConvertTraceAnalyticsNewNavUrl', () => {
let originalLocation;

beforeEach(() => {
// Save the original window.location object
originalLocation = window.location;

// Mock window.location.assign
Object.defineProperty(window, 'location', {
value: {
assign: jest.fn(),
},
writable: true,
});
});

afterEach(() => {
// Restore the original window.location object after each test
window.location = originalLocation;
});

it('should redirect to the new navigation traces URL if trace ID is present and new nav is enabled', () => {
const locationMock = {
pathname: `/app/${observabilityTracesID}`,
hash: '#/traces/03f9c770db5ee2f1caac0afc36db49ba',
} as Location;

coreRefs.chrome = { navGroup: { getNavGroupEnabled: jest.fn().mockReturnValue(true) } };

convertTraceAnalyticsNewNavUrl(locationMock);

expect(window.location.assign).toHaveBeenCalledWith(
`/app/${observabilityTracesNewNavID}#/traces?datasourceId=&traceId=03f9c770db5ee2f1caac0afc36db49ba`
);
});

it('should redirect to the old navigation traces URL if trace ID is present and new nav is disabled', () => {
const locationMock = {
pathname: `/app/${observabilityTracesID}`,
hash: '#/traces/03f9c770db5ee2f1caac0afc36db49ba',
} as Location;

coreRefs.chrome = { navGroup: { getNavGroupEnabled: jest.fn().mockReturnValue(false) } };

convertTraceAnalyticsNewNavUrl(locationMock);

expect(window.location.assign).toHaveBeenCalledWith(
`/app/${observabilityTracesID}#/traces?datasourceId=&traceId=03f9c770db5ee2f1caac0afc36db49ba`
);
});

it('should redirect to the new navigation services URL if service ID is present and new nav is enabled', () => {
const locationMock = {
pathname: `/app/${observabilityTracesID}`,
hash: '#/services/analytics-service',
} as Location;

coreRefs.chrome = { navGroup: { getNavGroupEnabled: jest.fn().mockReturnValue(true) } };

convertTraceAnalyticsNewNavUrl(locationMock);

expect(window.location.assign).toHaveBeenCalledWith(
`/app/${observabilityServicesNewNavID}#/services?datasourceId=&serviceId=analytics-service`
);
});

it('should redirect to the old navigation services URL if service ID is present and new nav is disabled', () => {
const locationMock = {
pathname: `/app/${observabilityTracesID}`,
hash: '#/services/analytics-service',
} as Location;

coreRefs.chrome = { navGroup: { getNavGroupEnabled: jest.fn().mockReturnValue(false) } };

convertTraceAnalyticsNewNavUrl(locationMock);

expect(window.location.assign).toHaveBeenCalledWith(
`/app/${observabilityTracesID}#/services?datasourceId=&serviceId=analytics-service`
);
});

it('should redirect to new navigation traces page if on root traces page and new nav is enabled', () => {
const locationMock = {
pathname: `/app/${observabilityTracesID}`,
hash: '#/traces',
} as Location;

coreRefs.chrome = { navGroup: { getNavGroupEnabled: jest.fn().mockReturnValue(true) } };

convertTraceAnalyticsNewNavUrl(locationMock);

expect(window.location.assign).toHaveBeenCalledWith(
`/app/${observabilityTracesNewNavID}#/traces`
);
});

it('should redirect to new navigation services page if on root services page and new nav is enabled', () => {
const locationMock = {
pathname: `/app/${observabilityTracesID}`,
hash: '#/services',
} as Location;

coreRefs.chrome = { navGroup: { getNavGroupEnabled: jest.fn().mockReturnValue(true) } };

convertTraceAnalyticsNewNavUrl(locationMock);

expect(window.location.assign).toHaveBeenCalledWith(
`/app/${observabilityServicesNewNavID}#/services`
);
});

it('should not redirect if new nav is disabled and on root traces/services page', () => {
const locationMock = {
pathname: `/app/${observabilityTracesID}`,
hash: '#/services',
} as Location;

coreRefs.chrome = { navGroup: { getNavGroupEnabled: jest.fn().mockReturnValue(false) } };

convertTraceAnalyticsNewNavUrl(locationMock);

expect(window.location.assign).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { observabilityTracesID } from '../../../../../common/constants/shared';
import {
observabilityTracesID,
observabilityTracesNewNavID,
observabilityServicesNewNavID,
} from '../../../../../common/constants/shared';
import { coreRefs } from '../../../../framework/core_refs';

export const convertLegacyTraceAnalyticsUrl = (location: Location) => {
const pathname = location.pathname.replace('trace-analytics-dashboards', observabilityTracesID);
Expand All @@ -12,3 +17,72 @@ export const convertLegacyTraceAnalyticsUrl = (location: Location) => {
}`;
return pathname + hash;
};

export const convertTraceAnalyticsNewNavUrl = (location: Location) => {
const pathname = location.pathname;
const hash = location.hash;

const isNewNavEnabled = coreRefs?.chrome?.navGroup?.getNavGroupEnabled();

// Handle service URLs with IDs
if (hash.includes('#/services/')) {
const serviceId = location.hash.split('/services/')[1]?.split('?')[0] || '';
if (serviceId) {
if (isNewNavEnabled) {
window.location.assign(
`/app/${observabilityServicesNewNavID}#/services?datasourceId=&serviceId=${encodeURIComponent(
serviceId
)}`
);
} else {
window.location.assign(
`/app/${observabilityTracesID}#/services?datasourceId=&serviceId=${encodeURIComponent(
serviceId
)}`
);
}
return;
}
}

// Handle trace URLs with IDs
if (hash.includes('#/traces/')) {
const traceId = location.hash.split('/traces/')[1]?.split('?')[0] || '';
if (traceId) {
if (isNewNavEnabled) {
window.location.assign(
`/app/${observabilityTracesNewNavID}#/traces?datasourceId=&traceId=${encodeURIComponent(
traceId
)}`
);
} else {
window.location.assign(
`/app/${observabilityTracesID}#/traces?datasourceId=&traceId=${encodeURIComponent(
traceId
)}`
);
}
return;
}
}

if (hash === '#/traces') {
if (isNewNavEnabled) {
window.location.assign(`/app/${observabilityTracesNewNavID}#/traces`);
}
return;
}

if (hash === '#/services') {
if (isNewNavEnabled) {
window.location.assign(`/app/${observabilityServicesNewNavID}#/services`);
}
return;
}

if (pathname === `/app/${observabilityTracesID}`) {
if (isNewNavEnabled) {
window.location.assign(`/app/${observabilityTracesNewNavID}#/traces`);
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,31 @@ export function ServiceView(props: ServiceViewProps) {
},
{
text: props.serviceName,
href: `#/services/${encodeURIComponent(props.serviceName)}`,
href: (() => {
const dataSourceId = props.dataSourceMDSId[0].id;
if (dataSourceId && dataSourceId !== '') {
return `#/services?datasourceId=${encodeURIComponent(
dataSourceId
)}&serviceId=${encodeURIComponent(props.serviceName)}`;
} else {
return `#/services?serviceId=${encodeURIComponent(props.serviceName)}`;
}
})(),
},
]
);
props.setDataSourceMenuSelectable?.(false);
}, [props.serviceName, props.setDataSourceMenuSelectable]);

const redirectToServicePage = (service: string) => {
window.location.href = `#/services/${service}`;
const dataSourceId = props.dataSourceMDSId[0].id;
if (dataSourceId && dataSourceId !== '') {
window.location.href = `#/services?datasourceId=${encodeURIComponent(
dataSourceId
)}&serviceId=${encodeURIComponent(service)}`;
} else {
window.location.href = `#/services?datasourceId=&serviceId=${encodeURIComponent(service)}`;
}
};

const onClickConnectedService = (service: string) => {
Expand Down
Loading

0 comments on commit 32fcf79

Please sign in to comment.