From 2e31c99ea803a53dc5dcaf2ee2538b020fa05b60 Mon Sep 17 00:00:00 2001 From: Jiahui <4543bxy@gmail.com> Date: Sat, 12 Oct 2024 10:33:01 +0800 Subject: [PATCH] Modify the dbprovider cost estimate obtained through the account service (#5146) --- .../src/pages/api/platform/resourcePrice.ts | 79 ++++++++----------- .../providers/dbprovider/src/types/user.d.ts | 1 + 2 files changed, 33 insertions(+), 47 deletions(-) diff --git a/frontend/providers/dbprovider/src/pages/api/platform/resourcePrice.ts b/frontend/providers/dbprovider/src/pages/api/platform/resourcePrice.ts index 0dcfa2d1f2d..05de97e0ee4 100644 --- a/frontend/providers/dbprovider/src/pages/api/platform/resourcePrice.ts +++ b/frontend/providers/dbprovider/src/pages/api/platform/resourcePrice.ts @@ -1,8 +1,7 @@ import type { NextApiRequest, NextApiResponse } from 'next'; -import * as yaml from 'js-yaml'; -import { getK8s } from '@/services/backend/kubernetes'; import { jsonRes } from '@/services/backend/response'; import { authSession } from '@/services/backend/auth'; +import type { userPriceType } from '@/types/user'; export type Response = { cpu: number; @@ -10,6 +9,17 @@ export type Response = { storage: number; nodeports: number; }; + +type ResourcePriceType = { + data: { + properties: { + name: string; + unit_price: number; + unit: string; + }[]; + }; +}; + type ResourceType = | 'cpu' | 'infra-cpu' @@ -21,75 +31,50 @@ type ResourceType = | 'infra-memory' | 'infra-disk' | 'services.nodeports'; -type PriceCrdType = { - apiVersion: 'account.sealos.io/v1'; - kind: 'PriceQuery'; - status: { - billingRecords: { - price: number; - resourceType: ResourceType; - }[]; - }; -}; + const PRICE_SCALE = 1000000; export const valuationMap: Record = { cpu: 1000, memory: 1024, - storage: 1024 + storage: 1024, + 'services.nodeports': 1000 }; export default async function handler(req: NextApiRequest, res: NextApiResponse) { try { - // source price - const { applyYamlList, k8sCustomObjects, namespace } = await getK8s({ - kubeconfig: await authSession(req) - }); + const priceResponse = await getResourcePrice(); - const crdJson = { - apiVersion: `account.sealos.io/v1`, - kind: 'PriceQuery', - metadata: { - name: 'prices', - namespace - }, - spec: {} - }; - - const crdYaml = yaml.dump(crdJson); - - try { - await applyYamlList([crdYaml], 'replace'); - await new Promise((resolve) => setTimeout(() => resolve(), 1000)); - } catch (error) {} - - const { body: priceResponse } = (await k8sCustomObjects.getNamespacedCustomObject( - 'account.sealos.io', - 'v1', - namespace, - 'pricequeries', - crdJson.metadata.name - )) as { body: PriceCrdType }; - - const data = { + const data: userPriceType = { cpu: countSourcePrice(priceResponse, 'cpu'), memory: countSourcePrice(priceResponse, 'memory'), storage: countSourcePrice(priceResponse, 'storage'), nodeports: countSourcePrice(priceResponse, 'services.nodeports') }; - jsonRes(res, { + jsonRes(res, { data }); } catch (error) { + console.log(error); jsonRes(res, { code: 500, message: 'get price error' }); } } -function countSourcePrice(rawData: PriceCrdType, type: ResourceType) { - const rawPrice = - rawData?.status?.billingRecords.find((item) => item.resourceType === type)?.price || 1; +function countSourcePrice(rawData: ResourcePriceType['data']['properties'], type: ResourceType) { + const rawPrice = rawData.find((item) => item.name === type)?.unit_price || 1; const sourceScale = rawPrice * (valuationMap[type] || 1); const unitScale = sourceScale / PRICE_SCALE; return unitScale; } + +const getResourcePrice = async () => { + const url = process.env.BILLING_URL; + + const res = await fetch(`${url}/account/v1alpha1/properties`, { + method: 'POST' + }); + const data: ResourcePriceType = await res.json(); + + return data.data.properties; +}; diff --git a/frontend/providers/dbprovider/src/types/user.d.ts b/frontend/providers/dbprovider/src/types/user.d.ts index 2d7c9ff54c5..6261dcd88d3 100644 --- a/frontend/providers/dbprovider/src/types/user.d.ts +++ b/frontend/providers/dbprovider/src/types/user.d.ts @@ -25,6 +25,7 @@ export type userPriceType = { cpu: number; memory: number; storage: number; + nodeports: number; gpu?: { alias: string; type: string; price: number; inventory: number; vm: number }[]; };