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

15873 - Make Cluster resource counters more readable #15900

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
2 changes: 1 addition & 1 deletion netbox/templates/virtualization/cluster.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ <h5 class="card-header">{% trans "Allocated Resources" %}</h5>
<th scope="row"><i class="mdi mdi-chip"></i> {% trans "Memory" %}</th>
<td>
{% if memory_sum %}
{{ memory_sum|humanize_megabytes }}
<span title={{ memory_sum }}>{{ memory_sum|humanize_megabytes }}</span>
{% else %}
{{ ''|placeholder }}
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion netbox/templates/virtualization/virtualmachine.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ <h5 class="card-header">{% trans "Resources" %}</h5>
<th scope="row"><i class="mdi mdi-chip"></i> {% trans "Memory" %}</th>
<td>
{% if object.memory %}
{{ object.memory|humanize_megabytes }}
<span title={{ object.memory }}>{{ object.memory|humanize_megabytes }}</span>
{% else %}
{{ ''|placeholder }}
{% endif %}
Expand Down
26 changes: 14 additions & 12 deletions netbox/utilities/templatetags/helpers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
Expand Down