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 tenancy.tables import TenantColumn
from utilities.tables import ( from utilities.tables import (
BaseTable, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, LinkedCountColumn, MPTTColumn, 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__ = ( __all__ = (
'RackTable', 'RackTable',
@ -98,13 +98,10 @@ class RackDetailTable(RackTable):
url_params={'rack_id': 'pk'}, url_params={'rack_id': 'pk'},
verbose_name='Devices' verbose_name='Devices'
) )
get_utilization = tables.TemplateColumn( get_utilization = UtilizationColumn(
template_code=UTILIZATION_GRAPH,
orderable=False,
verbose_name='Space' verbose_name='Space'
) )
get_power_utilization = tables.TemplateColumn( get_power_utilization = UtilizationColumn(
template_code=UTILIZATION_GRAPH,
orderable=False, orderable=False,
verbose_name='Power' verbose_name='Power'
) )

View File

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

View File

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

View File

@ -290,6 +290,9 @@ class TagColumn(tables.TemplateColumn):
class MPTTColumn(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 %}""" \ 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>""" """<a href="{{ record.get_absolute_url }}">{{ record.name }}</a>"""
@ -304,3 +307,16 @@ class MPTTColumn(tables.TemplateColumn):
def value(self, value): def value(self, value):
return 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}%'