Skip to content

Commit 6245137

Browse files
committed
refactor(frontend): use new usage formatting utils
Signed-off-by: Nixieboluo <me@sagirii.me>
1 parent 3ed0fbc commit 6245137

File tree

24 files changed

+47
-325
lines changed

24 files changed

+47
-325
lines changed

frontend/desktop/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@sealos/driver": "workspace:^",
3737
"@sealos/gtm": "workspace:^",
3838
"@sealos/shadcn-ui": "workspace:^",
39+
"@sealos/shared": "workspace:^",
3940
"@sealos/ui": "workspace:^",
4041
"@tailwindcss/postcss": "^4.1.10",
4142
"@tanstack/react-query": "^4.35.3",

frontend/desktop/src/pages/api/workspace/getQuota.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { generateBillingToken, verifyAccessToken } from '@/services/backend/auth
22
import { jsonRes } from '@/services/backend/response';
33
import { WorkspaceQuotaItem } from '@/types/workspace';
44
import type { NextApiRequest, NextApiResponse } from 'next';
5-
import { cpuFormatToM, memoryFormatToMi } from 'sealos-desktop-sdk';
5+
import { cpuFormatToM, memoryFormatToMi, storageFormatToMi } from '@sealos/shared';
66

77
type QuotaStatus = Record<string, string>;
88
type UpstreamQuotaResponse = {
@@ -74,8 +74,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
7474
if (hard['requests.storage'] !== undefined || used['requests.storage'] !== undefined) {
7575
quota.push({
7676
type: 'storage',
77-
limit: memoryFormatToMi(hard['requests.storage'] || ''),
78-
used: memoryFormatToMi(used['requests.storage'] || '')
77+
limit: storageFormatToMi(hard['requests.storage'] || ''),
78+
used: storageFormatToMi(used['requests.storage'] || '')
7979
});
8080
}
8181

frontend/pnpm-lock.yaml

Lines changed: 6 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/providers/applaunchpad/src/pages/app/edit/components/QuotaBox.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const QuotaBox = () => {
4444

4545
const tip = `${t('Total')}: ${(limit / scale).toFixed(2)} ${unit}
4646
${t('common.Used')}: ${(used / scale).toFixed(2)} ${unit}
47-
${t('common.Surplus')}: ${((limit - used) / scale).toFixed(2)} ${unit}`;
47+
${t('common.Surplus')}: ${Math.max(0, limit - used).toFixed(2)} ${unit}`;
4848

4949
return { ...item, tip, color };
5050
});

frontend/providers/applaunchpad/src/utils/adapt.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ import {
3939
gpuResourceKey,
4040
AppSourceConfigs
4141
} from '@/constants/app';
42-
import { cpuFormatToM, memoryFormatToMi, formatPodTime, atobSecretYaml } from '@/utils/tools';
42+
import { formatPodTime, atobSecretYaml } from '@/utils/tools';
4343
import { defaultEditVal } from '@/constants/editApp';
4444
import { customAlphabet } from 'nanoid';
4545
import { has } from 'lodash';
4646
import { lauchpadRemarkKey } from '@/constants/account';
4747
import { getInitData } from '@/api/platform';
48+
import { cpuFormatToM, memoryFormatToMi } from '@sealos/shared';
4849

4950
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz', 12);
5051

@@ -214,7 +215,7 @@ export const adaptPod = (pod: V1Pod): PodDetailType => {
214215
nodeName: pod.spec?.nodeName || 'node name',
215216
ip: pod.status?.podIP || 'pod ip',
216217
restarts: pod.status?.containerStatuses
217-
? (pod.status?.containerStatuses[0]?.restartCount ?? 0)
218+
? pod.status?.containerStatuses[0]?.restartCount ?? 0
218219
: 0,
219220
age: formatPodTime(pod.metadata?.creationTimestamp),
220221
usedCpu: {
@@ -510,8 +511,8 @@ export const adaptAppDetail = async (
510511
domain: isCustomDomain
511512
? SEALOS_DOMAIN
512513
: item?.nodePort
513-
? domain
514-
: domain.split('.').slice(1).join('.') || SEALOS_DOMAIN
514+
? domain
515+
: domain.split('.').slice(1).join('.') || SEALOS_DOMAIN
515516
};
516517
return result;
517518
}) || [],

frontend/providers/applaunchpad/src/utils/tools.ts

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -137,54 +137,6 @@ export const atobSecretYaml = (secret?: string): AppEditType['secret'] => {
137137
return defaultEditVal.secret;
138138
};
139139

140-
/**
141-
* cpu format
142-
*/
143-
export const cpuFormatToM = (cpu: string) => {
144-
if (!cpu || cpu === '0') {
145-
return 0;
146-
}
147-
let value = parseFloat(cpu);
148-
149-
if (/n/gi.test(cpu)) {
150-
value = value / 1000 / 1000;
151-
} else if (/u/gi.test(cpu)) {
152-
value = value / 1000;
153-
} else if (/m/gi.test(cpu)) {
154-
value = value;
155-
} else {
156-
value = value * 1000;
157-
}
158-
if (value < 0.1) return 0;
159-
return Number(value.toFixed(4));
160-
};
161-
162-
/**
163-
* memory format
164-
*/
165-
export const memoryFormatToMi = (memory: string) => {
166-
if (!memory || memory === '0') {
167-
return 0;
168-
}
169-
170-
let value = parseFloat(memory);
171-
172-
if (/Ki/gi.test(memory)) {
173-
value = value / 1024;
174-
} else if (/Mi/gi.test(memory)) {
175-
value = value;
176-
} else if (/Gi/gi.test(memory)) {
177-
value = value * 1024;
178-
} else if (/Ti/gi.test(memory)) {
179-
value = value * 1024 * 1024;
180-
} else {
181-
console.log('Invalid memory value');
182-
value = 0;
183-
}
184-
185-
return Number(value.toFixed(2));
186-
};
187-
188140
/**
189141
* print memory to Mi of Gi
190142
*/

frontend/providers/costcenter/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@emotion/styled": "^11.11.0",
2222
"@kubernetes/client-node": "0.18.0",
2323
"@sealos/shadcn-ui": "workspace:^",
24+
"@sealos/shared": "workspace:^",
2425
"@sealos/ui": "workspace:^",
2526
"@stripe/stripe-js": "^1.54.2",
2627
"@tailwindcss/postcss": "^4.1.10",

frontend/providers/costcenter/src/pages/api/workspace/get-workspace-quota.ts

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { NextApiRequest, NextApiResponse } from 'next';
22
import { makeAPIClientByHeader } from '@/service/backend/region';
33
import { jsonRes } from '@/service/backend/response';
44
import { UserQuotaItem, WorkspaceQuotaRequestSchema } from '@/types/workspace';
5+
import { cpuFormatToM, memoryFormatToMi, storageFormatToMi } from '@sealos/shared';
56

67
type QuotaStatus = Record<string, string>;
78
type UpstreamQuotaResponse = {
@@ -11,47 +12,6 @@ type UpstreamQuotaResponse = {
1112
};
1213
};
1314

14-
const cpuFormatToM = (cpu: string) => {
15-
if (!cpu || cpu === '0') {
16-
return 0;
17-
}
18-
let value = parseFloat(cpu);
19-
20-
if (/n/gi.test(cpu)) {
21-
value = value / 1000 / 1000;
22-
} else if (/u/gi.test(cpu)) {
23-
value = value / 1000;
24-
} else if (/m/gi.test(cpu)) {
25-
value = value;
26-
} else {
27-
value = value * 1000;
28-
}
29-
if (value < 0.1) return 0;
30-
return Number(value.toFixed(4));
31-
};
32-
33-
const memoryFormatToMi = (memory: string) => {
34-
if (!memory || memory === '0') {
35-
return 0;
36-
}
37-
38-
let value = parseFloat(memory);
39-
40-
if (/Ki/gi.test(memory)) {
41-
value = value / 1024;
42-
} else if (/Mi/gi.test(memory)) {
43-
value = value;
44-
} else if (/Gi/gi.test(memory)) {
45-
value = value * 1024;
46-
} else if (/Ti/gi.test(memory)) {
47-
value = value * 1024 * 1024;
48-
} else {
49-
value = 0;
50-
}
51-
52-
return Number(value.toFixed(2));
53-
};
54-
5515
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
5616
if (req.method !== 'POST') {
5717
return jsonRes(res, { code: 405, message: 'Method not allowed' });
@@ -97,8 +57,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
9757
},
9858
{
9959
type: 'storage',
100-
limit: memoryFormatToMi(hard['requests.storage'] || ''),
101-
used: memoryFormatToMi(used['requests.storage'] || '')
60+
limit: storageFormatToMi(hard['requests.storage'] || ''),
61+
used: storageFormatToMi(used['requests.storage'] || '')
10262
},
10363
{
10464
type: 'nodeport',

frontend/providers/cronjob/src/utils/adapt.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
JobList,
99
JobStatus
1010
} from '@/types/job';
11-
import { cpuFormatToM, cron2Time, formatPodTime, memoryFormatToMi } from '@/utils/tools';
11+
import { formatPodTime } from '@/utils/tools';
1212
import {
1313
CoreV1EventList,
1414
V1CronJob,
@@ -25,6 +25,7 @@ import 'cronstrue/locales/en';
2525
import cronParser from 'cron-parser';
2626
import { getLangStore } from './cookieUtils';
2727
import { getJobEvents, getJobPodList } from '@/api/job';
28+
import { cpuFormatToM, memoryFormatToMi } from '@sealos/shared';
2829

2930
export const adaptCronJobList = (job: V1CronJob): CronJobListItemType => {
3031
const LANG_KEY = getLangStore() === 'en' ? 'en' : 'zh_CN';

frontend/providers/cronjob/src/utils/tools.ts

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -58,54 +58,6 @@ export const reactLocalFileContent = (file: File) => {
5858
});
5959
};
6060

61-
/**
62-
* cpu format
63-
*/
64-
export const cpuFormatToM = (cpu = '0') => {
65-
if (!cpu || cpu === '0') {
66-
return 0;
67-
}
68-
let value = parseFloat(cpu);
69-
70-
if (/n/gi.test(cpu)) {
71-
value = value / 1000 / 1000;
72-
} else if (/u/gi.test(cpu)) {
73-
value = value / 1000;
74-
} else if (/m/gi.test(cpu)) {
75-
value = value;
76-
} else {
77-
value = value * 1000;
78-
}
79-
if (value < 0.1) return 0;
80-
return Number(value.toFixed(4));
81-
};
82-
83-
/**
84-
* memory format
85-
*/
86-
export const memoryFormatToMi = (memory = '0') => {
87-
if (!memory || memory === '0') {
88-
return 0;
89-
}
90-
91-
let value = parseFloat(memory);
92-
93-
if (/Ki/gi.test(memory)) {
94-
value = value / 1024;
95-
} else if (/Mi/gi.test(memory)) {
96-
value = value;
97-
} else if (/Gi/gi.test(memory)) {
98-
value = value * 1024;
99-
} else if (/Ti/gi.test(memory)) {
100-
value = value * 1024 * 1024;
101-
} else {
102-
console.log('Invalid memory value');
103-
value = 0;
104-
}
105-
106-
return Number(value.toFixed(2));
107-
};
108-
10961
/**
11062
* format pod createTime
11163
*/

0 commit comments

Comments
 (0)