Skip to content

Commit

Permalink
fix(mis): 修复自动生成的计费项id不正确的bug (#577)
Browse files Browse the repository at this point in the history
之前的实现中,计算当前租户的下一个计费项id由前端完成,这就导致一个问题:当修改集群配置文件,如修改qos配置后,前端无法获取到之前qos对应的计费项,也就导致无法计算出最新的计费项id,使自动生成的计费项id和数据库中的重复,出现如下行为

![img_v2_61ba19bf-8573-4c61-86ff-1825c8b4a3cg](https://user-images.githubusercontent.com/98016770/233538316-85559481-0891-45f0-8dfe-4fd6580b6d87.jpg)

这个pr将计算过程放到api层完成,修复了这个bug
  • Loading branch information
qhqhqhq committed Apr 23, 2023
1 parent 6236865 commit 2abcb9e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-onions-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scow/mis-web": patch
---

修复了一个 bug,曾导致当用户修改集群配置后,无法计算出正确的计费项 id
1 change: 1 addition & 0 deletions apps/mis-web/src/apis/api.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export const mockApi: MockApi<typeof api> = {
{ cluster: "hpc01", partition: "compute", qos: "low",
priceItem: { itemId: "HPC07", price: numberToMoney(0.02), amountStrategy: "gpu" } },
],
nextId: "1",
}),

setAsInitAdmin: async () => null,
Expand Down
21 changes: 2 additions & 19 deletions apps/mis-web/src/pageComponents/job/ManageJobBillingTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface Props {
data?: {
activeItems: BillingItemType[],
historyItems: BillingItemType[],
nextId: string,
};
loading?: boolean;
tenant?: string;
Expand All @@ -45,25 +46,7 @@ export interface BillingItemType {
}
}

const calculateNextId = (data?: BillingItemType[], tenant?: string) => {
const currentItemIds = data
? data.filter((x) => x.priceItem && x.tenantName === tenant).map((x) => x.priceItem!.itemId) : [];
if (!tenant) {
const nums = currentItemIds.map((x) => parseInt(x)).filter((x) => !isNaN(x));
return (nums.length === 0 ? 1 : Math.max(...nums) + 1).toString();
}
else {
const flag = tenant + "_";
const nums = currentItemIds
.filter((x) => x.startsWith(flag))
.map((x) => parseInt(x.replace(flag, "")))
.filter((x) => !isNaN(x));
return tenant + "_" + (nums.length === 0 ? 1 : Math.max(...nums) + 1);
}
};

export const ManageJobBillingTable: React.FC<Props> = ({ data, loading, tenant, reload }) => {
const nextId = calculateNextId(data?.activeItems, tenant);

return (
<Table
Expand Down Expand Up @@ -176,7 +159,7 @@ export const ManageJobBillingTable: React.FC<Props> = ({ data, loading, tenant,
children: (
<Space>
<EditPriceModalLink
nextId={nextId}
nextId={data!.nextId}
cluster={r.cluster}
partition={r.partition}
qos={r.qos}
Expand Down
27 changes: 26 additions & 1 deletion apps/mis-web/src/pages/api/job/getBillingItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ export interface GetBillingItemsSchema {
* If only returns active billing items
*/
activeOnly: boolean;

}

responses: {
200: {
activeItems: BillingItemType[],
historyItems: BillingItemType[],

/**
* next item id for this tenant
*/
nextId: string;
};
}
}
Expand Down Expand Up @@ -73,6 +79,23 @@ export async function getBillingItems(tenantName: string | undefined, activeOnly
return reply;
}

const calculateNextId = (data?: JobBillingItem[], tenant?: string) => {
const currentItemIds = data
? data.filter((x) => x.tenantName === tenant).map((x) => x.id) : [];
if (!tenant) {
const nums = currentItemIds.map((x) => parseInt(x)).filter((x) => !isNaN(x));
return (nums.length === 0 ? 1 : Math.max(...nums) + 1).toString();
}
else {
const flag = tenant + "_";
const nums = currentItemIds
.filter((x) => x.startsWith(flag))
.map((x) => parseInt(x.replace(flag, "")))
.filter((x) => !isNaN(x));
return tenant + "_" + (nums.length === 0 ? 1 : Math.max(...nums) + 1);
}
};


export default /* #__PURE__*/route<GetBillingItemsSchema>("GetBillingItemsSchema", async (req, res) => {
const { tenant, activeOnly } = req.query;
Expand All @@ -85,6 +108,8 @@ export default /* #__PURE__*/route<GetBillingItemsSchema>("GetBillingItemsSchema

const reply = await getBillingItems(tenant, activeOnly);

const nextId = calculateNextId(reply.activeItems, tenant);

const sourceToBillingItemType = (item: JobBillingItem) => {
const priceItem = item.path.split(".");
return {
Expand All @@ -100,7 +125,7 @@ export default /* #__PURE__*/route<GetBillingItemsSchema>("GetBillingItemsSchema
} as BillingItemType;
};

const result = { activeItems: [] as BillingItemType[], historyItems: [] as BillingItemType[] };
const result = { activeItems: [] as BillingItemType[], historyItems: [] as BillingItemType[], nextId };

for (const [cluster, { slurm: { partitions } }] of Object.entries(runtimeConfig.CLUSTERS_CONFIG)) {
for (const partition of partitions) {
Expand Down

0 comments on commit 2abcb9e

Please sign in to comment.