change rounding

This commit is contained in:
tranthang2404 2022-03-12 23:17:06 +07:00
parent d434c3d7e5
commit 497c325cc5
3 changed files with 9 additions and 14 deletions

View File

@ -248,9 +248,9 @@ class Aggregate(GetAvailablePrefixesMixin, PrimaryModel):
"""
queryset = Prefix.objects.filter(prefix__net_contained_or_equal=str(self.prefix))
child_prefixes = netaddr.IPSet([p.prefix for p in queryset])
utilization = int(float(child_prefixes.size) / self.prefix.size * 100)
utilization = float(child_prefixes.size) / self.prefix.size * 100
return min(utilization, 100)
return utilization
@extras_features('custom_fields', 'custom_links', 'export_templates', 'tags', 'webhooks')
@ -548,7 +548,7 @@ class Prefix(GetAvailablePrefixesMixin, PrimaryModel):
vrf=self.vrf
)
child_prefixes = netaddr.IPSet([p.prefix for p in queryset])
utilization = int(float(child_prefixes.size) / self.prefix.size * 100)
utilization = float(child_prefixes.size) / self.prefix.size * 100
else:
# Compile an IPSet to avoid counting duplicate IPs
child_ips = netaddr.IPSet(
@ -558,9 +558,9 @@ class Prefix(GetAvailablePrefixesMixin, PrimaryModel):
prefix_size = self.prefix.size
if self.prefix.version == 4 and self.prefix.prefixlen < 31 and not self.is_pool:
prefix_size -= 2
utilization = int(float(child_ips.size) / prefix_size * 100)
utilization = float(child_ips.size) / prefix_size * 100
return min(utilization, 100)
return utilization
@extras_features('custom_fields', 'custom_links', 'export_templates', 'tags', 'webhooks')

View File

@ -9,7 +9,7 @@
aria-valuemin="0"
aria-valuemax="100"
aria-valuenow="{{ utilization }}"
{% if is_full_danger %}
{% if utilization == 100 %}
class="progress-bar"
style="width: {{ utilization }}%; background-color: #800080;"
{% else %}
@ -17,10 +17,10 @@
style="width: {{ utilization }}%;"
{% endif %}
>
{% if utilization >= 25 %}{{ utilization }}%{% endif %}
{% if utilization >= 25 %}{{ utilization|floatformat:-2 }}%{% endif %}
</div>
{% if utilization < 25 %}
<span class="ps-1">{{ utilization }}%</span>
<span class="ps-1">{{ utilization|floatformat:-2 }}%</span>
{% endif %}
</div>
{% endif %}
{% endif %}

View File

@ -389,9 +389,6 @@ def utilization_graph(utilization, warning_threshold=75, danger_threshold=90):
"""
Display a horizontal bar graph indicating a percentage of utilization.
"""
is_full_danger = False
if utilization == 100:
is_full_danger = True
if danger_threshold and utilization >= danger_threshold:
bar_class = 'bg-danger'
elif warning_threshold and utilization >= warning_threshold:
@ -400,11 +397,9 @@ def utilization_graph(utilization, warning_threshold=75, danger_threshold=90):
bar_class = 'bg-success'
else:
bar_class = 'bg-gray'
is_full_danger = False
return {
'utilization': utilization,
'bar_class': bar_class,
'is_full_danger': is_full_danger,
}