From 7e1b3d0b54d2cff3be2d63896845d527e9dd811b Mon Sep 17 00:00:00 2001 From: Julio Oliveira at Encora <149191228+Julio-Oliveira-Encora@users.noreply.github.com> Date: Thu, 6 Jun 2024 10:37:29 -0300 Subject: [PATCH] 15873 - Make Cluster resource counters more readable (#15900) * 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. * Changed decimal size for memory_sum and disk_sum * Added test for convert_byte_size. * Fixed * Addressed PR comments. Changed humanize_megabytes in helpers.py * Addressed PR comments. Changed humanize_megabytes in helpers.py * Linter issues for helpers.py * Changed humanize_megabytes * Changed humanize_megabytes * Changed humanize_megabytes * Added the title to display the value in MB when mouseover. * Addressed PR comment. * Addressed PR comment. * Rewrite sizing logic --------- Co-authored-by: Jeremy Stretch --- netbox/templates/virtualization/cluster.html | 2 +- .../virtualization/virtualmachine.html | 2 +- netbox/utilities/templatetags/helpers.py | 26 ++++++++++--------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/netbox/templates/virtualization/cluster.html b/netbox/templates/virtualization/cluster.html index 264a275e9..a2c3e06c1 100644 --- a/netbox/templates/virtualization/cluster.html +++ b/netbox/templates/virtualization/cluster.html @@ -59,7 +59,7 @@ {% 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 38432fdfe..ed8980f5c 100644 --- a/netbox/templates/virtualization/virtualmachine.html +++ b/netbox/templates/virtualization/virtualmachine.html @@ -125,7 +125,7 @@ {% 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 e8686f8c6..b9a3e0005 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,15 +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). + Express a number of megabytes in the most suitable unit (e.g. gigabytes, terabytes, etc.). """ 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'{mb} MB' + return "" + + 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()