Parcourir la source

Merge pull request #346 from bellwood/patch-1

properly support #304
Jeremy Stretch il y a 8 ans
Parent
commit
65fb10059a
3 fichiers modifiés avec 25 ajouts et 2 suppressions
  1. 9 0
      netbox/dcim/models.py
  2. 14 0
      netbox/dcim/tables.py
  3. 2 2
      netbox/dcim/views.py

+ 9 - 0
netbox/dcim/models.py

@@ -360,6 +360,15 @@ class Rack(CreatedUpdatedModel):
     def get_0u_devices(self):
         return self.devices.filter(position=0)
 
+    def get_utilization(self):
+        """
+        Determine the utilization rate of the rack and return it as a percentage.
+        """
+        if self.u_consumed is None:
+                self.u_consumed = 0
+        u_available = self.u_height - self.u_consumed
+        return int(float(self.u_height - u_available) / self.u_height * 100)
+
 
 #
 # Device Types

+ 14 - 0
netbox/dcim/tables.py

@@ -48,6 +48,18 @@ STATUS_ICON = """
 {% endif %}
 """
 
+UTILIZATION_GRAPH = """
+{% with record.get_utilization as percentage %}
+<div class="progress text-center">
+    {% if percentage < 15 %}<span style="font-size: 12px;">{{ percentage }}%</span>{% endif %}
+    <div class="progress-bar progress-bar-{% if percentage >= 90 %}danger{% elif percentage >= 75 %}warning{% else %}success{% endif %}"
+        role="progressbar" aria-valuenow="{{ percentage }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ percentage }}%">
+        {% if percentage >= 15 %}{{ percentage }}%{% endif %}
+    </div>
+</div>
+{% endwith %}
+"""
+
 
 #
 # Sites
@@ -97,6 +109,8 @@ class RackTable(BaseTable):
     group = tables.Column(accessor=Accessor('group.name'), verbose_name='Group')
     facility_id = tables.Column(verbose_name='Facility ID')
     u_height = tables.Column(verbose_name='Height (U)')
+    u_consumed = tables.Column(accessor=Accessor('u_consumed'), verbose_name='Used (U)')
+    utilization = tables.TemplateColumn(UTILIZATION_GRAPH, orderable=False, verbose_name='Utilization')
     devices = tables.Column(accessor=Accessor('device_count'), verbose_name='Devices')
 
     class Meta(BaseTable.Meta):

+ 2 - 2
netbox/dcim/views.py

@@ -7,7 +7,7 @@ from django.contrib.auth.decorators import permission_required
 from django.contrib.auth.mixins import PermissionRequiredMixin
 from django.core.exceptions import ValidationError
 from django.core.urlresolvers import reverse
-from django.db.models import Count, ProtectedError
+from django.db.models import Count, ProtectedError, Sum
 from django.forms import ModelMultipleChoiceField, MultipleHiddenInput
 from django.http import HttpResponseRedirect
 from django.shortcuts import get_object_or_404, redirect, render
@@ -144,7 +144,7 @@ class RackGroupBulkDeleteView(PermissionRequiredMixin, BulkDeleteView):
 #
 
 class RackListView(ObjectListView):
-    queryset = Rack.objects.select_related('site', 'group').annotate(device_count=Count('devices', distinct=True))
+    queryset = Rack.objects.select_related('site').prefetch_related('devices__device_type').annotate(device_count=Count('devices', distinct=True), u_consumed=Sum('devices__device_type__u_height'))
     filter = filters.RackFilter
     filter_form = forms.RackFilterForm
     table = tables.RackTable