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 <jstretch@netboxlabs.com>
This commit is contained in:
Julio Oliveira at Encora 2024-06-06 10:37:29 -03:00 committed by GitHub
parent 3acf3b51ee
commit 7e1b3d0b54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 14 deletions

View File

@ -59,7 +59,7 @@
<th scope="row"><i class="mdi mdi-chip"></i> {% trans "Memory" %}</th> <th scope="row"><i class="mdi mdi-chip"></i> {% trans "Memory" %}</th>
<td> <td>
{% if memory_sum %} {% if memory_sum %}
{{ memory_sum|humanize_megabytes }} <span title={{ memory_sum }}>{{ memory_sum|humanize_megabytes }}</span>
{% else %} {% else %}
{{ ''|placeholder }} {{ ''|placeholder }}
{% endif %} {% endif %}

View File

@ -125,7 +125,7 @@
<th scope="row"><i class="mdi mdi-chip"></i> {% trans "Memory" %}</th> <th scope="row"><i class="mdi mdi-chip"></i> {% trans "Memory" %}</th>
<td> <td>
{% if object.memory %} {% if object.memory %}
{{ object.memory|humanize_megabytes }} <span title={{ object.memory }}>{{ object.memory|humanize_megabytes }}</span>
{% else %} {% else %}
{{ ''|placeholder }} {{ ''|placeholder }}
{% endif %} {% endif %}

View File

@ -1,14 +1,9 @@
import datetime
import json import json
from typing import Dict, Any from typing import Dict, Any
from urllib.parse import quote from urllib.parse import quote
from django import template from django import template
from django.conf import settings
from django.template.defaultfilters import date
from django.urls import NoReverseMatch, reverse from django.urls import NoReverseMatch, reverse
from django.utils import timezone
from django.utils.safestring import mark_safe
from core.models import ObjectType from core.models import ObjectType
from utilities.forms import get_selected_values, TableConfigForm from utilities.forms import get_selected_values, TableConfigForm
@ -92,15 +87,22 @@ def humanize_speed(speed):
@register.filter() @register.filter()
def humanize_megabytes(mb): 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: if not mb:
return '' return ""
if not mb % 1048576: # 1024^2
return f'{int(mb / 1048576)} TB' PB_SIZE = 1000000000
if not mb % 1024: TB_SIZE = 1000000
return f'{int(mb / 1024)} GB' GB_SIZE = 1000
return f'{mb} MB'
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() @register.filter()