From 1ba2dd04fa24ad3768d0e7e945b7aff39797f26e Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Tue, 30 Apr 2024 14:58:05 -0300 Subject: [PATCH 01/14] Created "convert_byte_size" method to convert the memory and disk size according to unit informed. Changed "get_extra_context" method from "ClusterView" to use the method above and convert all the disks and memories from VMs to normalize the units. --- netbox/netbox/utils.py | 18 ++++++++++++++++++ netbox/templates/virtualization/cluster.html | 4 ++-- netbox/virtualization/views.py | 13 ++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/netbox/netbox/utils.py b/netbox/netbox/utils.py index f27d1b5f7fc..27f67744b18 100644 --- a/netbox/netbox/utils.py +++ b/netbox/netbox/utils.py @@ -24,3 +24,21 @@ def _wrapper(cls): return cls return _wrapper + + +def convert_byte_size(value, unit="mega"): + """ + Convert a size value to unit. + """ + factors = { + "kilo": 1024, + "mega": 1024 ** 2, + "giga": 1024 ** 3, + "tera": 1024 ** 4, + } + if value: + if len(str(value)) < 6: + return value + value_converted = float(value) / factors[unit] + return value_converted + return 0 diff --git a/netbox/templates/virtualization/cluster.html b/netbox/templates/virtualization/cluster.html index 264a275e994..d74ead0fa34 100644 --- a/netbox/templates/virtualization/cluster.html +++ b/netbox/templates/virtualization/cluster.html @@ -59,9 +59,9 @@
{% trans "Allocated Resources" %}
{% trans "Memory" %} {% if memory_sum %} - {{ memory_sum|humanize_megabytes }} + {{ memory_sum }} {% trans "MB" context "Abbreviation for megabyte" %} {% else %} - {{ ''|placeholder }} + {{ ''|placeholder }} {% endif %} diff --git a/netbox/virtualization/views.py b/netbox/virtualization/views.py index 1ddd2c92d39..775561e95c3 100644 --- a/netbox/virtualization/views.py +++ b/netbox/virtualization/views.py @@ -16,6 +16,7 @@ from ipam.models import IPAddress from ipam.tables import InterfaceVLANTable from netbox.constants import DEFAULT_ACTION_PERMISSIONS +from netbox.utils import convert_byte_size from netbox.views import generic from tenancy.views import ObjectContactsView from utilities.query import count_related @@ -172,7 +173,17 @@ class ClusterView(generic.ObjectView): queryset = Cluster.objects.all() def get_extra_context(self, request, instance): - return instance.virtual_machines.aggregate(vcpus_sum=Sum('vcpus'), memory_sum=Sum('memory'), disk_sum=Sum('disk')) + vm_memory = [convert_byte_size(item.memory, 'mega') for item in instance.virtual_machines.all() if item.memory] + vm_disk = [convert_byte_size(item.disk, 'giga') for item in instance.virtual_machines.all() if item.disk] + + memory_sum = sum(vm_memory) + disk_sum = sum(vm_disk) + + extra_content = instance.virtual_machines.aggregate(vcpus_sum=Sum('vcpus')) + extra_content['memory_sum'] = f"{memory_sum:.0f}" + extra_content['disk_sum'] = f"{disk_sum:.0f}" + + return extra_content @register_model_view(Cluster, 'virtualmachines', path='virtual-machines') From 07305f9037ac2c52d1b162968c30137f82ea2750 Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Tue, 30 Apr 2024 15:17:26 -0300 Subject: [PATCH 02/14] Changed decimal size for memory_sum and disk_sum --- netbox/netbox/utils.py | 1 + netbox/virtualization/views.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/netbox/netbox/utils.py b/netbox/netbox/utils.py index 27f67744b18..f8f6bcc6568 100644 --- a/netbox/netbox/utils.py +++ b/netbox/netbox/utils.py @@ -37,6 +37,7 @@ def convert_byte_size(value, unit="mega"): "tera": 1024 ** 4, } if value: + # If the value is less than 6 digits, it understands the value is expressed according to the unit. if len(str(value)) < 6: return value value_converted = float(value) / factors[unit] diff --git a/netbox/virtualization/views.py b/netbox/virtualization/views.py index 775561e95c3..064a362cafb 100644 --- a/netbox/virtualization/views.py +++ b/netbox/virtualization/views.py @@ -180,8 +180,8 @@ def get_extra_context(self, request, instance): disk_sum = sum(vm_disk) extra_content = instance.virtual_machines.aggregate(vcpus_sum=Sum('vcpus')) - extra_content['memory_sum'] = f"{memory_sum:.0f}" - extra_content['disk_sum'] = f"{disk_sum:.0f}" + extra_content['memory_sum'] = f"{memory_sum:.2f}" + extra_content['disk_sum'] = f"{disk_sum:.2f}" return extra_content From 2567ef297cd4e2c0d12c4129217e16d95551ec82 Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Wed, 1 May 2024 09:17:37 -0300 Subject: [PATCH 03/14] Added test for convert_byte_size. --- netbox/netbox/tests/test_convert_byte_size.py | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 netbox/netbox/tests/test_convert_byte_size.py diff --git a/netbox/netbox/tests/test_convert_byte_size.py b/netbox/netbox/tests/test_convert_byte_size.py new file mode 100644 index 00000000000..f5d8b4a5591 --- /dev/null +++ b/netbox/netbox/tests/test_convert_byte_size.py @@ -0,0 +1,25 @@ +from netbox.utils import convert_byte_size +from utilities.testing import TestCase + + +class TestConvertByteSize(TestCase): + def test_convert_byte_size_returns_original_value_for_small_numbers(self): + self.assertEqual(convert_byte_size(500, "kilo"), 500) + + def test_convert_byte_size_converts_kilobytes_to_bytes(self): + self.assertEqual(convert_byte_size(1024, "kilo"), 1024) + + def test_convert_byte_size_converts_megabytes_to_bytes(self): + self.assertEqual(convert_byte_size(1048576, "mega"), 1) + + def test_convert_byte_size_converts_gigabytes_to_bytes(self): + self.assertEqual(convert_byte_size(1073741824, "giga"), 1) + + def test_convert_byte_size_converts_terabytes_to_bytes(self): + self.assertEqual(convert_byte_size(1099511627776, "tera"), 1) + + def test_convert_byte_size_returns_zero_for_none(self): + self.assertEqual(convert_byte_size(None, "mega"), 0) + + def test_convert_byte_size_without_unit(self): + self.assertEqual(round(convert_byte_size(123456789), 2), 117.74) From 872f1e9800598bc25d3e26dde52f2a8d5fb3dd36 Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Thu, 9 May 2024 15:41:50 -0300 Subject: [PATCH 04/14] Fixed --- netbox/netbox/utils.py | 19 ------------------- netbox/templates/virtualization/cluster.html | 2 +- netbox/utilities/templatetags/helpers.py | 4 +++- netbox/virtualization/views.py | 13 +------------ 4 files changed, 5 insertions(+), 33 deletions(-) diff --git a/netbox/netbox/utils.py b/netbox/netbox/utils.py index f8f6bcc6568..f27d1b5f7fc 100644 --- a/netbox/netbox/utils.py +++ b/netbox/netbox/utils.py @@ -24,22 +24,3 @@ def _wrapper(cls): return cls return _wrapper - - -def convert_byte_size(value, unit="mega"): - """ - Convert a size value to unit. - """ - factors = { - "kilo": 1024, - "mega": 1024 ** 2, - "giga": 1024 ** 3, - "tera": 1024 ** 4, - } - if value: - # If the value is less than 6 digits, it understands the value is expressed according to the unit. - if len(str(value)) < 6: - return value - value_converted = float(value) / factors[unit] - return value_converted - return 0 diff --git a/netbox/templates/virtualization/cluster.html b/netbox/templates/virtualization/cluster.html index d74ead0fa34..7628bf8765c 100644 --- a/netbox/templates/virtualization/cluster.html +++ b/netbox/templates/virtualization/cluster.html @@ -59,7 +59,7 @@
{% trans "Allocated Resources" %}
{% trans "Memory" %} {% if memory_sum %} - {{ memory_sum }} {% trans "MB" context "Abbreviation for megabyte" %} + {{ memory_sum|humanize_megabytes }} {% else %} {{ ''|placeholder }} {% endif %} diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index e8686f8c6c6..175d283a35f 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -96,11 +96,13 @@ def humanize_megabytes(mb): """ if not mb: return '' + if len(str(mb)) < 6: + return mb if not mb % 1048576: # 1024^2 return f'{int(mb / 1048576)} TB' if not mb % 1024: return f'{int(mb / 1024)} GB' - return f'{mb} MB' + return f'{float(mb)/(1048576):.2f} MB' @register.filter() diff --git a/netbox/virtualization/views.py b/netbox/virtualization/views.py index 064a362cafb..1ddd2c92d39 100644 --- a/netbox/virtualization/views.py +++ b/netbox/virtualization/views.py @@ -16,7 +16,6 @@ from ipam.models import IPAddress from ipam.tables import InterfaceVLANTable from netbox.constants import DEFAULT_ACTION_PERMISSIONS -from netbox.utils import convert_byte_size from netbox.views import generic from tenancy.views import ObjectContactsView from utilities.query import count_related @@ -173,17 +172,7 @@ class ClusterView(generic.ObjectView): queryset = Cluster.objects.all() def get_extra_context(self, request, instance): - vm_memory = [convert_byte_size(item.memory, 'mega') for item in instance.virtual_machines.all() if item.memory] - vm_disk = [convert_byte_size(item.disk, 'giga') for item in instance.virtual_machines.all() if item.disk] - - memory_sum = sum(vm_memory) - disk_sum = sum(vm_disk) - - extra_content = instance.virtual_machines.aggregate(vcpus_sum=Sum('vcpus')) - extra_content['memory_sum'] = f"{memory_sum:.2f}" - extra_content['disk_sum'] = f"{disk_sum:.2f}" - - return extra_content + return instance.virtual_machines.aggregate(vcpus_sum=Sum('vcpus'), memory_sum=Sum('memory'), disk_sum=Sum('disk')) @register_model_view(Cluster, 'virtualmachines', path='virtual-machines') From c91c25d059749090305a029070b8711ee2e451c3 Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Thu, 9 May 2024 15:44:27 -0300 Subject: [PATCH 05/14] Addressed PR comments. Changed humanize_megabytes in helpers.py --- netbox/netbox/tests/test_convert_byte_size.py | 25 ------------------- 1 file changed, 25 deletions(-) delete mode 100644 netbox/netbox/tests/test_convert_byte_size.py diff --git a/netbox/netbox/tests/test_convert_byte_size.py b/netbox/netbox/tests/test_convert_byte_size.py deleted file mode 100644 index f5d8b4a5591..00000000000 --- a/netbox/netbox/tests/test_convert_byte_size.py +++ /dev/null @@ -1,25 +0,0 @@ -from netbox.utils import convert_byte_size -from utilities.testing import TestCase - - -class TestConvertByteSize(TestCase): - def test_convert_byte_size_returns_original_value_for_small_numbers(self): - self.assertEqual(convert_byte_size(500, "kilo"), 500) - - def test_convert_byte_size_converts_kilobytes_to_bytes(self): - self.assertEqual(convert_byte_size(1024, "kilo"), 1024) - - def test_convert_byte_size_converts_megabytes_to_bytes(self): - self.assertEqual(convert_byte_size(1048576, "mega"), 1) - - def test_convert_byte_size_converts_gigabytes_to_bytes(self): - self.assertEqual(convert_byte_size(1073741824, "giga"), 1) - - def test_convert_byte_size_converts_terabytes_to_bytes(self): - self.assertEqual(convert_byte_size(1099511627776, "tera"), 1) - - def test_convert_byte_size_returns_zero_for_none(self): - self.assertEqual(convert_byte_size(None, "mega"), 0) - - def test_convert_byte_size_without_unit(self): - self.assertEqual(round(convert_byte_size(123456789), 2), 117.74) From d3e9906af3335a916ccbcea344cbdcbaaa135a8d Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Thu, 9 May 2024 16:17:17 -0300 Subject: [PATCH 06/14] Addressed PR comments. Changed humanize_megabytes in helpers.py --- netbox/utilities/templatetags/helpers.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 175d283a35f..46431c4b7fd 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -96,8 +96,6 @@ def humanize_megabytes(mb): """ if not mb: return '' - if len(str(mb)) < 6: - return mb if not mb % 1048576: # 1024^2 return f'{int(mb / 1048576)} TB' if not mb % 1024: From deb6e9922cbe610f53e26c96a359a00ed90292ef Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Thu, 9 May 2024 16:25:39 -0300 Subject: [PATCH 07/14] Linter issues for helpers.py --- netbox/utilities/templatetags/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 46431c4b7fd..0c089b246a9 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -100,7 +100,7 @@ def humanize_megabytes(mb): return f'{int(mb / 1048576)} TB' if not mb % 1024: return f'{int(mb / 1024)} GB' - return f'{float(mb)/(1048576):.2f} MB' + return f'{int(mb) / (1048576):.2f} MB' @register.filter() From a1fd983173caa10825a7097e77cd03732e1ac37f Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Thu, 23 May 2024 16:04:08 -0300 Subject: [PATCH 08/14] Changed humanize_megabytes --- netbox/templates/virtualization/cluster.html | 2 +- netbox/utilities/templatetags/helpers.py | 29 +++++++++++++++---- .../tests/test_humanize_megabytes_helper.py | 25 ++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 netbox/utilities/tests/test_humanize_megabytes_helper.py diff --git a/netbox/templates/virtualization/cluster.html b/netbox/templates/virtualization/cluster.html index 7628bf8765c..264a275e994 100644 --- a/netbox/templates/virtualization/cluster.html +++ b/netbox/templates/virtualization/cluster.html @@ -61,7 +61,7 @@
{% trans "Allocated Resources" %}
{% if memory_sum %} {{ memory_sum|humanize_megabytes }} {% else %} - {{ ''|placeholder }} + {{ ''|placeholder }} {% endif %} diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 0c089b246a9..e2a16d0889f 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -93,14 +93,31 @@ def humanize_speed(speed): def humanize_megabytes(mb): """ Express a number of megabytes in the most suitable unit (e.g. gigabytes or terabytes). + It considers the mb value as megabytes and converts it to the most suitable unit. """ + + # Factors in bytes + factors = { + "mega": 1024 ** 2, + "giga": 1024 ** 3, + "tera": 1024 ** 4, + } + if not mb: - return '' - if not mb % 1048576: # 1024^2 - return f'{int(mb / 1048576)} TB' - if not mb % 1024: - return f'{int(mb / 1024)} GB' - return f'{int(mb) / (1048576):.2f} MB' + return "" + + print(factors['mega']) + + bytes = int(mb * 1024**2) + + if bytes >= factors["tera"]: + return f"{bytes / factors["tera"]:.2f} TB" + + if bytes >= factors["giga"]: + return f"{bytes / factors["giga"]:.2f} GB" + + if bytes >= factors["mega"]: + return f"{bytes / factors["mega"]:.2f} MB" @register.filter() diff --git a/netbox/utilities/tests/test_humanize_megabytes_helper.py b/netbox/utilities/tests/test_humanize_megabytes_helper.py new file mode 100644 index 00000000000..be6e2a29d82 --- /dev/null +++ b/netbox/utilities/tests/test_humanize_megabytes_helper.py @@ -0,0 +1,25 @@ +from utilities.templatetags.helpers import humanize_megabytes +from utilities.testing import TestCase + + +class TestConvertByteSize(TestCase): + + def test_humanize_megabytes_converts_megabytes(self): + """Test that humanize_megabytes converts megabytes to the most suitable unit.""" + self.assertEqual(humanize_megabytes(1), "1.00 MB") + + def test_humanize_megabytes_converts_to_gigabytes(self): + """Test that humanize_megabytes converts megabytes to gigabytes.""" + self.assertEqual(humanize_megabytes(1024), "1.00 GB") + + def test_humanize_megabytes_converts_to_terabytes(self): + """Test that humanize_megabytes converts megabytes to terabytes.""" + self.assertEqual(humanize_megabytes(1048576), "1.00 TB") + + def test_humanize_megabytes_returns_empty_for_none(self): + """Test that humanize_megabytes returns empty for None.""" + self.assertEqual(humanize_megabytes(None), '') + + def test_humanize_megabytes_without_unit(self): + """Test that humanize_megabytes returns the value without unit.""" + self.assertEqual(humanize_megabytes(123456789), "117.74 TB") From fab2a612ac47a4beddf7e3c74285a580b631abb8 Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Thu, 23 May 2024 16:08:05 -0300 Subject: [PATCH 09/14] Changed humanize_megabytes --- netbox/utilities/templatetags/helpers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index e2a16d0889f..02ddc8e801d 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -111,13 +111,13 @@ def humanize_megabytes(mb): bytes = int(mb * 1024**2) if bytes >= factors["tera"]: - return f"{bytes / factors["tera"]:.2f} TB" + return f"{bytes / factors['tera']:.2f} TB" if bytes >= factors["giga"]: - return f"{bytes / factors["giga"]:.2f} GB" + return f"{bytes / factors['giga']:.2f} GB" if bytes >= factors["mega"]: - return f"{bytes / factors["mega"]:.2f} MB" + return f"{bytes / factors['mega']:.2f} MB" @register.filter() From ee7ec9bfb3527712971a12a7c1a299c07ebdeef4 Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Thu, 23 May 2024 16:08:35 -0300 Subject: [PATCH 10/14] Changed humanize_megabytes --- netbox/utilities/templatetags/helpers.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 02ddc8e801d..18e2b2070ca 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -106,8 +106,6 @@ def humanize_megabytes(mb): if not mb: return "" - print(factors['mega']) - bytes = int(mb * 1024**2) if bytes >= factors["tera"]: From 3c8b184fdfdc2d461c58d3695e09f9e8630a1e75 Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Thu, 23 May 2024 16:22:01 -0300 Subject: [PATCH 11/14] Added the title to display the value in MB when mouseover. --- netbox/templates/virtualization/cluster.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/templates/virtualization/cluster.html b/netbox/templates/virtualization/cluster.html index 264a275e994..c1ac7460344 100644 --- a/netbox/templates/virtualization/cluster.html +++ b/netbox/templates/virtualization/cluster.html @@ -59,7 +59,7 @@
{% trans "Allocated Resources" %}
{% trans "Memory" %} {% if memory_sum %} - {{ memory_sum|humanize_megabytes }} + {{ memory_sum|humanize_megabytes }} {% else %} {{ ''|placeholder }} {% endif %} From 1d21cd4f8a3bae925858f0b9b3651df6c2101d92 Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Thu, 30 May 2024 14:42:43 -0300 Subject: [PATCH 12/14] Addressed PR comment. --- netbox/utilities/templatetags/helpers.py | 8 ++++---- netbox/utilities/tests/test_humanize_megabytes_helper.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 18e2b2070ca..4f08dc1071b 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -98,15 +98,15 @@ def humanize_megabytes(mb): # Factors in bytes factors = { - "mega": 1024 ** 2, - "giga": 1024 ** 3, - "tera": 1024 ** 4, + "mega": 1000 ** 2, + "giga": 1000 ** 3, + "tera": 1000 ** 4, } if not mb: return "" - bytes = int(mb * 1024**2) + bytes = int(mb * 1000**2) if bytes >= factors["tera"]: return f"{bytes / factors['tera']:.2f} TB" diff --git a/netbox/utilities/tests/test_humanize_megabytes_helper.py b/netbox/utilities/tests/test_humanize_megabytes_helper.py index be6e2a29d82..642e92b9ffe 100644 --- a/netbox/utilities/tests/test_humanize_megabytes_helper.py +++ b/netbox/utilities/tests/test_humanize_megabytes_helper.py @@ -10,11 +10,11 @@ def test_humanize_megabytes_converts_megabytes(self): def test_humanize_megabytes_converts_to_gigabytes(self): """Test that humanize_megabytes converts megabytes to gigabytes.""" - self.assertEqual(humanize_megabytes(1024), "1.00 GB") + self.assertEqual(humanize_megabytes(1000), "1.00 GB") def test_humanize_megabytes_converts_to_terabytes(self): """Test that humanize_megabytes converts megabytes to terabytes.""" - self.assertEqual(humanize_megabytes(1048576), "1.00 TB") + self.assertEqual(humanize_megabytes(1000000), "1.00 TB") def test_humanize_megabytes_returns_empty_for_none(self): """Test that humanize_megabytes returns empty for None.""" @@ -22,4 +22,4 @@ def test_humanize_megabytes_returns_empty_for_none(self): def test_humanize_megabytes_without_unit(self): """Test that humanize_megabytes returns the value without unit.""" - self.assertEqual(humanize_megabytes(123456789), "117.74 TB") + self.assertEqual(humanize_megabytes(123456789), "123.46 TB") From 37d53c99709bc911301ac1177d945c2362697b8c Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Mon, 3 Jun 2024 11:36:52 -0300 Subject: [PATCH 13/14] Addressed PR comment. --- netbox/templates/virtualization/virtualmachine.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/netbox/templates/virtualization/virtualmachine.html b/netbox/templates/virtualization/virtualmachine.html index 38432fdfee4..2d87c6df0f6 100644 --- a/netbox/templates/virtualization/virtualmachine.html +++ b/netbox/templates/virtualization/virtualmachine.html @@ -125,7 +125,7 @@
{% trans "Resources" %}
{% trans "Memory" %} {% if object.memory %} - {{ object.memory|humanize_megabytes }} + {{ object.memory|humanize_megabytes }} {% else %} {{ ''|placeholder }} {% endif %} From bfd921bdbc690fa74eba30cb58b81b8e3f5520c3 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 6 Jun 2024 09:24:29 -0400 Subject: [PATCH 14/14] Rewrite sizing logic --- netbox/templates/virtualization/cluster.html | 2 +- .../virtualization/virtualmachine.html | 2 +- netbox/utilities/templatetags/helpers.py | 37 ++++++------------- .../tests/test_humanize_megabytes_helper.py | 25 ------------- 4 files changed, 14 insertions(+), 52 deletions(-) delete mode 100644 netbox/utilities/tests/test_humanize_megabytes_helper.py diff --git a/netbox/templates/virtualization/cluster.html b/netbox/templates/virtualization/cluster.html index c1ac7460344..a2c3e06c10d 100644 --- a/netbox/templates/virtualization/cluster.html +++ b/netbox/templates/virtualization/cluster.html @@ -59,7 +59,7 @@
{% trans "Allocated Resources" %}
{% trans "Memory" %} {% if memory_sum %} - {{ memory_sum|humanize_megabytes }} + {{ memory_sum|humanize_megabytes }} {% else %} {{ ''|placeholder }} {% endif %} diff --git a/netbox/templates/virtualization/virtualmachine.html b/netbox/templates/virtualization/virtualmachine.html index 2d87c6df0f6..ed8980f5c81 100644 --- a/netbox/templates/virtualization/virtualmachine.html +++ b/netbox/templates/virtualization/virtualmachine.html @@ -125,7 +125,7 @@
{% trans "Resources" %}
{% trans "Memory" %} {% if object.memory %} - {{ object.memory|humanize_megabytes }} + {{ object.memory|humanize_megabytes }} {% else %} {{ ''|placeholder }} {% endif %} diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 4f08dc1071b..b9a3e0005d0 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -1,14 +1,9 @@ -import datetime import json from typing import Dict, Any from urllib.parse import quote from django import template -from django.conf import settings -from django.template.defaultfilters import date from django.urls import NoReverseMatch, reverse -from django.utils import timezone -from django.utils.safestring import mark_safe from core.models import ObjectType from utilities.forms import get_selected_values, TableConfigForm @@ -92,30 +87,22 @@ def humanize_speed(speed): @register.filter() def humanize_megabytes(mb): """ - Express a number of megabytes in the most suitable unit (e.g. gigabytes or terabytes). - It considers the mb value as megabytes and converts it to the most suitable unit. + Express a number of megabytes in the most suitable unit (e.g. gigabytes, terabytes, etc.). """ - - # Factors in bytes - factors = { - "mega": 1000 ** 2, - "giga": 1000 ** 3, - "tera": 1000 ** 4, - } - if not mb: return "" - bytes = int(mb * 1000**2) - - if bytes >= factors["tera"]: - return f"{bytes / factors['tera']:.2f} TB" - - if bytes >= factors["giga"]: - return f"{bytes / factors['giga']:.2f} GB" - - if bytes >= factors["mega"]: - return f"{bytes / factors['mega']:.2f} MB" + PB_SIZE = 1000000000 + TB_SIZE = 1000000 + GB_SIZE = 1000 + + if mb >= PB_SIZE: + return f"{mb / PB_SIZE:.2f} PB" + if mb >= TB_SIZE: + return f"{mb / TB_SIZE:.2f} TB" + if mb >= GB_SIZE: + return f"{mb / GB_SIZE:.2f} GB" + return f"{mb} MB" @register.filter() diff --git a/netbox/utilities/tests/test_humanize_megabytes_helper.py b/netbox/utilities/tests/test_humanize_megabytes_helper.py deleted file mode 100644 index 642e92b9ffe..00000000000 --- a/netbox/utilities/tests/test_humanize_megabytes_helper.py +++ /dev/null @@ -1,25 +0,0 @@ -from utilities.templatetags.helpers import humanize_megabytes -from utilities.testing import TestCase - - -class TestConvertByteSize(TestCase): - - def test_humanize_megabytes_converts_megabytes(self): - """Test that humanize_megabytes converts megabytes to the most suitable unit.""" - self.assertEqual(humanize_megabytes(1), "1.00 MB") - - def test_humanize_megabytes_converts_to_gigabytes(self): - """Test that humanize_megabytes converts megabytes to gigabytes.""" - self.assertEqual(humanize_megabytes(1000), "1.00 GB") - - def test_humanize_megabytes_converts_to_terabytes(self): - """Test that humanize_megabytes converts megabytes to terabytes.""" - self.assertEqual(humanize_megabytes(1000000), "1.00 TB") - - def test_humanize_megabytes_returns_empty_for_none(self): - """Test that humanize_megabytes returns empty for None.""" - self.assertEqual(humanize_megabytes(None), '') - - def test_humanize_megabytes_without_unit(self): - """Test that humanize_megabytes returns the value without unit.""" - self.assertEqual(humanize_megabytes(123456789), "123.46 TB")