Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert memory with unit to proper int #131

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 87 additions & 10 deletions lib/php/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,6 @@ private function convertTo01(array $data): array
'drives/free',
'drives/total',
'hardware/etime',
'hardware/memory',
'hardware/swap',
'storages/disksize',
'physical_volumes/free',
'physical_volumes/pe_size',
Expand All @@ -433,16 +431,12 @@ private function convertTo01(array $data): array
'volume_groups/size',
'logical_volumes/seg_count',
'logical_volumes/size',
'memories/capacity',
'memories/numslots',
'processes/pid',
'processes/virtualmemory',
'networks/mtu',
'softwares/filesize',
'virtualmachines/memory',
'virtualmachines/vcpu',
'videos/memory',
'batteries/real_capacity',
'network_ports/ifinerrors',
'network_ports/ifinoctets',
'network_ports/ifinbytes',
Expand All @@ -458,8 +452,6 @@ private function convertTo01(array $data): array
'network_ports/iftype',
'network_components/fru',
'network_components/index',
'network_device/ram',
'network_device/memory',
'network_device/credentials',
'pagecounters/total',
'pagecounters/black',
Expand Down Expand Up @@ -845,7 +837,6 @@ private function convertTo01(array $data): array
}
}


//Fix batteries capacities & voltages
if (isset($data['content']['batteries'])) {
foreach ($data['content']['batteries'] as &$battery) {
Expand Down Expand Up @@ -890,7 +881,6 @@ private function convertTo01(array $data): array
}
}


//type on ports is required
if (isset($data['content']['ports'])) {
foreach ($data['content']['ports'] as &$port) {
Expand All @@ -917,6 +907,45 @@ private function convertTo01(array $data): array
}
}

//fix memories that can have a unit
if (isset($data['content']['hardware']['memory'])) {
$data['content']['hardware']['memory'] = $this->convertMemory($data['content']['hardware']['memory']);
}
if (isset($data['content']['hardware']['swap'])) {
$data['content']['hardware']['swap'] = $this->convertMemory($data['content']['hardware']['swap']);
}
if (isset($data['content']['memories'])) {
foreach ($data['content']['memories'] as &$memory) {
if (isset($memory['capacity'])) {
$memory['capacity'] = $this->convertMemory($memory['capacity']);
}
}
}
if (isset($data['content']['videos'])) {
foreach ($data['content']['videos'] as &$video) {
if (isset($video['memory'])) {
$video['memory'] = $this->convertMemory($video['memory']);
}
}
}
if (isset($data['content']['virtualmachines'])) {
foreach ($data['content']['virtualmachines'] as &$vm) {
if (isset($vm['memory'])) {
$vm['memory'] = $this->convertMemory($vm['memory']);
}
}
}
if (isset($data['content']['network_device'])) {
foreach ($data['content']['network_device'] as &$netdev) {
if (isset($netdev['memory'])) {
$netdev['memory'] = $this->convertMemory($netdev['memory']);
}
if (isset($netdev['ram'])) {
$netdev['ram'] = $this->convertMemory($netdev['ram']);
}
}
}

//no longer existing
$drops = [
'registry',
Expand Down Expand Up @@ -1317,6 +1346,54 @@ public function convertBatteryVoltage(string $voltage)
return false;
}

/**
* Convert memory capacity
*
* @param string $capacity Inventoried capacity
*
* @return ?integer
*/
public function convertMemory(string $capacity)
{
if (is_int($casted_capa = $this->getCastedValue($capacity, 'integer'))) {
return $casted_capa;
}

$mem_pattern = "/^([0-9]+([\.|,][0-9])?) ?(.?B)$/i";

$matches = [];
if (preg_match($mem_pattern, $capacity, $matches)) {
//we got a memory with a unit. first, convert to bytes
$real_value = $this->getCastedValue($matches[1], 'integer');
switch (strtolower($matches[3])) {
case 'pb':
$real_value *= 1024;
//no break, continue to next
case 'tb':
$real_value *= 1024;
//no break, continue to next
case 'gb':
$real_value *= 1024;
//no break, continue to next
case 'mb':
$real_value *= 1024;
//no break, continue to next
case 'kb':
$real_value *= 1024;
//no break, continue to next
case 'b':
break;
default:
return null;
}

//then return as Mb.
return $real_value / 1024 / 1024;
}

return null;
}

/**
* Handle network inventory XML format
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Glpi/Inventory/tests/units/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,4 +887,42 @@ public function testAssetTagFromInventory()
);
$this->assertCount(1, $json->content->network_ports);
}

/**
* Memory capacities convert provider
*
* @return array
*/
public static function memoryCapasToConvertProvider()
{
return [
['2048', 2048],
['2048Mb', 2048],
['2048MB', 2048],
['2048 Mb', 2048],
['2097152Kb', 2048],
['2147483648b', 2048],
['2.0Gb', 2048],
['5Tb', 5242880],
['3Pb', 3221225472]
];
}

/**
* Test memory capacity conversion
*
* @dataProvider memoryCapasToConvertProvider
*
* @param string $orig Original data
* @param string $expected Expected result
*
* @return void
*/
public function testConvertMemoryCapacity($orig, $expected)
{
$instance = new \Glpi\Inventory\Converter();
$this->assertSame($expected, $instance->convertMemory($orig));
}


}
2 changes: 1 addition & 1 deletion tests/data/1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1713,7 +1713,7 @@
<VMTYPE>libvirt</VMTYPE>
</VIRTUALMACHINES>
<VIRTUALMACHINES>
<MEMORY>4194</MEMORY>
<MEMORY>4194MB</MEMORY>
<NAME>win8.1</NAME>
<STATUS>off</STATUS>
<SUBSYSTEM>kvm</SUBSYSTEM>
Expand Down