Rewrite sizing logic

This commit is contained in:
Jeremy Stretch 2024-06-06 09:24:29 -04:00
parent 37d53c9970
commit bfd921bdbc
4 changed files with 13 additions and 51 deletions

View File

@ -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,30 +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).
It considers the mb value as megabytes and converts it to the most suitable unit.
Express a number of megabytes in the most suitable unit (e.g. gigabytes, terabytes, etc.).
"""
# Factors in bytes
factors = {
"mega": 1000 ** 2,
"giga": 1000 ** 3,
"tera": 1000 ** 4,
}
if not mb:
return ""
bytes = int(mb * 1000**2)
PB_SIZE = 1000000000
TB_SIZE = 1000000
GB_SIZE = 1000
if bytes >= factors["tera"]:
return f"{bytes / factors['tera']:.2f} TB"
if bytes >= factors["giga"]:
return f"{bytes / factors['giga']:.2f} GB"
if bytes >= factors["mega"]:
return f"{bytes / factors['mega']:.2f} 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()

View File

@ -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")