Skip to content

Commit 7e1b3d0

Browse files
15873 - Make Cluster resource counters more readable (netbox-community#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 <[email protected]>
1 parent 3acf3b5 commit 7e1b3d0

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

netbox/templates/virtualization/cluster.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ <h5 class="card-header">{% trans "Allocated Resources" %}</h5>
5959
<th scope="row"><i class="mdi mdi-chip"></i> {% trans "Memory" %}</th>
6060
<td>
6161
{% if memory_sum %}
62-
{{ memory_sum|humanize_megabytes }}
62+
<span title={{ memory_sum }}>{{ memory_sum|humanize_megabytes }}</span>
6363
{% else %}
6464
{{ ''|placeholder }}
6565
{% endif %}

netbox/templates/virtualization/virtualmachine.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ <h5 class="card-header">{% trans "Resources" %}</h5>
125125
<th scope="row"><i class="mdi mdi-chip"></i> {% trans "Memory" %}</th>
126126
<td>
127127
{% if object.memory %}
128-
{{ object.memory|humanize_megabytes }}
128+
<span title={{ object.memory }}>{{ object.memory|humanize_megabytes }}</span>
129129
{% else %}
130130
{{ ''|placeholder }}
131131
{% endif %}

netbox/utilities/templatetags/helpers.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import datetime
21
import json
32
from typing import Dict, Any
43
from urllib.parse import quote
54

65
from django import template
7-
from django.conf import settings
8-
from django.template.defaultfilters import date
96
from django.urls import NoReverseMatch, reverse
10-
from django.utils import timezone
11-
from django.utils.safestring import mark_safe
127

138
from core.models import ObjectType
149
from utilities.forms import get_selected_values, TableConfigForm
@@ -92,15 +87,22 @@ def humanize_speed(speed):
9287
@register.filter()
9388
def humanize_megabytes(mb):
9489
"""
95-
Express a number of megabytes in the most suitable unit (e.g. gigabytes or terabytes).
90+
Express a number of megabytes in the most suitable unit (e.g. gigabytes, terabytes, etc.).
9691
"""
9792
if not mb:
98-
return ''
99-
if not mb % 1048576: # 1024^2
100-
return f'{int(mb / 1048576)} TB'
101-
if not mb % 1024:
102-
return f'{int(mb / 1024)} GB'
103-
return f'{mb} MB'
93+
return ""
94+
95+
PB_SIZE = 1000000000
96+
TB_SIZE = 1000000
97+
GB_SIZE = 1000
98+
99+
if mb >= PB_SIZE:
100+
return f"{mb / PB_SIZE:.2f} PB"
101+
if mb >= TB_SIZE:
102+
return f"{mb / TB_SIZE:.2f} TB"
103+
if mb >= GB_SIZE:
104+
return f"{mb / GB_SIZE:.2f} GB"
105+
return f"{mb} MB"
104106

105107

106108
@register.filter()

0 commit comments

Comments
 (0)