Skip to content

Commit 7cca399

Browse files
committed
fix(template): correct resource price calculation with proper unit conversion
1 parent 449e326 commit 7cca399

File tree

2 files changed

+48
-58
lines changed

2 files changed

+48
-58
lines changed
Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
11
import type { NextApiRequest, NextApiResponse } from 'next';
2-
import { POST } from '@/services/request';
3-
import { getK8s } from '@/services/backend/kubernetes';
42
import { jsonRes } from '@/services/backend/response';
5-
import { authSession } from '@/services/backend/auth';
63
import type { userPriceType } from '@/types/user';
74

8-
type properties = {
9-
properties: property[];
5+
type ResourcePriceType = {
6+
data: {
7+
properties: {
8+
name: string;
9+
unit_price: number;
10+
unit: string;
11+
}[];
12+
};
1013
};
1114

12-
type property = {
13-
name: string;
14-
unit_price: number;
15-
unit: string;
15+
const PRICE_SCALE = 1000000;
16+
17+
export const valuationMap: Record<string, number> = {
18+
cpu: 1000,
19+
memory: 1024,
20+
storage: 1024,
21+
'services.nodeports': 1
1622
};
1723

18-
export function transformProperties(data: properties): userPriceType {
19-
const userPrice: userPriceType = {
20-
cpu: 0,
21-
memory: 0,
22-
storage: 0,
23-
nodeports: 0
24-
};
24+
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
25+
try {
26+
const priceResponse = await getResourcePrice();
2527

26-
data.properties.forEach((property: property) => {
27-
switch (property.name) {
28-
case 'cpu':
29-
userPrice.cpu = property.unit_price;
30-
break;
31-
case 'memory':
32-
userPrice.memory = property.unit_price;
33-
break;
34-
case 'storage':
35-
userPrice.storage = property.unit_price;
36-
break;
37-
case 'services.nodeports':
38-
userPrice.nodeports = property.unit_price;
39-
break;
40-
}
41-
});
28+
const data: userPriceType = {
29+
cpu: countSourcePrice(priceResponse, 'cpu'),
30+
memory: countSourcePrice(priceResponse, 'memory'),
31+
storage: countSourcePrice(priceResponse, 'storage'),
32+
nodeports: countSourcePrice(priceResponse, 'services.nodeports')
33+
};
34+
35+
jsonRes<userPriceType>(res, {
36+
data
37+
});
38+
} catch (error) {
39+
console.log(error);
40+
jsonRes(res, { code: 500, message: 'get price error' });
41+
}
42+
}
4243

43-
return userPrice;
44+
function countSourcePrice(rawData: ResourcePriceType['data']['properties'], type: string) {
45+
const rawPrice = rawData.find((item) => item.name === type)?.unit_price || 1;
46+
const sourceScale = rawPrice * (valuationMap[type] || 1);
47+
const unitScale = sourceScale / PRICE_SCALE;
48+
return unitScale;
4449
}
4550

4651
const getResourcePrice = async () => {
@@ -51,20 +56,7 @@ const getResourcePrice = async () => {
5156
const res = await fetch(`${baseUrl}/account/v1alpha1/properties`, {
5257
method: 'POST'
5358
});
54-
const data = await res.json();
55-
return transformProperties(data.data as properties);
56-
};
57-
58-
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
59-
try {
60-
const data = await getResourcePrice();
59+
const data: ResourcePriceType = await res.json();
6160

62-
jsonRes<userPriceType>(res, {
63-
code: 200,
64-
data: data
65-
});
66-
} catch (error) {
67-
console.log('get resoure price error: ', error);
68-
jsonRes(res, { code: 500, message: 'get price error' });
69-
}
70-
}
61+
return data.data.properties;
62+
};

frontend/providers/template/src/pages/deploy/components/PriceBox.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,22 @@ import { type ResourceUsage } from '@/utils/usage';
99
import { PriceIcon } from '@/components/icons/PriceIcon';
1010
import { useSystemConfigStore } from '@/store/config';
1111

12-
const scale = 1000000;
13-
1412
export const usePriceCalculation = ({ cpu, memory, storage, nodeport }: ResourceUsage) => {
1513
const { userSourcePrice } = useUserStore();
1614

1715
return React.useMemo(() => {
1816
if (!userSourcePrice) return [];
1917

20-
const cpuPMin = +((userSourcePrice.cpu * cpu.min * 24) / scale).toFixed(2);
21-
const cpuPMax = +((userSourcePrice.cpu * cpu.max * 24) / scale).toFixed(2);
18+
const cpuPMin = +((userSourcePrice.cpu * cpu.min * 24) / 1000).toFixed(2);
19+
const cpuPMax = +((userSourcePrice.cpu * cpu.max * 24) / 1000).toFixed(2);
2220

23-
const memoryPMin = +((userSourcePrice.memory * memory.min * 24) / scale).toFixed(2);
24-
const memoryPMax = +((userSourcePrice.memory * memory.max * 24) / scale).toFixed(2);
21+
const memoryPMin = +((userSourcePrice.memory * memory.min * 24) / 1024).toFixed(2);
22+
const memoryPMax = +((userSourcePrice.memory * memory.max * 24) / 1024).toFixed(2);
2523

26-
const storagePMin = +((userSourcePrice.storage * storage.min * 24) / scale).toFixed(2);
27-
const storagePMax = +((userSourcePrice.storage * storage.max * 24) / scale).toFixed(2);
24+
const storagePMin = +(userSourcePrice.storage * storage.min * 24).toFixed(2);
25+
const storagePMax = +(userSourcePrice.storage * storage.max * 24).toFixed(2);
2826

29-
const nodePortP = +((userSourcePrice.nodeports * nodeport * 24) / 1000).toFixed(2);
27+
const nodePortP = +(userSourcePrice.nodeports * nodeport * 24).toFixed(2);
3028

3129
const totalPMin = +(cpuPMin + memoryPMin + storagePMin + nodePortP).toFixed(2);
3230
const totalPMax = +(cpuPMax + memoryPMax + storagePMax + nodePortP).toFixed(2);

0 commit comments

Comments
 (0)