mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-13 02:58:17 -06:00
Created "convert_byte_size" method to convert the memory and disk size according to unit informed.
Changed "get_extra_context" method from "ClusterView" to use the method above and convert all the disks and memories from VMs to normalize the units.
This commit is contained in:
parent
5af3c659a5
commit
1ba2dd04fa
@ -24,3 +24,21 @@ 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 len(str(value)) < 6:
|
||||||
|
return value
|
||||||
|
value_converted = float(value) / factors[unit]
|
||||||
|
return value_converted
|
||||||
|
return 0
|
||||||
|
@ -59,9 +59,9 @@
|
|||||||
<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|humanize_megabytes }}
|
{{ memory_sum }} {% trans "MB" context "Abbreviation for megabyte" %}
|
||||||
{% else %}
|
{% else %}
|
||||||
{{ ''|placeholder }}
|
{{ ''|placeholder }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
@ -16,6 +16,7 @@ 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
|
||||||
@ -172,7 +173,17 @@ 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):
|
||||||
return instance.virtual_machines.aggregate(vcpus_sum=Sum('vcpus'), memory_sum=Sum('memory'), disk_sum=Sum('disk'))
|
vm_memory = [convert_byte_size(item.memory, 'mega') for item in instance.virtual_machines.all() if item.memory]
|
||||||
|
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:.0f}"
|
||||||
|
extra_content['disk_sum'] = f"{disk_sum:.0f}"
|
||||||
|
|
||||||
|
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