Skip to content

Commit

Permalink
Fixes #2257: Corrected casting of RIR utilization stats as floats
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Jul 23, 2018
1 parent d04727f commit ba3ae0d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
40 changes: 29 additions & 11 deletions netbox/ipam/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,35 @@ class Meta(BaseTable.Meta):


class RIRDetailTable(RIRTable):
stats_total = tables.Column(accessor='stats.total', verbose_name='Total',
footer=lambda table: sum(r.stats['total'] for r in table.data))
stats_active = tables.Column(accessor='stats.active', verbose_name='Active',
footer=lambda table: sum(r.stats['active'] for r in table.data))
stats_reserved = tables.Column(accessor='stats.reserved', verbose_name='Reserved',
footer=lambda table: sum(r.stats['reserved'] for r in table.data))
stats_deprecated = tables.Column(accessor='stats.deprecated', verbose_name='Deprecated',
footer=lambda table: sum(r.stats['deprecated'] for r in table.data))
stats_available = tables.Column(accessor='stats.available', verbose_name='Available',
footer=lambda table: sum(r.stats['available'] for r in table.data))
utilization = tables.TemplateColumn(template_code=RIR_UTILIZATION, verbose_name='Utilization')
stats_total = tables.Column(
accessor='stats.total',
verbose_name='Total',
footer=lambda table: sum(r.stats['total'] for r in table.data)
)
stats_active = tables.Column(
accessor='stats.active',
verbose_name='Active',
footer=lambda table: sum(r.stats['active'] for r in table.data)
)
stats_reserved = tables.Column(
accessor='stats.reserved',
verbose_name='Reserved',
footer=lambda table: sum(r.stats['reserved'] for r in table.data)
)
stats_deprecated = tables.Column(
accessor='stats.deprecated',
verbose_name='Deprecated',
footer=lambda table: sum(r.stats['deprecated'] for r in table.data)
)
stats_available = tables.Column(
accessor='stats.available',
verbose_name='Available',
footer=lambda table: sum(r.stats['available'] for r in table.data)
)
utilization = tables.TemplateColumn(
template_code=RIR_UTILIZATION,
verbose_name='Utilization'
)

class Meta(RIRTable.Meta):
fields = (
Expand Down
36 changes: 14 additions & 22 deletions netbox/ipam/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,15 @@ def alter_queryset(self, request):
queryset = Prefix.objects.filter(prefix__net_contained_or_equal=str(aggregate.prefix))

# Find all consumed space for each prefix status (we ignore containers for this purpose).
active_prefixes = netaddr.cidr_merge([p.prefix for p in queryset.filter(status=PREFIX_STATUS_ACTIVE)])
reserved_prefixes = netaddr.cidr_merge([p.prefix for p in queryset.filter(status=PREFIX_STATUS_RESERVED)])
deprecated_prefixes = netaddr.cidr_merge([p.prefix for p in queryset.filter(status=PREFIX_STATUS_DEPRECATED)])
active_prefixes = netaddr.cidr_merge(
[p.prefix for p in queryset.filter(status=PREFIX_STATUS_ACTIVE)]
)
reserved_prefixes = netaddr.cidr_merge(
[p.prefix for p in queryset.filter(status=PREFIX_STATUS_RESERVED)]
)
deprecated_prefixes = netaddr.cidr_merge(
[p.prefix for p in queryset.filter(status=PREFIX_STATUS_DEPRECATED)]
)

# Find all available prefixes by subtracting each of the existing prefix sets from the aggregate prefix.
available_prefixes = (
Expand All @@ -205,11 +211,11 @@ def alter_queryset(self, request):
)

# Add the size of each metric to the RIR total.
stats['total'] += aggregate.prefix.size / denominator
stats['active'] += netaddr.IPSet(active_prefixes).size / denominator
stats['reserved'] += netaddr.IPSet(reserved_prefixes).size / denominator
stats['deprecated'] += netaddr.IPSet(deprecated_prefixes).size / denominator
stats['available'] += available_prefixes.size / denominator
stats['total'] += int(aggregate.prefix.size / denominator)
stats['active'] += int(netaddr.IPSet(active_prefixes).size / denominator)
stats['reserved'] += int(netaddr.IPSet(reserved_prefixes).size / denominator)
stats['deprecated'] += int(netaddr.IPSet(deprecated_prefixes).size / denominator)
stats['available'] += int(available_prefixes.size / denominator)

# Calculate the percentage of total space for each prefix status.
total = float(stats['total'])
Expand All @@ -229,20 +235,6 @@ def alter_queryset(self, request):

return rirs

def extra_context(self):

totals = {
'total': sum([rir.stats['total'] for rir in self.queryset]),
'active': sum([rir.stats['active'] for rir in self.queryset]),
'reserved': sum([rir.stats['reserved'] for rir in self.queryset]),
'deprecated': sum([rir.stats['deprecated'] for rir in self.queryset]),
'available': sum([rir.stats['available'] for rir in self.queryset]),
}

return {
'totals': totals,
}


class RIRCreateView(PermissionRequiredMixin, ObjectEditView):
permission_required = 'ipam.add_rir'
Expand Down

0 comments on commit ba3ae0d

Please sign in to comment.