11import type { NextApiRequest , NextApiResponse } from 'next' ;
2- import { POST } from '@/services/request' ;
3- import { getK8s } from '@/services/backend/kubernetes' ;
42import { jsonRes } from '@/services/backend/response' ;
5- import { authSession } from '@/services/backend/auth' ;
63import 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
4651const 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+ } ;
0 commit comments