Moves to tuple return on utilization; updates template

This commit is contained in:
Josh VanDeraa 2020-11-23 04:08:34 +00:00
parent 26e14bc8e1
commit 0c74a9e82a
5 changed files with 22 additions and 24 deletions

View File

@ -542,9 +542,9 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
available_units.remove(u) available_units.remove(u)
occupied_unit_count = self.u_height - len(available_units) occupied_unit_count = self.u_height - len(available_units)
percentage = int(float(occupied_unit_count) / self.u_height * 100)
return percentage # Return the numerator and denominator as percentage is to be calculated later where needed
return (occupied_unit_count, self.u_height)
def get_power_utilization(self): def get_power_utilization(self):
""" """
@ -562,8 +562,8 @@ class Rack(ChangeLoggedModel, CustomFieldModel):
if power_stats: if power_stats:
allocated_draw_total = sum(x['allocated_draw_total'] or 0 for x in power_stats) allocated_draw_total = sum(x['allocated_draw_total'] or 0 for x in power_stats)
available_power_total = sum(x['available_power'] for x in power_stats) available_power_total = sum(x['available_power'] for x in power_stats)
return int(allocated_draw_total / available_power_total * 100) or 0 return int(allocated_draw_total / available_power_total * 100) or (0, 0)
return 0 return (0, 0)
@extras_features('custom_links', 'export_templates', 'webhooks') @extras_features('custom_links', 'export_templates', 'webhooks')

View File

@ -258,7 +258,7 @@ class Aggregate(ChangeLoggedModel, CustomFieldModel):
""" """
queryset = Prefix.objects.filter(prefix__net_contained_or_equal=str(self.prefix)) queryset = Prefix.objects.filter(prefix__net_contained_or_equal=str(self.prefix))
child_prefixes = netaddr.IPSet([p.prefix for p in queryset]) child_prefixes = netaddr.IPSet([p.prefix for p in queryset])
return int(float(child_prefixes.size) / self.prefix.size * 100) return (child_prefixes.size, self.prefix.size)
class Role(ChangeLoggedModel): class Role(ChangeLoggedModel):
@ -550,8 +550,8 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
def get_utilization(self): def get_utilization(self):
""" """
Determine the utilization of the prefix and return it as a percentage. For Prefixes with a status of Get the child prefix size and parent prefix size return them as a tuple. For Prefixes with a status of
"container", calculate utilization based on child prefixes. For all others, count child IP addresses. "container", get the number child prefixes. For all others, count child IP addresses.
""" """
if self.status == PrefixStatusChoices.STATUS_CONTAINER: if self.status == PrefixStatusChoices.STATUS_CONTAINER:
queryset = Prefix.objects.filter( queryset = Prefix.objects.filter(
@ -559,14 +559,14 @@ class Prefix(ChangeLoggedModel, CustomFieldModel):
vrf=self.vrf vrf=self.vrf
) )
child_prefixes = netaddr.IPSet([p.prefix for p in queryset]) child_prefixes = netaddr.IPSet([p.prefix for p in queryset])
return (int(float(child_prefixes.size) / self.prefix.size * 100), child_prefixes.size, self.prefix.size) return (child_prefixes.size, self.prefix.size)
else: else:
# Compile an IPSet to avoid counting duplicate IPs # Compile an IPSet to avoid counting duplicate IPs
child_count = netaddr.IPSet([ip.address.ip for ip in self.get_child_ips()]).size child_count = netaddr.IPSet([ip.address.ip for ip in self.get_child_ips()]).size
prefix_size = self.prefix.size prefix_size = self.prefix.size
if self.prefix.version == 4 and self.prefix.prefixlen < 31 and not self.is_pool: if self.prefix.version == 4 and self.prefix.prefixlen < 31 and not self.is_pool:
prefix_size -= 2 prefix_size -= 2
return (int(float(child_count) / prefix_size * 100), child_count, prefix_size) return (child_count, prefix_size)
@extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks') @extras_features('custom_fields', 'custom_links', 'export_templates', 'webhooks')

