Skip to content

Commit d4794ad

Browse files
authored
Merge pull request #173 from warjiang/fix/workload-duration
fix duration for workload
2 parents 122a37e + 4c6aa68 commit d4794ad

File tree

3 files changed

+79
-44
lines changed

3 files changed

+79
-44
lines changed

ui/apps/dashboard/src/main.tsx

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,66 @@ limitations under the License.
1717
import React from 'react';
1818
import ReactDOM from 'react-dom/client';
1919
import App from './App.tsx';
20-
import i18nInstance, {getLang} from '@/utils/i18n';
21-
import {initReactI18next} from 'react-i18next';
22-
import {loader} from '@monaco-editor/react';
20+
import i18nInstance, { getLang } from '@/utils/i18n';
21+
import { initReactI18next } from 'react-i18next';
22+
import dayjs from 'dayjs';
23+
import duration from 'dayjs/plugin/duration';
24+
import relativeTime from 'dayjs/plugin/relativeTime';
25+
import utc from 'dayjs/plugin/utc';
26+
import timezone from 'dayjs/plugin/timezone';
27+
import { loader } from '@monaco-editor/react';
2328
import * as monaco from 'monaco-editor';
2429
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
2530
// https://github.com/remcohaszing/monaco-yaml/issues/150
2631
import yamlWorker from '@/utils/workaround-yaml.worker?worker';
2732
import enTexts from '../locales/en-US.json';
2833
import zhTexts from '../locales/zh-CN.json';
29-
import {initRoute} from '@/routes/route.tsx';
34+
import { initRoute } from '@/routes/route.tsx';
3035

36+
dayjs.extend(duration);
37+
dayjs.extend(relativeTime);
38+
dayjs.extend(utc);
39+
dayjs.extend(timezone);
3140
window.MonacoEnvironment = {
32-
getWorker(_, label) {
33-
if (label === 'yaml') {
34-
return new yamlWorker();
35-
}
36-
return new editorWorker();
37-
},
41+
getWorker(_, label) {
42+
if (label === 'yaml') {
43+
return new yamlWorker();
44+
}
45+
return new editorWorker();
46+
},
3847
};
39-
loader.config({monaco});
48+
loader.config({ monaco });
4049

4150
i18nInstance
42-
.use(initReactI18next) // passes i18n down to react-i18next
43-
.init({
44-
debug: true,
45-
lng: getLang(), // if you're using a language detector, do not define the lng option
46-
fallbackLng: ['zh-CN'],
47-
resources: {
48-
zh: {
49-
translation: zhTexts,
50-
},
51-
en: {
52-
translation: enTexts,
53-
},
54-
},
55-
interpolation: {
56-
escapeValue: false, // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape
57-
},
58-
saveMissing: true, // send not translated keys to endpoint,
59-
react: {
60-
useSuspense: false,
61-
},
62-
})
63-
.then(() => {
64-
initRoute();
65-
ReactDOM.createRoot(document.getElementById('root')!).render(
66-
<React.StrictMode>
67-
<App/>
68-
</React.StrictMode>,
69-
);
70-
})
71-
.catch(() => {
72-
73-
})
51+
.use(initReactI18next) // passes i18n down to react-i18next
52+
.init({
53+
debug: true,
54+
lng: getLang(), // if you're using a language detector, do not define the lng option
55+
fallbackLng: ['zh-CN'],
56+
resources: {
57+
zh: {
58+
translation: zhTexts,
59+
},
60+
en: {
61+
translation: enTexts,
62+
},
63+
},
64+
interpolation: {
65+
escapeValue: false, // react already safes from xss => https://www.i18next.com/translation-function/interpolation#unescape
66+
},
67+
saveMissing: true, // send not translated keys to endpoint,
68+
react: {
69+
useSuspense: false,
70+
},
71+
})
72+
.then(() => {
73+
initRoute();
74+
ReactDOM.createRoot(document.getElementById('root')!).render(
75+
<React.StrictMode>
76+
<App />
77+
</React.StrictMode>,
78+
);
79+
})
80+
.catch((err) => {
81+
console.error('initialization failed:', err);
82+
});

ui/apps/dashboard/src/pages/multicloud-resource-manage/workload/workload-detail-drawer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import styles from './index.module.less';
3535
import { WorkloadKind } from '@/services/base.ts';
3636
import { cn } from '@/utils/cn';
3737
import TagList, { convertLabelToTags } from '@/components/tag-list';
38+
import { calculateDuration } from '@/utils/time.ts';
3839

3940
export interface WorkloadDetailDrawerProps {
4041
open: boolean;
@@ -156,7 +157,7 @@ const WorkloadDetailDrawer: FC<WorkloadDetailDrawerProps> = (props) => {
156157
'4a6341a8bcc68e0b7120dbc89014b6a2',
157158
'持续时间',
158159
)}
159-
value="2h"
160+
value={calculateDuration(detailData?.objectMeta?.creationTimestamp)}
160161
/>
161162
<Statistic
162163
className={styles['no-value']}

ui/apps/dashboard/src/utils/time.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright 2025 The Karmada Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import dayjs from 'dayjs';
18+
19+
export function calculateDuration(start: string | undefined) {
20+
if (!start) return '-';
21+
const startDate = dayjs.utc(start).local();
22+
const endDate = dayjs();
23+
const duration = dayjs.duration(endDate.diff(startDate));
24+
return duration.humanize();
25+
}

0 commit comments

Comments
 (0)