mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-13 02:58:17 -06:00
Fixed
This commit is contained in:
parent
1ad63761ea
commit
872f1e9800
@ -24,22 +24,3 @@ def register_data_backend():
|
|||||||
return cls
|
return cls
|
||||||
|
|
||||||
return _wrapper
|
return _wrapper
|
||||||
|
|
||||||
|
|
||||||
def convert_byte_size(value, unit="mega"):
|
|
||||||
"""
|
|
||||||
Convert a size value to unit.
|
|
||||||
"""
|
|
||||||
factors = {
|
|
||||||
"kilo": 1024,
|
|
||||||
"mega": 1024 ** 2,
|
|
||||||
"giga": 1024 ** 3,
|
|
||||||
"tera": 1024 ** 4,
|
|
||||||
}
|
|
||||||
if value:
|
|
||||||
# If the value is less than 6 digits, it understands the value is expressed according to the unit.
|
|
||||||
if len(str(value)) < 6:
|
|
||||||
return value
|
|
||||||
value_converted = float(value) / factors[unit]
|
|
||||||
return value_converted
|
|
||||||
return 0
|
|
||||||
|
@ -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 }} {% trans "MB" context "Abbreviation for megabyte" %}
|
{{ memory_sum|humanize_megabytes }}
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ ''|placeholder }}
|
{{ ''|placeholder }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
@ -96,11 +96,13 @@ def humanize_megabytes(mb):
|
|||||||
"""
|
"""
|
||||||
if not mb:
|
if not mb:
|
||||||
return ''
|
return ''
|
||||||
|
if len(str(mb)) < 6:
|
||||||
|
return mb
|
||||||
if not mb % 1048576: # 1024^2
|
if not mb % 1048576: # 1024^2
|
||||||
return f'{int(mb / 1048576)} TB'
|
return f'{int(mb / 1048576)} TB'
|
||||||
if not mb % 1024:
|
if not mb % 1024:
|
||||||
return f'{int(mb / 1024)} GB'
|
return f'{int(mb / 1024)} GB'
|
||||||
return f'{mb} MB'
|
return f'{float(mb)/(1048576):.2f} MB'
|
||||||
|
|
||||||
|
|
||||||
@register.filter()
|
@register.filter()
|
||||||
|
@ -16,7 +16,6 @@ from extras.views import ObjectConfigContextView
|
|||||||
from ipam.models import IPAddress
|
from ipam.models import IPAddress
|
||||||
from ipam.tables import InterfaceVLANTable
|
from ipam.tables import InterfaceVLANTable
|
||||||
from netbox.constants import DEFAULT_ACTION_PERMISSIONS
|
from netbox.constants import DEFAULT_ACTION_PERMISSIONS
|
||||||
from netbox.utils import convert_byte_size
|
|
||||||
from netbox.views import generic
|
from netbox.views import generic
|
||||||
from tenancy.views import ObjectContactsView
|
from tenancy.views import ObjectContactsView
|
||||||
from utilities.query import count_related
|
from utilities.query import count_related
|
||||||
@ -173,17 +172,7 @@ class ClusterView(generic.ObjectView):
|
|||||||
queryset = Cluster.objects.all()
|
queryset = Cluster.objects.all()
|
||||||
|
|
||||||
def get_extra_context(self, request, instance):
|
def get_extra_context(self, request, instance):
|
||||||
vm_memory = [convert_byte_size(item.memory, 'mega') for item in instance.virtual_machines.all() if item.memory]
|
return instance.virtual_machines.aggregate(vcpus_sum=Sum('vcpus'), memory_sum=Sum('memory'), disk_sum=Sum('disk'))
|
||||||
vm_disk = [convert_byte_size(item.disk, 'giga') for item in instance.virtual_machines.all() if item.disk]
|
|
||||||
|
|
||||||
memory_sum = sum(vm_memory)
|
|
||||||
disk_sum = sum(vm_disk)
|
|
||||||
|
|
||||||
extra_content = instance.virtual_machines.aggregate(vcpus_sum=Sum('vcpus'))
|
|
||||||
extra_content['memory_sum'] = f"{memory_sum:.2f}"
|
|
||||||
extra_content['disk_sum'] = f"{disk_sum:.2f}"
|
|
||||||
|
|
||||||
return extra_content
|
|
||||||
|
|
||||||
|
|
||||||
@register_model_view(Cluster, 'virtualmachines', path='virtual-machines')
|
@register_model_view(Cluster, 'virtualmachines', path='virtual-machines')
|
||||||
|
Loading…
Reference in New Issue
Block a user