Skip to content

Commit

Permalink
refactor:Fetch Launchpad pricing from service API (labring#5025)
Browse files Browse the repository at this point in the history
* refactor:Fetch Launchpad pricing from service API

* update
  • Loading branch information
zjy365 committed Sep 3, 2024
1 parent 462d939 commit b4d7709
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 281 deletions.
71 changes: 6 additions & 65 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/providers/applaunchpad/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"axios": "^1.5.1",
"base64-stream": "^1.0.0",
"dayjs": "^1.11.10",
"decimal.js": "^10.4.3",
"dns": "^0.2.2",
"echarts": "^5.4.3",
"fast-json-patch": "^3.1.1",
Expand Down
5 changes: 3 additions & 2 deletions frontend/providers/applaunchpad/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,6 @@
"confirm_to_go": "Confirm",
"app_store": "App Store",
"sealaf": "sealaf",
"total_price_tip": "The estimated cost does not include port fees and traffic fees, and is subject to actual usage."
}
"total_price_tip": "The estimated cost does not include port fees and traffic fees, and is subject to actual usage.",
"nodeports": "NodePorts"
}
5 changes: 3 additions & 2 deletions frontend/providers/applaunchpad/public/locales/zh/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,6 @@
"confirm_to_go": "确认前往",
"sealaf": "云开发",
"app_store": "应用商店",
"total_price_tip": "预估费用不包括端口费用和流量费用,以实际使用为准"
}
"total_price_tip": "预估费用不包括端口费用和流量费用,以实际使用为准",
"nodeports": "外网端口"
}
2 changes: 1 addition & 1 deletion frontend/providers/applaunchpad/src/api/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const getInitData = () => GET<InitDataType>('/api/platform/getInitData');

export const getUserQuota = () =>
GET<{
balance: number;
balance: string;
quota: UserQuotaItemType[];
}>('/api/platform/getQuota');

Expand Down
75 changes: 73 additions & 2 deletions frontend/providers/applaunchpad/src/pages/api/platform/getQuota.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,60 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';
import { authSession } from '@/services/backend/auth';
import { UserQuotaItemType } from '@/types/user';
import Decimal from 'decimal.js';

async function getAmount(req: NextApiRequest): Promise<{
data?: {
balance: number;
deductionBalance: number;
};
}> {
const domain = global.AppConfig.cloud.domain;
const base = `https://account-api.${domain}`;

if (!base) throw Error('not base url');
const { kube_user, kc } = await getK8s({
kubeconfig: await authSession(req.headers)
});

if (kube_user === null) {
return { data: undefined };
}

const body = JSON.stringify({
kubeConfig: kc.exportConfig()
});

const response = await fetch(base + '/account/v1alpha1/account', {
method: 'POST',
body,
headers: {
'Content-Type': 'application/json'
}
});

const data = (await response.json()) as {
account?: {
UserUID: string;
ActivityBonus: number;
EncryptBalance: string;
EncryptDeductionBalance: string;
CreatedAt: Date;
Balance: number;
DeductionBalance: number;
};
};

if (!kc || !data?.account) return { data: undefined };

return {
data: {
balance: data.account.Balance,
deductionBalance: data.account.DeductionBalance
}
};
}

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
Expand All @@ -12,9 +66,26 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)

const quota = await getUserQuota();

jsonRes(res, {
let balance = '0';
try {
const { data } = await getAmount(req);
if (data) {
balance = new Decimal(data.balance)
.minus(new Decimal(data.deductionBalance))
.dividedBy(1000000)
.toFixed(2);
}
} catch (error) {
console.log(error, 'getAmount Error');
}

jsonRes<{
balance: string;
quota: UserQuotaItemType[];
}>(res, {
data: {
quota
quota,
balance
}
});
} catch (error) {
Expand Down
Loading

0 comments on commit b4d7709

Please sign in to comment.