Skip to content

Commit

Permalink
Closes netbox-community#17686: Abillity to select disk divider
Browse files Browse the repository at this point in the history
  • Loading branch information
mika.busch committed Nov 13, 2024
1 parent 494d410 commit 09b7715
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 18 deletions.
5 changes: 5 additions & 0 deletions netbox/netbox/configuration_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@
# database access.) Note that the user as which NetBox runs must have read and write permissions to this path.
SESSION_FILE_PATH = None

# By default the memory and disk sizes are displayed using base 10 (e.g. 1000 MB = 1 GB).
# If you would like to use base 2 (e.g. 1024 MB = 1 GB) set this to 1024.
DISK_UNIT_DIVISOR = 1024
RAM_UNIT_DIVISOR = 1024

# By default, uploaded media is stored on the local filesystem. Using Django-storages is also supported. Provide the
# class path of the storage driver in STORAGE_BACKEND and any configuration options in STORAGE_CONFIG. For example:
# STORAGE_BACKEND = 'storages.backends.s3boto3.S3Boto3Storage'
Expand Down
2 changes: 2 additions & 0 deletions netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@
STORAGE_CONFIG = getattr(configuration, 'STORAGE_CONFIG', {})
TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
TRANSLATION_ENABLED = getattr(configuration, 'TRANSLATION_ENABLED', True)
DISK_UNIT_DIVISOR = getattr(configuration, 'DISK_UNIT_DIVISOR', 1000)
RAM_UNIT_DIVISOR = getattr(configuration, 'MEMORY_UNIT_DIVISOR', 1000)

# Load any dynamic configuration parameters which have been hard-coded in the configuration file
for param in CONFIG_PARAMS:
Expand Down
4 changes: 2 additions & 2 deletions netbox/templates/virtualization/cluster.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ <h2 class="card-header">{% trans "Allocated Resources" %}</h2>
<th scope="row"><i class="mdi mdi-chip"></i> {% trans "Memory" %}</th>
<td>
{% if memory_sum %}
<span title={{ memory_sum }}>{{ memory_sum|humanize_megabytes }}</span>
<span title={{ memory_sum }}>{{ memory_sum|humanize_ram_megabytes }}</span>
{% else %}
{{ ''|placeholder }}
{% endif %}
Expand All @@ -69,7 +69,7 @@ <h2 class="card-header">{% trans "Allocated Resources" %}</h2>
<th scope="row"><i class="mdi mdi-harddisk"></i> {% trans "Disk Space" %}</th>
<td>
{% if disk_sum %}
{{ disk_sum|humanize_megabytes }}
{{ disk_sum|humanize_disk_megabytes }}
{% else %}
{{ ''|placeholder }}
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion netbox/templates/virtualization/virtualdisk.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h2 class="card-header">{% trans "Virtual Disk" %}</h2>
<th scope="row"><i class="mdi mdi-harddisk"></i> {% trans "Size" %}</th>
<td>
{% if object.size %}
{{ object.size|humanize_megabytes }}
{{ object.size|humanize_disk_megabytes }}
{% else %}
{{ ''|placeholder }}
{% endif %}
Expand Down
4 changes: 2 additions & 2 deletions netbox/templates/virtualization/virtualmachine.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ <h2 class="card-header">{% trans "Resources" %}</h2>
<th scope="row"><i class="mdi mdi-chip"></i> {% trans "Memory" %}</th>
<td>
{% if object.memory %}
<span title={{ object.memory }}>{{ object.memory|humanize_megabytes }}</span>
<span title={{ object.memory }}>{{ object.memory|humanize_ram_megabytes }}</span>
{% else %}
{{ ''|placeholder }}
{% endif %}
Expand All @@ -141,7 +141,7 @@ <h2 class="card-header">{% trans "Resources" %}</h2>
</th>
<td>
{% if object.disk %}
{{ object.disk|humanize_megabytes }}
{{ object.disk|humanize_disk_megabytes }}
{% else %}
{{ ''|placeholder }}
{% endif %}
Expand Down
30 changes: 23 additions & 7 deletions netbox/utilities/templatetags/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
from core.models import ObjectType
from utilities.forms import get_selected_values, TableConfigForm
from utilities.views import get_viewname
from netbox.settings import DISK_UNIT_DIVISOR, RAM_UNIT_DIVISOR

