From ecf51406c57d27b8bf1ebc290953fb0bb025d47f Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Tue, 11 May 2021 14:52:27 -0400 Subject: [PATCH] Permit disabling utilization graph warning/danger thresholds --- netbox/ipam/tables.py | 17 ++++++- netbox/templates/ipam/prefix.html | 5 +- .../templatetags/utilization_graph.html | 50 +++++-------------- netbox/utilities/templatetags/helpers.py | 11 +++- 4 files changed, 41 insertions(+), 42 deletions(-) diff --git a/netbox/ipam/tables.py b/netbox/ipam/tables.py index aa4119fa7..e0f63a5ca 100644 --- a/netbox/ipam/tables.py +++ b/netbox/ipam/tables.py @@ -256,6 +256,21 @@ class RoleTable(BaseTable): # Prefixes # +class PrefixUtilizationColumn(UtilizationColumn): + """ + Extend UtilizationColumn to allow disabling the warning & danger thresholds for prefixes + marked as fully utilized. + """ + template_code = """ + {% load helpers %} + {% if record.pk and record.mark_utilized %} + {% utilization_graph value warning_threshold=0 danger_threshold=0 %} + {% elif record.pk %} + {% utilization_graph value %} + {% endif %} + """ + + class PrefixTable(BaseTable): pk = ToggleColumn() prefix = tables.TemplateColumn( @@ -300,7 +315,7 @@ class PrefixTable(BaseTable): class PrefixDetailTable(PrefixTable): - utilization = UtilizationColumn( + utilization = PrefixUtilizationColumn( accessor='get_utilization', orderable=False ) diff --git a/netbox/templates/ipam/prefix.html b/netbox/templates/ipam/prefix.html index 3b69f1423..36675aa13 100644 --- a/netbox/templates/ipam/prefix.html +++ b/netbox/templates/ipam/prefix.html @@ -101,8 +101,9 @@ Utilization - {% if object.marked_utilized %} - {% utilization_graph 100 %} + {% if object.mark_utilized %} + {% utilization_graph 100 warning_threshold=0 danger_threshold=0 %} + (Marked fully utilized) {% else %} {% utilization_graph object.get_utilization %} {% endif %} diff --git a/netbox/templates/utilities/templatetags/utilization_graph.html b/netbox/templates/utilities/templatetags/utilization_graph.html index c4a33911f..7f722c50e 100644 --- a/netbox/templates/utilities/templatetags/utilization_graph.html +++ b/netbox/templates/utilities/templatetags/utilization_graph.html @@ -1,42 +1,18 @@ {% if utilization == 0 %} -
+
{{ utilization }}% -
+
{% else %} -
- {% if utilization >= danger_threshold %} -
+
- {{ utilization }}% + {{ utilization }}%
- {% elif utilization >= warning_threshold %} -
- {{ utilization }}% -
- {% else %} -
- {{ utilization }}% -
- {% endif %} -
-{% endif %} \ No newline at end of file +
+{% endif %} diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 6abbec619..78189ec49 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -276,10 +276,17 @@ def utilization_graph(utilization, warning_threshold=75, danger_threshold=90): """ Display a horizontal bar graph indicating a percentage of utilization. """ + if danger_threshold and utilization >= danger_threshold: + bar_class = 'bg-danger' + elif warning_threshold and utilization >= warning_threshold: + bar_class = 'bg-warning' + elif warning_threshold or danger_threshold: + bar_class = 'bg-success' + else: + bar_class = 'bg-default' return { 'utilization': utilization, - 'warning_threshold': warning_threshold, - 'danger_threshold': danger_threshold, + 'bar_class': bar_class, }