def get_queryset(self): "Build queryset for API using filters" net_types = self.request.query_params.getlist("net_type", None) if "all" in net_types: net_types = None search = self.request.query_params.get("search", None) if search is not None and search: net_types = None ip = self.request.query_params.get("ip", None) if ip is not None and ip: net_types = None organization = self.request.query_params.get("organization", None) vlan_number = self.request.query_params.get("vlan", None) description = self.request.query_params.get("description", None) within = self.request.query_params.get("within", None) usage = self.request.query_params.get("usage", None) # Build queryset queryset = PrefixQuerysetBuilder() queryset.within(within) queryset.net_type(net_types) queryset.search(search) queryset.organization(organization) queryset.vlan_number(vlan_number) queryset.contains_ip(ip) queryset.usage(usage) queryset.description(description) queryset.filter_full_prefixes() return queryset.finalize()
def get_queryset(self): # Extract filters organization = self.request.query_params.get("organization", None) # Build queryset queryset = PrefixQuerysetBuilder() queryset.organization(organization) queryset.net_type(["scope", "reserved"]) return queryset.finalize()
def get_queryset(self): # Extract filters prefix = self.request.QUERY_PARAMS.get("prefix", None) organization = self.request.QUERY_PARAMS.get("organization", None) # Build queryset queryset = PrefixQuerysetBuilder() queryset.organization(organization) queryset.net_type(["scope", "reserved"]) return queryset.finalize()
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)