Skip to content

Commit

Permalink
Merge pull request netbox-community#346 from bellwood/patch-1
Browse files Browse the repository at this point in the history
properly support netbox-community#304
  • Loading branch information
jeremystretch authored Jul 20, 2016
2 parents 3115d02 + 9e2c8a4 commit b124569
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
9 changes: 9 additions & 0 deletions netbox/dcim/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,15 @@ def get_available_units(self, u_height=1, rack_face=None, exclude=list()):
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
Expand Down
14 changes: 14 additions & 0 deletions netbox/dcim/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@
{% 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
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions netbox/dcim/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b124569

Please sign in to comment.