__all__ = (
'applied_filters',
'as_range',
'divide',
'get_item',
'get_key',
'humanize_megabytes',
'humanize_disk_megabytes',
'humanize_ram_megabytes',
'humanize_speed',
'icon_from_status',
'kg_to_pounds',
Expand Down Expand Up @@ -83,18 +85,16 @@ def humanize_speed(speed):
else:
return '{} Kbps'.format(speed)


@register.filter()
def humanize_megabytes(mb):
def _humanize_megabytes(mb, divisor=1000):
"""
Express a number of megabytes in the most suitable unit (e.g. gigabytes, terabytes, etc.).
"""
if not mb:
return ""

PB_SIZE = 1000000000
TB_SIZE = 1000000
GB_SIZE = 1000
PB_SIZE = divisor**3
TB_SIZE = divisor**2
GB_SIZE = divisor

if mb >= PB_SIZE:
return f"{mb / PB_SIZE:.2f} PB"
Expand All @@ -104,6 +104,22 @@ def humanize_megabytes(mb):
return f"{mb / GB_SIZE:.2f} GB"
return f"{mb} MB"

@register.filter()
def humanize_disk_megabytes(mb):
"""
Express a number of megabytes in the most suitable unit (e.g. gigabytes, terabytes, etc.).
Use the DISK_UNIT_DIVISOR setting to determine the divisor. Default is 1000.
"""
return _humanize_megabytes(mb, DISK_UNIT_DIVISOR)

@register.filter()
def humanize_ram_megabytes(mb):
"""
Express a number of megabytes in the most suitable unit (e.g. gigabytes, terabytes, etc.).
Use the RAM_UNIT_DIVISOR setting to determine the divisor. Default is 1000.
"""
return _humanize_megabytes(mb, RAM_UNIT_DIVISOR)


@register.filter()
def divide(x, y):
Expand Down
6 changes: 3 additions & 3 deletions netbox/virtualization/migrations/0040_convert_disk_size.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.db import migrations
from django.db.models import F, Sum

from netbox.settings import DISK_UNIT_DIVISOR

def convert_disk_size(apps, schema_editor):
VirtualMachine = apps.get_model('virtualization', 'VirtualMachine')
VirtualMachine.objects.filter(disk__isnull=False).update(disk=F('disk') * 1000)
VirtualMachine.objects.filter(disk__isnull=False).update(disk=F('disk') * DISK_UNIT_DIVISOR)

VirtualDisk = apps.get_model('virtualization', 'VirtualDisk')
VirtualDisk.objects.filter(size__isnull=False).update(size=F('size') * 1000)
VirtualDisk.objects.filter(size__isnull=False).update(size=F('size') * DISK_UNIT_DIVISOR)

# Recalculate disk size on all VMs with virtual disks
id_list = VirtualDisk.objects.values_list('virtual_machine_id').distinct()
Expand Down
6 changes: 3 additions & 3 deletions netbox/virtualization/tables/virtualmachines.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dcim.tables.devices import BaseInterfaceTable
from netbox.tables import NetBoxTable, columns
from tenancy.tables import ContactsColumnMixin, TenancyColumnsMixin
from utilities.templatetags.helpers import humanize_megabytes
from utilities.templatetags.helpers import humanize_disk_megabytes
from virtualization.models import VirtualDisk, VirtualMachine, VMInterface

__all__ = (
Expand Down Expand Up @@ -123,7 +123,7 @@ class Meta(NetBoxTable.Meta):
)

def render_disk(self, value):
return humanize_megabytes(value)
return humanize_disk_megabytes(value)


#
Expand Down Expand Up @@ -212,7 +212,7 @@ class Meta(NetBoxTable.Meta):
}

def render_size(self, value):
return humanize_megabytes(value)
return humanize_disk_megabytes(value)


class VirtualMachineVirtualDiskTable(VirtualDiskTable):
Expand Down

0 comments on commit 09b7715

Please sign in to comment.