View File

@ -179,8 +179,7 @@ class TestPrefix(TestCase):
Prefix(prefix=netaddr.IPNetwork('10.0.0.0/26')), Prefix(prefix=netaddr.IPNetwork('10.0.0.0/26')),
Prefix(prefix=netaddr.IPNetwork('10.0.0.128/26')), Prefix(prefix=netaddr.IPNetwork('10.0.0.128/26')),
)) ))
self.assertEqual(prefix.get_utilization()[0], 50) self.assertEqual(prefix.get_utilization(), (128, 256))
self.assertEqual(prefix.get_utilization(), (50, 128, 256))
# Non-container Prefix # Non-container Prefix
prefix.status = PrefixStatusChoices.STATUS_ACTIVE prefix.status = PrefixStatusChoices.STATUS_ACTIVE
@ -189,8 +188,7 @@ class TestPrefix(TestCase):
# Create 32 IPAddresses within the Prefix # Create 32 IPAddresses within the Prefix
[IPAddress(address=netaddr.IPNetwork('10.0.0.{}/24'.format(i))) for i in range(1, 33)] [IPAddress(address=netaddr.IPNetwork('10.0.0.{}/24'.format(i))) for i in range(1, 33)]
) )
self.assertEqual(prefix.get_utilization()[0], 12) # ~= 12% self.assertEqual(prefix.get_utilization(), (32, 254))
self.assertEqual(prefix.get_utilization(), (12, 32, 254))
# #
# Uniqueness enforcement tests # Uniqueness enforcement tests

View File

@ -1,4 +1,5 @@
<div title="Used: {{ util_count }}&#013Available: {{ num_available }}" class="progress text-center"> <div {% if utilization_count and num_available %}title="Used: {{ utilization_count }}&#013Available: {{ num_available }}"
{% endif %}class="progress text-center">
{% if utilization < 30 %}<span style="font-size: 12px;">{{ utilization }}%</span>{% endif %} {% if utilization < 30 %}<span style="font-size: 12px;">{{ utilization }}%</span>{% endif %}
<div class="progress-bar progress-bar-{% if utilization >= danger_threshold %}danger{% elif utilization >= warning_threshold %}warning{% else %}success{% endif %}" <div class="progress-bar progress-bar-{% if utilization >= danger_threshold %}danger{% elif utilization >= warning_threshold %}warning{% else %}success{% endif %}"
role="progressbar" aria-valuenow="{{ utilization }}" aria-valuemin="0" aria-valuemax="100" style="width: {% if utilization > 100 %}100{% else %}{{ utilization }}{% endif %}%"> role="progressbar" aria-valuenow="{{ utilization }}" aria-valuemin="0" aria-valuemax="100" style="width: {% if utilization > 100 %}100{% else %}{{ utilization }}{% endif %}%">

View File

@ -230,23 +230,22 @@ def querystring(request, **kwargs):
@register.inclusion_tag('utilities/templatetags/utilization_graph.html') @register.inclusion_tag('utilities/templatetags/utilization_graph.html')
def utilization_graph(utilization, warning_threshold=75, danger_threshold=90): def utilization_graph(data_input, warning_threshold=75, danger_threshold=90):
""" """
Display a horizontal bar graph indicating a percentage of utilization. Display a horizontal bar graph indicating a percentage of utilization.
""" """
if isinstance(utilization, int): # Check for possible division by zero error
return { if data_input[1] == 0:
'utilization': utilization, utilization = 0
'warning_threshold': warning_threshold, else:
'danger_threshold': danger_threshold, utilization = int(float(data_input[0]) / data_input[1] * 100)
}
return { return {
'utilization': utilization[0], 'utilization': utilization,
'warning_threshold': warning_threshold, 'warning_threshold': warning_threshold,
'danger_threshold': danger_threshold, 'danger_threshold': danger_threshold,
'util_count': utilization[1], 'utilization_count': data_input[0],
'num_available': utilization[2], 'num_available': data_input[1],
} }