Introduce UtilizationColumn to render utilization graphs consistently

This commit is contained in:
Jeremy Stretch 2021-03-04 20:58:43 -05:00
parent 32501c96e5
commit e703d9ff78
4 changed files with 25 additions and 21 deletions

View File

@ -5,9 +5,9 @@ from dcim.models import Rack, Location, RackReservation, RackRole
from tenancy.tables import TenantColumn
from utilities.tables import (
BaseTable, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, LinkedCountColumn, MPTTColumn,
TagColumn, ToggleColumn,
TagColumn, ToggleColumn, UtilizationColumn,
)
from .template_code import LOCATION_ELEVATIONS, UTILIZATION_GRAPH
from .template_code import LOCATION_ELEVATIONS
__all__ = (
'RackTable',
@ -98,13 +98,10 @@ class RackDetailTable(RackTable):
url_params={'rack_id': 'pk'},
verbose_name='Devices'
)
get_utilization = tables.TemplateColumn(
template_code=UTILIZATION_GRAPH,
orderable=False,
get_utilization = UtilizationColumn(
verbose_name='Space'
)
get_power_utilization = tables.TemplateColumn(
template_code=UTILIZATION_GRAPH,
get_power_utilization = UtilizationColumn(
orderable=False,
verbose_name='Power'
)

View File

@ -75,11 +75,6 @@ LOCATION_ELEVATIONS = """
</a>
"""
UTILIZATION_GRAPH = """
{% load helpers %}
{% utilization_graph value %}
"""
#
# Device component buttons
#

View File

@ -6,17 +6,13 @@ from dcim.models import Interface
from tenancy.tables import TenantColumn
from utilities.tables import (
BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, LinkedCountColumn, TagColumn, ToggleColumn,
UtilizationColumn,
)
from virtualization.models import VMInterface
from .models import Aggregate, IPAddress, Prefix, RIR, Role, RouteTarget, Service, VLAN, VLANGroup, VRF
AVAILABLE_LABEL = mark_safe('<span class="label label-success">Available</span>')
UTILIZATION_GRAPH = """
{% load helpers %}
{% if record.pk %}{% utilization_graph record.get_utilization %}{% else %}&mdash;{% endif %}
"""
PREFIX_LINK = """
{% load helpers %}
{% for i in record.parents|as_range %}
@ -209,8 +205,8 @@ class AggregateDetailTable(AggregateTable):
child_count = tables.Column(
verbose_name='Prefixes'
)
utilization = tables.TemplateColumn(
template_code=UTILIZATION_GRAPH,
utilization = UtilizationColumn(
accessor='get_utilization',
orderable=False
)
tags = TagColumn(
@ -290,8 +286,8 @@ class PrefixTable(BaseTable):
class PrefixDetailTable(PrefixTable):
utilization = tables.TemplateColumn(
template_code=UTILIZATION_GRAPH,
utilization = UtilizationColumn(
accessor='get_utilization',
orderable=False
)
tenant = TenantColumn()

View File

@ -290,6 +290,9 @@ class TagColumn(tables.TemplateColumn):
class MPTTColumn(tables.TemplateColumn):
"""
Display a nested hierarchy for MPTT-enabled models.
"""
template_code = """{% for i in record.get_ancestors %}<i class="mdi mdi-circle-small"></i>{% endfor %}""" \
"""<a href="{{ record.get_absolute_url }}">{{ record.name }}</a>"""
@ -304,3 +307,16 @@ class MPTTColumn(tables.TemplateColumn):
def value(self, value):
return value
class UtilizationColumn(tables.TemplateColumn):
"""
Display a colored utilization bar graph.
"""
template_code = """{% load helpers %}{% if record.pk %}{% utilization_graph value %}{% endif %}"""
def __init__(self, *args, **kwargs):
super().__init__(template_code=self.template_code, *args, **kwargs)
def value(self, value):
return f'{value}%'