Browse Source

Permit disabling utilization graph warning/danger thresholds

jeremystretch 4 years ago
parent
commit
ecf51406c5

+ 16 - 1
netbox/ipam/tables.py

@@ -256,6 +256,21 @@ class RoleTable(BaseTable):
 # Prefixes
 # 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):
 class PrefixTable(BaseTable):
     pk = ToggleColumn()
     pk = ToggleColumn()
     prefix = tables.TemplateColumn(
     prefix = tables.TemplateColumn(
@@ -300,7 +315,7 @@ class PrefixTable(BaseTable):
 
 
 
 
 class PrefixDetailTable(PrefixTable):
 class PrefixDetailTable(PrefixTable):
-    utilization = UtilizationColumn(
+    utilization = PrefixUtilizationColumn(
         accessor='get_utilization',
         accessor='get_utilization',
         orderable=False
         orderable=False
     )
     )

+ 3 - 2
netbox/templates/ipam/prefix.html

@@ -101,8 +101,9 @@
                 <tr>
                 <tr>
                     <th scope="row">Utilization</th>
                     <th scope="row">Utilization</th>
                     <td>
                     <td>
-                      {% if object.marked_utilized %}
-                        {% utilization_graph 100 %}
+                      {% if object.mark_utilized %}
+                        {% utilization_graph 100 warning_threshold=0 danger_threshold=0 %}
+                        <small>(Marked fully utilized)</small>
                       {% else %}
                       {% else %}
                         {% utilization_graph object.get_utilization %}
                         {% utilization_graph object.get_utilization %}
                       {% endif %}
                       {% endif %}

+ 13 - 37
netbox/templates/utilities/templatetags/utilization_graph.html

@@ -1,42 +1,18 @@
 {% if utilization == 0 %}
 {% if utilization == 0 %}
-<div class="progress align-items-center justify-content-center">
+  <div class="progress align-items-center justify-content-center">
     <span class="w-100 text-center">{{ utilization }}%</span>
     <span class="w-100 text-center">{{ utilization }}%</span>
-</div>
+  </div>
 {% else %}
 {% else %}
-<div class="progress">
-    {% if utilization >= danger_threshold %}
-    <div 
-        aria-valuemin="0"
-        role="progressbar"
-        aria-valuemax="100"
-        class="progress-bar bg-danger"
-        aria-valuenow="{{ utilization }}"
-        style="width: {{ utilization }}%;"
+  <div class="progress">
+    <div
+      role="progressbar"
+      aria-valuemin="0"
+      aria-valuemax="100"
+      aria-valuenow="{{ utilization }}"
+      class="progress-bar {{ bar_class }}"
+      style="min-width: 8%; width: {{ utilization }}%;"
     >
     >
-        {{ utilization }}%
+      {{ utilization }}%
     </div>
     </div>
-    {% elif utilization >= warning_threshold %}
-    <div 
-        aria-valuemin="0"
-        role="progressbar"
-        aria-valuemax="100"
-        aria-valuenow="{{ utilization }}"
-        style="width: {{ utilization }}%;"
-        class="progress-bar bg-warning"
-    >
-        {{ utilization }}%
-    </div>
-    {% else %}
-    <div 
-        aria-valuemin="0"
-        role="progressbar"
-        aria-valuemax="100"
-        class="progress-bar bg-success"
-        aria-valuenow="{{ utilization }}"
-        style="min-width: 8%;width: {{ utilization }}%;"
-    >
-        {{ utilization }}%
-    </div>
-    {% endif %}
-</div>
-{% endif %}
+  </div>
+{% endif %}

+ 9 - 2
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.
     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 {
     return {
         'utilization': utilization,
         'utilization': utilization,
-        'warning_threshold': warning_threshold,
-        'danger_threshold': danger_threshold,
+        'bar_class': bar_class,
     }
     }