Skip to content

Commit

Permalink
Modify the dbprovider cost estimate obtained through the account serv…
Browse files Browse the repository at this point in the history
…ice (#5146)
  • Loading branch information
bxy4543 authored Oct 12, 2024
1 parent b38d6b1 commit 2e31c99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
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;
memory: number;
storage: number;
nodeports: number;
};

type ResourcePriceType = {
data: {
properties: {
name: string;
unit_price: number;
unit: string;
}[];
};
};

type ResourceType =
| 'cpu'
| 'infra-cpu'
Expand All @@ -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<string, number> = {
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<void>((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<userPriceType>(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;
};
1 change: 1 addition & 0 deletions frontend/providers/dbprovider/src/types/user.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }[];
};

Expand Down

0 comments on commit 2e31c99

Please sign in to comment.