Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify the dbprovider cost estimate obtained through the account service #5146

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading