Extract variables to utils/unit

Signed-off-by: Andy Lee <andy.lee@suse.com>
(cherry picked from commit 8db4de1c4a1200f4f303e4444b6435c581187d72)
This commit is contained in:
Andy Lee 2025-03-03 16:50:43 +08:00 committed by Mergify
parent a8f8f0d288
commit f02074a25e
18 changed files with 57 additions and 34 deletions

View File

@ -1,3 +1,4 @@
// TODO: delete this not used variable
export const MemoryUnit = [{ export const MemoryUnit = [{
label: 'Mi', label: 'Mi',
value: 'Mi' value: 'Mi'

View File

@ -4,6 +4,7 @@ import { formatSi, exponentNeeded, UNITS } from '@shell/utils/units';
import { HCI as HCI_ANNOTATIONS } from '@pkg/harvester/config/labels-annotations'; import { HCI as HCI_ANNOTATIONS } from '@pkg/harvester/config/labels-annotations';
import { LONGHORN, METRIC } from '@shell/config/types'; import { LONGHORN, METRIC } from '@shell/config/types';
import { Banner } from '@components/Banner'; import { Banner } from '@components/Banner';
import { UNIT_SUFFIX } from '../../utils/unit';
import HarvesterCPUUsed from '../../formatters/HarvesterCPUUsed'; import HarvesterCPUUsed from '../../formatters/HarvesterCPUUsed';
import HarvesterMemoryUsed from '../../formatters/HarvesterMemoryUsed'; import HarvesterMemoryUsed from '../../formatters/HarvesterMemoryUsed';
import HarvesterStorageUsed from '../../formatters/HarvesterStorageUsed'; import HarvesterStorageUsed from '../../formatters/HarvesterStorageUsed';
@ -121,7 +122,7 @@ export default {
memoryUnits() { memoryUnits() {
const exponent = exponentNeeded(this.memoryTotal, 1024); const exponent = exponentNeeded(this.memoryTotal, 1024);
return `${ UNITS[exponent] }i`; return `${ UNITS[exponent] }${ UNIT_SUFFIX }`;
}, },
nodeType() { nodeType() {

View File

@ -5,6 +5,7 @@ import { Banner } from '@components/Banner';
import AsyncButton from '@shell/components/AsyncButton'; import AsyncButton from '@shell/components/AsyncButton';
import UnitInput from '@shell/components/form/UnitInput'; import UnitInput from '@shell/components/form/UnitInput';
import { exceptionToErrorsArray } from '@shell/utils/error'; import { exceptionToErrorsArray } from '@shell/utils/error';
import { GIBIBYTE } from '../utils/unit';
export default { export default {
name: 'HarvesterVMQuotaDialog', name: 'HarvesterVMQuotaDialog',
@ -29,6 +30,7 @@ export default {
data() { data() {
return { return {
GIBIBYTE,
totalSnapshotSize: '', totalSnapshotSize: '',
errors: [] errors: []
}; };
@ -44,7 +46,6 @@ export default {
}, },
methods: { methods: {
close() { close() {
this.totalSnapshotSize = ''; this.totalSnapshotSize = '';
this.$emit('close'); this.$emit('close');
@ -92,7 +93,7 @@ export default {
:input-exponent="3" :input-exponent="3"
:increment="1024" :increment="1024"
:output-modifier="true" :output-modifier="true"
suffix="Gi" :suffix="GIBIBYTE"
class="mb-20" class="mb-20"
/> />
</template> </template>

View File

@ -20,7 +20,7 @@ import Loading from '@shell/components/Loading.vue';
import MessageLink from '@shell/components/MessageLink'; import MessageLink from '@shell/components/MessageLink';
import { ADD_ONS } from '@pkg/harvester/config/harvester-map'; import { ADD_ONS } from '@pkg/harvester/config/harvester-map';
import { PRODUCT_NAME as HARVESTER_PRODUCT } from '@pkg/harvester/config/harvester'; import { PRODUCT_NAME as HARVESTER_PRODUCT } from '@pkg/harvester/config/harvester';
import { UNIT_SUFFIX } from '../../utils/unit';
import { _EDIT } from '@shell/config/query-params'; import { _EDIT } from '@shell/config/query-params';
import { sortBy } from '@shell/utils/sort'; import { sortBy } from '@shell/utils/sort';
import { Banner } from '@components/Banner'; import { Banner } from '@components/Banner';
@ -185,7 +185,7 @@ export default {
minExponent: 3, minExponent: 3,
maxExponent: 3, maxExponent: 3,
maxPrecision: 2, maxPrecision: 2,
suffix: 'i', suffix: UNIT_SUFFIX,
}; };
const longhornDisks = Object.keys(diskStatus).map((key) => { const longhornDisks = Object.keys(diskStatus).map((key) => {
@ -457,7 +457,7 @@ export default {
const devPath = d.spec?.devPath; const devPath = d.spec?.devPath;
const deviceType = d.status?.deviceStatus?.details?.deviceType; const deviceType = d.status?.deviceStatus?.details?.deviceType;
const sizeBytes = d.status?.deviceStatus?.capacity?.sizeBytes; const sizeBytes = d.status?.deviceStatus?.capacity?.sizeBytes;
const size = formatSi(sizeBytes, { increment: 1024, suffix: 'i' }); const size = formatSi(sizeBytes, { increment: 1024, suffix: UNIT_SUFFIX });
const parentDevice = d.status?.deviceStatus?.parentDevice; const parentDevice = d.status?.deviceStatus?.parentDevice;
const isChildAdded = this.newDisks.find((newDisk) => newDisk.blockDevice?.status?.deviceStatus?.parentDevice === devPath); const isChildAdded = this.newDisks.find((newDisk) => newDisk.blockDevice?.status?.deviceStatus?.parentDevice === devPath);
const name = d.displayName; const name = d.displayName;

View File

@ -21,6 +21,7 @@ import { InterfaceOption, VOLUME_DATA_SOURCE_KIND } from '../config/harvester-ma
import { HCI, VOLUME_SNAPSHOT } from '../types'; import { HCI, VOLUME_SNAPSHOT } from '../types';
import { LVM_DRIVER } from '../models/harvester/storage.k8s.io.storageclass'; import { LVM_DRIVER } from '../models/harvester/storage.k8s.io.storageclass';
import { DATA_ENGINE_V2 } from '../models/harvester/persistentvolumeclaim'; import { DATA_ENGINE_V2 } from '../models/harvester/persistentvolumeclaim';
import { GIBIBYTE } from '../utils/unit';
export default { export default {
name: 'HarvesterVolume', name: 'HarvesterVolume',
@ -86,6 +87,7 @@ export default {
imageId, imageId,
snapshots: [], snapshots: [],
images: [], images: [],
GIBIBYTE
}; };
}, },
@ -288,7 +290,7 @@ export default {
const imageSize = Math.max(imageResource?.status?.size, imageResource?.status?.virtualSize); const imageSize = Math.max(imageResource?.status?.size, imageResource?.status?.virtualSize);
if (imageSize) { if (imageSize) {
this.storage = `${ Math.ceil(imageSize / 1024 / 1024 / 1024) }Gi`; this.storage = `${ Math.ceil(imageSize / 1024 / 1024 / 1024) }${ GIBIBYTE }`;
} }
} }
this.update(); this.update();
@ -377,7 +379,7 @@ export default {
:disabled="value?.isLonghornV2 && isEdit" :disabled="value?.isLonghornV2 && isEdit"
required required
class="mb-20" class="mb-20"
suffix="Gi" :suffix="GIBIBYTE"
@update:value="update" @update:value="update"
/> />

View File

@ -1,6 +1,7 @@
<script> <script>
import UnitInput from '@shell/components/form/UnitInput'; import UnitInput from '@shell/components/form/UnitInput';
import InputOrDisplay from '@shell/components/InputOrDisplay'; import InputOrDisplay from '@shell/components/InputOrDisplay';
import { GIBIBYTE } from '../../utils/unit';
export default { export default {
name: 'HarvesterEditCpuMemory', name: 'HarvesterEditCpuMemory',
@ -30,6 +31,7 @@ export default {
data() { data() {
return { return {
GIBIBYTE,
localCpu: this.cpu, localCpu: this.cpu,
localMemory: this.memory localMemory: this.memory
}; };
@ -63,7 +65,7 @@ export default {
if (String(this.localMemory).includes('Gi')) { if (String(this.localMemory).includes('Gi')) {
memory = this.localMemory; memory = this.localMemory;
} else { } else {
memory = `${ this.localMemory }Gi`; memory = `${ this.localMemory }${ GIBIBYTE }`;
} }
if (memory.includes('null')) { if (memory.includes('null')) {
memory = null; memory = null;
@ -113,7 +115,7 @@ export default {
:output-modifier="true" :output-modifier="true"
:disabled="disabled" :disabled="disabled"
required required
suffix="Gi" :suffix="GIBIBYTE"
class="mb-20" class="mb-20"
@update:value="change" @update:value="change"
/> />

View File

@ -1,6 +1,6 @@
<script> <script>
import UnitInput from '@shell/components/form/UnitInput'; import UnitInput from '@shell/components/form/UnitInput';
import { MEBIBYTE } from '../../utils/unit';
export default { export default {
name: 'HarvesterReserved', name: 'HarvesterReserved',
@ -20,7 +20,7 @@ export default {
}, },
data() { data() {
return { memory: this.reservedMemory }; return { MEBIBYTE, memory: this.reservedMemory };
}, },
watch: { watch: {
@ -47,7 +47,7 @@ export default {
:input-exponent="2" :input-exponent="2"
:increment="1024" :increment="1024"
:output-modifier="true" :output-modifier="true"
suffix="Mi" :suffix="MEBIBYTE"
@update:value="change" @update:value="change"
/> />
</template> </template>

View File

@ -11,6 +11,7 @@ import { _CREATE } from '@shell/config/query-params';
import { HCI as HCI_ANNOTATIONS } from '@pkg/harvester/config/labels-annotations'; import { HCI as HCI_ANNOTATIONS } from '@pkg/harvester/config/labels-annotations';
import { HCI } from '../../../../types'; import { HCI } from '../../../../types';
import { VOLUME_TYPE, InterfaceOption } from '../../../../config/harvester-map'; import { VOLUME_TYPE, InterfaceOption } from '../../../../config/harvester-map';
import { GIBIBYTE } from '../../../../utils/unit';
export default { export default {
name: 'HarvesterEditExisting', name: 'HarvesterEditExisting',
@ -59,6 +60,7 @@ export default {
} }
return { return {
GIBIBYTE,
VOLUME_TYPE, VOLUME_TYPE,
InterfaceOption, InterfaceOption,
loading: false, loading: false,
@ -254,7 +256,7 @@ export default {
:label="t('harvester.fields.size')" :label="t('harvester.fields.size')"
:mode="mode" :mode="mode"
:disabled="true" :disabled="true"
suffix="Gi" :suffix="GIBIBYTE"
@update:value="update" @update:value="update"
/> />
</InputOrDisplay> </InputOrDisplay>

View File

@ -12,6 +12,7 @@ import { VOLUME_TYPE, InterfaceOption } from '../../../../config/harvester-map';
import { _VIEW } from '@shell/config/query-params'; import { _VIEW } from '@shell/config/query-params';
import LabelValue from '@shell/components/LabelValue'; import LabelValue from '@shell/components/LabelValue';
import { ucFirst } from '@shell/utils/string'; import { ucFirst } from '@shell/utils/string';
import { GIBIBYTE } from '../../../../utils/unit';
export default { export default {
name: 'HarvesterEditVMImage', name: 'HarvesterEditVMImage',
@ -67,6 +68,7 @@ export default {
data() { data() {
return { return {
GIBIBYTE,
VOLUME_TYPE, VOLUME_TYPE,
InterfaceOption, InterfaceOption,
loading: false, loading: false,
@ -187,7 +189,7 @@ export default {
minExponent: 3, minExponent: 3,
}); });
this.value.size = `${ formatSize }Gi`; this.value.size = `${ formatSize }${ GIBIBYTE }`;
} }
}, },
deep: true, deep: true,
@ -229,7 +231,7 @@ export default {
if (!isIsoImage) { if (!isIsoImage) {
imageSizeGiB = Math.max(imageSizeGiB, 10); imageSizeGiB = Math.max(imageSizeGiB, 10);
} }
this.value['size'] = `${ imageSizeGiB }Gi`; this.value['size'] = `${ imageSizeGiB }${ GIBIBYTE }`;
} }
this.update(); this.update();
@ -336,7 +338,7 @@ export default {
:mode="mode" :mode="mode"
:required="validateRequired" :required="validateRequired"
:disable="isLonghornV2" :disable="isLonghornV2"
suffix="Gi" :suffix="GIBIBYTE"
@update:value="update" @update:value="update"
/> />
</InputOrDisplay> </InputOrDisplay>

View File

@ -12,6 +12,7 @@ import LabelValue from '@shell/components/LabelValue';
import { ucFirst } from '@shell/utils/string'; import { ucFirst } from '@shell/utils/string';
import { LVM_DRIVER } from '../../../../models/harvester/storage.k8s.io.storageclass'; import { LVM_DRIVER } from '../../../../models/harvester/storage.k8s.io.storageclass';
import { DATA_ENGINE_V2 } from '../../../../models/harvester/persistentvolumeclaim'; import { DATA_ENGINE_V2 } from '../../../../models/harvester/persistentvolumeclaim';
import { GIBIBYTE } from '../../../../utils/unit';
export default { export default {
name: 'HarvesterEditVolume', name: 'HarvesterEditVolume',
@ -58,6 +59,7 @@ export default {
data() { data() {
return { return {
GIBIBYTE,
VOLUME_TYPE, VOLUME_TYPE,
InterfaceOption, InterfaceOption,
loading: false, loading: false,
@ -140,7 +142,7 @@ export default {
minExponent: 3, minExponent: 3,
}); });
this.value.size = `${ formatSize }Gi`; this.value.size = `${ formatSize }${ GIBIBYTE }`;
} }
}, },
deep: true, deep: true,
@ -258,7 +260,7 @@ export default {
:required="validateRequired" :required="validateRequired"
:label="t('harvester.fields.size')" :label="t('harvester.fields.size')"
:disabled="isLonghornV2" :disabled="isLonghornV2"
suffix="Gi" :suffix="GIBIBYTE"
@update:value="update" @update:value="update"
/> />
</InputOrDisplay> </InputOrDisplay>

View File

@ -2,7 +2,7 @@
import ConsumptionGauge from '@shell/components/ConsumptionGauge'; import ConsumptionGauge from '@shell/components/ConsumptionGauge';
import { METRIC, NODE } from '@shell/config/types'; import { METRIC, NODE } from '@shell/config/types';
import { formatSi, exponentNeeded, UNITS, parseSi } from '@shell/utils/units'; import { formatSi, exponentNeeded, UNITS, parseSi } from '@shell/utils/units';
import { UNIT_SUFFIX } from '../utils/unit';
export default { export default {
name: 'HarvesterMemoryUsed', name: 'HarvesterMemoryUsed',
components: { ConsumptionGauge }, components: { ConsumptionGauge },
@ -51,7 +51,7 @@ export default {
memoryUnits() { memoryUnits() {
const exponent = exponentNeeded(this.memoryTotal, 1024); const exponent = exponentNeeded(this.memoryTotal, 1024);
return `${ UNITS[exponent] }i`; return `${ UNITS[exponent] }${ UNIT_SUFFIX }`;
}, },
node() { node() {

View File

@ -2,6 +2,7 @@
import ConsumptionGauge from '@shell/components/ConsumptionGauge'; import ConsumptionGauge from '@shell/components/ConsumptionGauge';
import { LONGHORN } from '@shell/config/types'; import { LONGHORN } from '@shell/config/types';
import { formatSi, exponentNeeded, UNITS } from '@shell/utils/units'; import { formatSi, exponentNeeded, UNITS } from '@shell/utils/units';
import { UNIT_SUFFIX } from '../utils/unit';
export default { export default {
name: 'HarvesterStorageUsed', name: 'HarvesterStorageUsed',
@ -75,13 +76,13 @@ export default {
allocatedUnits() { allocatedUnits() {
const exponent = exponentNeeded(this.storageStats.total, 1024); const exponent = exponentNeeded(this.storageStats.total, 1024);
return `${ UNITS[exponent] }i`; return `${ UNITS[exponent] }${ UNIT_SUFFIX }`;
}, },
usedUnits() { usedUnits() {
const exponent = exponentNeeded(this.storageStats.maximum, 1024); const exponent = exponentNeeded(this.storageStats.maximum, 1024);
return `${ UNITS[exponent] }i`; return `${ UNITS[exponent] }${ UNIT_SUFFIX }`;
}, },
formatUsed() { formatUsed() {

View File

@ -25,6 +25,7 @@ import { isEmpty } from '@shell/utils/object';
import { HCI } from '../types'; import { HCI } from '../types';
import HarvesterUpgrade from '../components/HarvesterUpgrade'; import HarvesterUpgrade from '../components/HarvesterUpgrade';
import { PRODUCT_NAME as HARVESTER_PRODUCT } from '../config/harvester'; import { PRODUCT_NAME as HARVESTER_PRODUCT } from '../config/harvester';
import { UNIT_SUFFIX } from '../utils/unit';
dayjs.extend(utc); dayjs.extend(utc);
dayjs.extend(minMax); dayjs.extend(minMax);
@ -32,13 +33,13 @@ dayjs.extend(minMax);
const PARSE_RULES = { const PARSE_RULES = {
format: { format: {
addSuffix: true, addSuffix: true,
firstSuffix: 'B', firstSuffix: UNIT_SUFFIX,
increment: 1024, increment: 1024,
maxExponent: 99, maxExponent: 99,
maxPrecision: 2, maxPrecision: 2,
minExponent: 0, minExponent: 0,
startingExponent: 0, startingExponent: 0,
suffix: 'i', suffix: UNIT_SUFFIX,
} }
}; };

View File

@ -25,6 +25,7 @@ import { HCI_SETTING } from '../../config/settings';
import { HCI } from '../../types'; import { HCI } from '../../types';
import { parseVolumeClaimTemplates } from '../../utils/vm'; import { parseVolumeClaimTemplates } from '../../utils/vm';
import impl, { QGA_JSON, USB_TABLET } from './impl'; import impl, { QGA_JSON, USB_TABLET } from './impl';
import { GIBIBYTE } from '../../utils/unit';
const LONGHORN_V2_DATA_ENGINE = 'longhorn-system/v2-data-engine'; const LONGHORN_V2_DATA_ENGINE = 'longhorn-system/v2-data-engine';
@ -448,7 +449,7 @@ export default {
if (!isIsoImage) { if (!isIsoImage) {
imageSizeGiB = Math.max(imageSizeGiB, 10); imageSizeGiB = Math.max(imageSizeGiB, 10);
} }
size = `${ imageSizeGiB }Gi`; size = `${ imageSizeGiB }${ GIBIBYTE }`;
} }
out.push({ out.push({
@ -558,7 +559,7 @@ export default {
volumeName, volumeName,
container, container,
accessMode, accessMode,
size: `${ formatSize }Gi`, size: `${ formatSize }${ GIBIBYTE }`,
volumeMode: volumeMode || this.customVolumeMode, volumeMode: volumeMode || this.customVolumeMode,
image, image,
type, type,
@ -973,7 +974,7 @@ export default {
parseVolumeClaimTemplate(R, dataVolumeName) { parseVolumeClaimTemplate(R, dataVolumeName) {
if (!String(R.size).includes('Gi') && R.size) { if (!String(R.size).includes('Gi') && R.size) {
R.size = `${ R.size }Gi`; R.size = `${ R.size }${ GIBIBYTE }`;
} }
const out = { const out = {

View File

@ -12,6 +12,7 @@ import { HCI } from '../types';
import { PRODUCT_NAME as HARVESTER_PRODUCT } from '../config/harvester'; import { PRODUCT_NAME as HARVESTER_PRODUCT } from '../config/harvester';
import HarvesterResource from './harvester'; import HarvesterResource from './harvester';
import { CSI_SECRETS } from '@pkg/harvester/config/harvester-map'; import { CSI_SECRETS } from '@pkg/harvester/config/harvester-map';
import { UNIT_SUFFIX } from '../utils/unit';
const { const {
CSI_PROVISIONER_SECRET_NAME, CSI_PROVISIONER_SECRET_NAME,
@ -243,8 +244,8 @@ export default class HciVmImage extends HarvesterResource {
return formatSi(size, { return formatSi(size, {
increment: 1024, increment: 1024,
maxPrecision: 2, maxPrecision: 2,
suffix: 'i', suffix: UNIT_SUFFIX,
firstSuffix: 'i', firstSuffix: UNIT_SUFFIX,
}); });
} }
@ -258,8 +259,8 @@ export default class HciVmImage extends HarvesterResource {
return formatSi(virtualSize, { return formatSi(virtualSize, {
increment: 1024, increment: 1024,
maxPrecision: 2, maxPrecision: 2,
suffix: 'i', suffix: UNIT_SUFFIX,
firstSuffix: 'i', firstSuffix: UNIT_SUFFIX,
}); });
} }

View File

@ -0,0 +1,3 @@
export const UNIT_SUFFIX = 'i';
export const GIBIBYTE = 'Gi';
export const MEBIBYTE = 'Mi';

View File

@ -2,6 +2,7 @@ import { PVC } from '@shell/config/types';
import { isValidMac, isValidDNSLabelName } from '@pkg/utils/regular'; import { isValidMac, isValidDNSLabelName } from '@pkg/utils/regular';
import { SOURCE_TYPE } from '@pkg/config/harvester-map'; import { SOURCE_TYPE } from '@pkg/config/harvester-map';
import { parseVolumeClaimTemplates } from '@pkg/utils/vm'; import { parseVolumeClaimTemplates } from '@pkg/utils/vm';
import { GIBIBYTE } from '../utils/unit';
const maxNameLength = 63; const maxNameLength = 63;
@ -88,7 +89,7 @@ export function vmDisks(spec, getters, errors, validatorArgs, displayKey, value)
} }
if (typeValue?.spec?.resources?.requests?.storage && !/^([0-9][0-9]{0,8})[a-zA-Z]+$/.test(typeValue?.spec?.resources?.requests?.storage)) { if (typeValue?.spec?.resources?.requests?.storage && !/^([0-9][0-9]{0,8})[a-zA-Z]+$/.test(typeValue?.spec?.resources?.requests?.storage)) {
const message = getters['i18n/t']('harvester.validation.generic.maximumSize', { max: '999999999 Gi' }); const message = getters['i18n/t']('harvester.validation.generic.maximumSize', { max: `999999999 ${ GIBIBYTE }` });
errors.push(getters['i18n/t']('harvester.validation.generic.tabError', { prefix, message })); errors.push(getters['i18n/t']('harvester.validation.generic.tabError', { prefix, message }));
} }

View File

@ -1,3 +1,5 @@
import { GIBIBYTE } from '../utils/unit';
export function volumeSize(size, getters, errors, validatorArgs, displayKey, value) { export function volumeSize(size, getters, errors, validatorArgs, displayKey, value) {
if (!size) { if (!size) {
const key = getters['i18n/t']('harvester.volume.size'); const key = getters['i18n/t']('harvester.volume.size');
@ -6,7 +8,7 @@ export function volumeSize(size, getters, errors, validatorArgs, displayKey, val
} }
if (size && !/^([0-9][0-9]{0,8})[a-zA-Z]+$/.test(size)) { if (size && !/^([0-9][0-9]{0,8})[a-zA-Z]+$/.test(size)) {
const message = getters['i18n/t']('harvester.validation.generic.maximumSize', { max: '999999999 Gi' }); const message = getters['i18n/t']('harvester.validation.generic.maximumSize', { max: `999999999 ${ GIBIBYTE }` });
errors.push(message); errors.push(message);
} }