diff --git a/netbox/dcim/models.py b/netbox/dcim/models.py index 64028dab3..07e25a9f3 100644 --- a/netbox/dcim/models.py +++ b/netbox/dcim/models.py @@ -360,6 +360,20 @@ class Rack(CreatedUpdatedModel): def get_0u_devices(self): return self.devices.filter(position=0) + def get_used_units(self): + """ + Determine how many rack units are used considering both faces of the rack. + """ + used_units = int(len(self.get_available_units(u_height=1, rack_face=None, exclude=list()))) + return int(self.u_height - used_units) + + def get_utilization(self): + """ + Determine the utilization rate of the rack and return it as a percentage. + """ + num_available_units = len(self.get_available_units(u_height=1, rack_face=None, exclude=list())) + return int(float(self.u_height - num_available_units) / self.u_height * 100) + # # Device Types diff --git a/netbox/dcim/tables.py b/netbox/dcim/tables.py index dc66c3ab1..69c350e35 100644 --- a/netbox/dcim/tables.py +++ b/netbox/dcim/tables.py @@ -48,6 +48,18 @@ STATUS_ICON = """ {% endif %} """ +UTILIZATION_GRAPH = """ +{% with record.get_utilization as percentage %} +
+ {% if percentage < 15 %}{{ percentage }}%{% endif %} +
+ {% if percentage >= 15 %}{{ percentage }}%{% endif %} +
+
+{% endwith %} +""" + # # Sites @@ -98,6 +110,8 @@ class RackTable(BaseTable): facility_id = tables.Column(verbose_name='Facility ID') u_height = tables.Column(verbose_name='Height (U)') devices = tables.Column(accessor=Accessor('device_count'), verbose_name='Devices') + u_used = tables.Column(accessor=Accessor('get_used_units'), verbose_name='Space Used') + utilization = tables.TemplateColumn(UTILIZATION_GRAPH, orderable=False, verbose_name='Utilization') class Meta(BaseTable.Meta): model = Rack