mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-09 09:08:15 -06:00
Rewrite sizing logic
This commit is contained in:
parent
37d53c9970
commit
bfd921bdbc
@ -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 %}
|
||||||
<span title={{ memory_sum }}>{{ memory_sum|humanize_megabytes }}</span>
|
<span title={{ memory_sum }}>{{ memory_sum|humanize_megabytes }}</span>
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ ''|placeholder }}
|
{{ ''|placeholder }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -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 %}
|
||||||
<span title={{ object.memory }}> {{ object.memory|humanize_megabytes }}</span>
|
<span title={{ object.memory }}>{{ object.memory|humanize_megabytes }}</span>
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ ''|placeholder }}
|
{{ ''|placeholder }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -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,30 +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.).
|
||||||
It considers the mb value as megabytes and converts it to the most suitable unit.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Factors in bytes
|
|
||||||
factors = {
|
|
||||||
"mega": 1000 ** 2,
|
|
||||||
"giga": 1000 ** 3,
|
|
||||||
"tera": 1000 ** 4,
|
|
||||||
}
|
|
||||||
|
|
||||||
if not mb:
|
if not mb:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
bytes = int(mb * 1000**2)
|
PB_SIZE = 1000000000
|
||||||
|
TB_SIZE = 1000000
|
||||||
|
GB_SIZE = 1000
|
||||||
|
|
||||||
if bytes >= factors["tera"]:
|
if mb >= PB_SIZE:
|
||||||
return f"{bytes / factors['tera']:.2f} TB"
|
return f"{mb / PB_SIZE:.2f} PB"
|
||||||
|
if mb >= TB_SIZE:
|
||||||
if bytes >= factors["giga"]:
|
return f"{mb / TB_SIZE:.2f} TB"
|
||||||
return f"{bytes / factors['giga']:.2f} GB"
|
if mb >= GB_SIZE:
|
||||||
|
return f"{mb / GB_SIZE:.2f} GB"
|
||||||
if bytes >= factors["mega"]:
|
return f"{mb} MB"
|
||||||
return f"{bytes / factors['mega']:.2f} MB"
|
|
||||||
|
|
||||||
|
|
||||||
@register.filter()
|
@register.filter()
|
||||||
|
@ -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")
|
|
Loading…
Reference in New Issue
Block a user