def usage(self, request, *args, **kwargs):
"Return usage for Prefix.pk == pk"
pk = kwargs.pop("pk", None)
prefix = Prefix.objects.get(pk=pk)
max_addr = IP(prefix.net_address).len()
active_addr = prefix_collector.collect_active_ip(prefix)
# calculate allocated ratio
query = PrefixQuerysetBuilder().within(prefix.net_address)
allocated = query.finalize().exclude(vlan__net_type="scope")
total_allocated = sum(p.get_prefix_size() for p in allocated)
payload = {
"max_addr": max_addr,
"active_addr": active_addr,
"usage": 1.0 * active_addr / max_addr,
"allocated": 1.0 * total_allocated / max_addr,
"pk": pk
}
return Response(payload, status=status.HTTP_200_OK)
评论列表
文章目录