Skip to content

Commit 56b8b10

Browse files
committed
[#noissue] Handle v2 url format
- Convert it to v3 url format and redirect
1 parent b247b12 commit 56b8b10

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

web-frontend/src/main/v3/apps/web/src/routes/index.tsx

+14-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ import Users from '@/pages/config/Users';
3535
import Alarm from '@/pages/config/Alarm';
3636
import Webhook from '@/pages/config/Webhook';
3737
import { threadDumpRouteLoader } from './loader/threadDump';
38+
import { handleV2RouteLoader } from './loader/handleV2';
3839

39-
const defaultLoader = () => redirect('/serverMap');
40+
const defaultLoader = () => {
41+
return redirect('/serverMap');
42+
};
4043

4144
const router = createBrowserRouter(
4245
[
@@ -46,7 +49,16 @@ const router = createBrowserRouter(
4649
},
4750
{
4851
path: '/main',
49-
loader: defaultLoader,
52+
children: [
53+
{
54+
path: '',
55+
loader: defaultLoader,
56+
},
57+
{
58+
path: ':application/:period/:endTime',
59+
loader: handleV2RouteLoader,
60+
},
61+
],
5062
},
5163
{
5264
path: `${APP_PATH.API_CHECK}`,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { APP_PATH, SEARCH_PARAMETER_DATE_FORMAT } from '@pinpoint-fe/constants';
2+
import { convertTimeStringToTime, convertParamsToQueryString } from '@pinpoint-fe/utils';
3+
import { parse, format, subMilliseconds } from 'date-fns';
4+
import { LoaderFunctionArgs, redirect } from 'react-router-dom';
5+
6+
export const handleV2RouteLoader = ({ params, request }: LoaderFunctionArgs) => {
7+
const basePath = `${APP_PATH.SERVER_MAP}/${params.application}`;
8+
const v2QueryParams = Object.fromEntries(new URL(request.url).searchParams);
9+
const currentDate = new Date();
10+
11+
const to = parse(params.endTime!, SEARCH_PARAMETER_DATE_FORMAT, currentDate);
12+
try {
13+
const timeDiff = convertTimeStringToTime(params.period!);
14+
const from = subMilliseconds(to, timeDiff);
15+
const formattedDateRange = {
16+
from: format(from, SEARCH_PARAMETER_DATE_FORMAT),
17+
to: format(to, SEARCH_PARAMETER_DATE_FORMAT),
18+
};
19+
const v3DateQueryString = convertParamsToQueryString({
20+
...formattedDateRange,
21+
...v2QueryParams,
22+
});
23+
24+
return redirect(`${basePath}?${v3DateQueryString}`);
25+
} catch (e) {
26+
return redirect(`${basePath}`);
27+
}
28+
};

web-frontend/src/main/v3/packages/utils/src/date.ts

+18
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,21 @@ export const formatNewLinedDateString = (date: Date | number) => {
105105

106106
return `${format(date, firstFormat)}\n${format(date, secondFormat)}`;
107107
};
108+
109+
export const convertTimeStringToTime = (timeString: string) => {
110+
const timePattern = /^(\d+)([mhd])$/;
111+
const match = timeString.match(timePattern);
112+
const value = Number(match?.[1]);
113+
const unit = match?.[2];
114+
115+
switch (unit) {
116+
case 'm':
117+
return value * 60 * 1000;
118+
case 'h':
119+
return value * 60 * 60 * 1000;
120+
case 'd':
121+
return value * 24 * 60 * 60 * 1000;
122+
default:
123+
throw new Error('Unknown time unit');
124+
}
125+
};

0 commit comments

Comments
 (0)