Skip to content

Commit

Permalink
Use cockpit.format_bytes() for displaying VM memory
Browse files Browse the repository at this point in the history
Eliminate convertToBestUnit() in favour of our standard API. Use the
default precision of "3" for consistency with other pages, and adjust
the test accordingly.
  • Loading branch information
martinpitt committed Sep 20, 2024
1 parent 613f90a commit dcc9f8f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 28 deletions.
5 changes: 1 addition & 4 deletions src/components/vm/overview/vmOverviewCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ import { HelpIcon } from '@patternfly/react-icons';
import { CPUModal } from './cpuModal.jsx';
import MemoryModal from './memoryModal.jsx';
import {
convertToBestUnit,
rephraseUI,
units,
vmId,
} from '../../../helpers.js';
import { updateVm } from '../../../actions/store-actions.js';
Expand Down Expand Up @@ -111,12 +109,11 @@ class VmOverviewCard extends React.Component {
label={_("Run when host boots")} />
</DescriptionListDescription>
);
const memory = convertToBestUnit(vm.currentMemory, units.KiB);
const memoryLink = (
<DescriptionListDescription id={`${idPrefix}-memory-count`}>
<Flex spaceItems={{ default: 'spaceItemsSm' }}>
<FlexItem>
{cockpit.format("$0 $1", parseFloat(memory.value).toFixed(1), memory.unit)}
{cockpit.format_bytes(vm.currentMemory * 1024, { base2: true })}
</FlexItem>
<Button variant="link" isInline isDisabled={!vm.persistent} onClick={this.openMemory}>
{_("edit")}
Expand Down
22 changes: 7 additions & 15 deletions src/components/vm/vmUsageCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,18 @@ import cockpit from 'cockpit';
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex";
import { Progress, ProgressVariant } from "@patternfly/react-core/dist/esm/components/Progress";

import {
logDebug,
units,
convertToBestUnit,
convertToUnit,
} from "../../helpers.js";
import { logDebug } from "../../helpers.js";

const _ = cockpit.gettext;

class VmUsageTab extends React.Component {
render() {
const vm = this.props.vm;

const rssMem = vm.rssMemory ? vm.rssMemory : 0; // in KiB
const memTotal = vm.currentMemory ? vm.currentMemory : 0; // in KiB
const memRssBest = convertToBestUnit(rssMem, units.KiB);
const memTotalBest = convertToBestUnit(memTotal, units.KiB);
const memTotal = vm.currentMemory ? vm.currentMemory * 1024 : 0;
const [memTotalFmt, memTotalUnit] = cockpit.format_bytes(memTotal, { base2: true, separate: true });
const rssMem = vm.rssMemory ? vm.rssMemory * 1024 : 0;
const memRss = cockpit.format_bytes(rssMem, memTotalUnit, { base2: true, separate: true })[0];

const totalCpus = vm.vcpus && vm.vcpus.count > 0 ? vm.vcpus.count : 0;
const vmCpuUsage = vm.cpuUsage;
Expand All @@ -54,12 +49,9 @@ class VmUsageTab extends React.Component {
<Progress value={rssMem}
className="pf-m-sm"
min={0} max={memTotal}
variant={(rssMem / memTotal * 100) > 90 ? ProgressVariant.danger : ProgressVariant.info}
variant={rssMem / memTotal > 0.9 ? ProgressVariant.danger : ProgressVariant.info}
title={_("Memory")}
label={cockpit.format("$0 / $1 $2",
parseFloat(memRssBest.value.toFixed(1)),
memRssBest.value != 0 ? parseFloat(convertToUnit(memTotal, units.KiB, memRssBest.unit).toFixed(1)) : parseFloat(memTotalBest.value.toFixed(1)),
memRssBest.value != 0 ? memRssBest.unit : memTotalBest.unit)} />
label={cockpit.format("$0 / $1 $2", memRss, memTotalFmt, memTotalUnit)} />
</FlexItem>
<FlexItem className="vcpu-usage-chart">
<Progress value={cpuUsage}
Expand Down
5 changes: 0 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ export function getBestUnit(input, inputUnit) {
return logUnitMap[getLogarithmOfBase1024(convertToUnitVerbose(input, inputUnit, units.B).value)];
}

export function convertToBestUnit(input, inputUnit) {
return convertToUnitVerbose(input, inputUnit,
logUnitMap[getLogarithmOfBase1024(convertToUnitVerbose(input, inputUnit, units.B).value)]);
}

export function convertToUnit(input, inputUnit, outputUnit) {
return convertToUnitVerbose(input, inputUnit, outputUnit).value;
}
Expand Down
2 changes: 1 addition & 1 deletion test/check-machines-create
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,7 @@ vnc_password= "{vnc_passwd}"

# Check configuration changes survived installation
# Check memory settings have persisted
b.wait_in_text("#vm-VmNotInstalled-memory-count", "130.0 MiB")
b.wait_in_text("#vm-VmNotInstalled-memory-count", "130 MiB")
b.click("#vm-VmNotInstalled-memory-count button")
b.wait_visible("#vm-VmNotInstalled-memory-modal-memory")
b.wait_val("#vm-VmNotInstalled-memory-modal-max-memory", "150")
Expand Down
4 changes: 2 additions & 2 deletions test/check-machines-settings
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,11 @@ class TestMachinesSettings(machineslib.VirtualMachinesCase):
b.click("#vm-subVmTest1-memory-modal-save")
b.wait_not_present("#vm-memory-modal")

b.wait_in_text("#vm-subVmTest1-memory-count", f"{f'{current_memory - 10:.1f}'} MiB")
b.wait_in_text("#vm-subVmTest1-memory-count", f"{f'{current_memory - 10}'} MiB")

# Shut off domain and check changes are still there
self.performAction("subVmTest1", "forceOff")
b.wait_in_text("#vm-subVmTest1-memory-count", f"{f'{current_memory - 10:.1f}'} MiB")
b.wait_in_text("#vm-subVmTest1-memory-count", f"{f'{current_memory - 10}'} MiB")

# Click for the edit link
b.click("#vm-subVmTest1-memory-count button")
Expand Down

0 comments on commit dcc9f8f

Please sign in to comment.