Skip to content

Commit

Permalink
update ops
Browse files Browse the repository at this point in the history
  • Loading branch information
zjy365 committed Dec 27, 2024
1 parent ddc0446 commit 63c95f4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
6 changes: 4 additions & 2 deletions frontend/providers/dbprovider/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@
"you_have_successfully_deployed_database": "You have successfully deployed and created a database!",
"change_log": "History",
"VerticalScaling": "Vertical Scaling",
"HorizontalScaling": "Horizontal Scaling",
"VolumeExpansion": "Volume Expansion",
"VerticalScalingCPU": "Vertical Scaling (CPU)",
"VerticalScalingMemory": "Vertical Scaling (Memory)",
"HorizontalScaling": "Horizontal Scaling (Instance)",
"VolumeExpansion": "Volume Expansion (Disk)",
"Stop": "Stop"
}
6 changes: 4 additions & 2 deletions frontend/providers/dbprovider/public/locales/zh/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,9 @@
"you_have_successfully_deployed_database": "您已成功部署创建一个数据库!",
"change_log": "变更历史",
"VerticalScaling": "纵向扩展",
"HorizontalScaling": "横向扩展",
"VolumeExpansion": "卷扩展",
"VerticalScalingCPU": "纵向扩展(CPU)",
"VerticalScalingMemory": "纵向扩展(内存)",
"HorizontalScaling": "横向扩展(实例数)",
"VolumeExpansion": "卷扩展(磁盘)",
"Stop": "停止"
}
7 changes: 1 addition & 6 deletions frontend/providers/dbprovider/src/pages/api/createDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<

const opsRequests = [];

if (cpu !== dbForm.cpu) {
const verticalScalingYaml = json2ResourceOps(dbForm, 'VerticalScaling');
opsRequests.push(verticalScalingYaml);
}

if (memory !== dbForm.memory) {
if (cpu !== dbForm.cpu || memory !== dbForm.memory) {
const verticalScalingYaml = json2ResourceOps(dbForm, 'VerticalScaling');
opsRequests.push(verticalScalingYaml);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { jsonRes } from '@/services/backend/response';
import { ApiResp } from '@/services/kubernet';
import { KubeBlockOpsRequestType } from '@/types/cluster';
import { DBType, OpsRequestItemType } from '@/types/db';
import { cpuFormatToC, memoryFormatToGi } from '@/utils/tools';
import type { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse<ApiResp>) {
Expand Down Expand Up @@ -53,17 +54,17 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<

if (oldConfig?.limits?.cpu !== newConfig?.limits?.cpu) {
changedConfigs.push({
parameterName: item.spec.type,
newValue: newConfig?.limits?.cpu || '-',
oldValue: oldConfig?.limits?.cpu || '-'
parameterName: item.spec.type + 'CPU',
newValue: cpuFormatToC(newConfig?.limits?.cpu) || '-',
oldValue: cpuFormatToC(oldConfig?.limits?.cpu) || '-'
});
}

if (oldConfig?.limits?.memory !== newConfig?.limits?.memory) {
changedConfigs.push({
parameterName: item.spec.type,
newValue: newConfig?.limits?.memory || '-',
oldValue: oldConfig?.limits?.memory || '-'
parameterName: item.spec.type + 'Memory',
newValue: memoryFormatToGi(newConfig?.limits?.memory) || '-',
oldValue: memoryFormatToGi(oldConfig?.limits?.memory) || '-'
});
}

Expand Down
57 changes: 57 additions & 0 deletions frontend/providers/dbprovider/src/utils/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ export const strToBase64 = (str: string) => {
return '';
};

/**
* Format CPU value to standard C format
* @param cpu CPU value, like "500m", "1", "2"
* @returns Standardized CPU value with C suffix, like "0.5C", "1C", "2C"
*/
export const cpuFormatToC = (cpu: string | number = '0'): string => {
if (!cpu || cpu === '0') {
return '0C';
}

let value: number;
const cpuStr = cpu.toString();

if (/m$/i.test(cpuStr)) {
// Handle values with 'm' suffix, like "500m"
value = parseFloat(cpuStr) / 1000;
} else {
// Handle values without unit, like "1", "2"
value = parseFloat(cpuStr);
}

return `${value.toFixed(1)}C`;
};

/**
* cpu format
*/
Expand Down Expand Up @@ -143,6 +167,39 @@ export const memoryFormatToMi = (memory = '0') => {
return Number(value.toFixed(2));
};

/**
* Format memory value to standard Gi format
* @param memory Memory value, like "512Mi", "1Gi", "2048Mi"
* @returns Standardized memory value with Gi suffix, like "0.5Gi", "1Gi", "2Gi"
*/
export const memoryFormatToGi = (memory: string | number = '0'): string => {
if (!memory || memory === '0') {
return '0Gi';
}

let value: number;
const memoryStr = memory.toString();

if (/Mi$/i.test(memoryStr)) {
// Convert Mi to Gi
value = parseFloat(memoryStr) / 1024;
} else if (/Gi$/i.test(memoryStr)) {
// Already in Gi
value = parseFloat(memoryStr);
} else if (/Ti$/i.test(memoryStr)) {
// Convert Ti to Gi
value = parseFloat(memoryStr) * 1024;
} else if (/Ki$/i.test(memoryStr)) {
// Convert Ki to Gi
value = parseFloat(memoryStr) / 1024 / 1024;
} else {
// Assume the value is in bytes and convert to Gi
value = parseFloat(memoryStr) / 1024 / 1024 / 1024;
}

return `${value.toFixed(1)}Gi`;
};

/**
* storage format
*/
Expand Down

0 comments on commit 63c95f4

Please sign in to comment.