def get_power_utilization(self): """Determine the utilization numerator and denominator for power utilization on the rack. Returns: UtilizationData: (numerator, denominator) """ powerfeeds = PowerFeed.objects.filter(rack=self) available_power_total = sum(pf.available_power for pf in powerfeeds) if not available_power_total: return UtilizationData(numerator=0, denominator=0) pf_powerports = PowerPort.objects.filter( _cable_peer_type=ContentType.objects.get_for_model(PowerFeed), _cable_peer_id__in=powerfeeds.values_list("id", flat=True), ) poweroutlets = PowerOutlet.objects.filter(power_port_id__in=pf_powerports) allocated_draw_total = ( PowerPort.objects.filter( _cable_peer_type=ContentType.objects.get_for_model(PowerOutlet), _cable_peer_id__in=poweroutlets.values_list("id", flat=True), ).aggregate(Sum("allocated_draw"))["allocated_draw__sum"] or 0 ) return UtilizationData(numerator=allocated_draw_total, denominator=available_power_total)
def get_utilization(self): """Get the child prefix size and parent size. For Prefixes with a status of "container", get the number child prefixes. For all others, count child IP addresses. Returns: UtilizationData (namedtuple): (numerator, denominator) """ if self.status == Prefix.STATUS_CONTAINER: queryset = Prefix.objects.filter(prefix__net_contained=str( self.prefix), vrf=self.vrf) child_prefixes = netaddr.IPSet([p.prefix for p in queryset]) return UtilizationData(numerator=child_prefixes.size, denominator=self.prefix.size) else: # Compile an IPSet to avoid counting duplicate IPs child_count = netaddr.IPSet( [ip.address.ip for ip in self.get_child_ips()]).size prefix_size = self.prefix.size if self.prefix.version == 4 and self.prefix.prefixlen < 31 and not self.is_pool: prefix_size -= 2 return UtilizationData(numerator=child_count, denominator=prefix_size)
def get_utilization(self): """Gets the numerator and denominator for calculating utilization of an Aggregrate. Returns: UtilizationData: Aggregate utilization (numerator=size of child prefixes, denominator=prefix size) """ queryset = Prefix.objects.net_contained_or_equal(self.prefix) child_prefixes = netaddr.IPSet([p.prefix for p in queryset]) return UtilizationData(numerator=child_prefixes.size, denominator=self.prefix.size)
def get_utilization(self): """Get the child prefix size and parent size. For Prefixes with a status of "container", get the number child prefixes. For all others, count child IP addresses. Returns: UtilizationData (namedtuple): (numerator, denominator) """ if self.status == Prefix.STATUS_CONTAINER: queryset = Prefix.objects.net_contained( self.prefix).filter(vrf=self.vrf) child_prefixes = netaddr.IPSet([p.prefix for p in queryset]) return UtilizationData(numerator=child_prefixes.size, denominator=self.prefix.size) else: prefix_size = self.prefix.size if self.prefix.version == 4 and self.prefix.prefixlen < 31 and not self.is_pool: prefix_size -= 2 child_count = prefix_size - self.get_available_ips().size return UtilizationData(numerator=child_count, denominator=prefix_size)
def get_utilization(self): """Gets utilization numerator and denominator for racks. Returns: UtilizationData: (numerator=Occupied Unit Count, denominator=U Height of the rack) """ # Determine unoccupied units available_units = self.get_available_units() # Remove reserved units for u in self.get_reserved_units(): if u in available_units: available_units.remove(u) # Return the numerator and denominator as percentage is to be calculated later where needed return UtilizationData(numerator=self.u_height - len(available_units), denominator=self.u_height)
def get_power_draw(self): """ Return the allocated and maximum power draw (in VA) and child PowerOutlet count for this PowerPort. """ # Calculate aggregate draw of all child power outlets if no numbers have been defined manually if self.allocated_draw is None and self.maximum_draw is None: poweroutlet_ct = ContentType.objects.get_for_model(PowerOutlet) outlet_ids = PowerOutlet.objects.filter( power_port=self).values_list("pk", flat=True) utilization = PowerPort.objects.filter( _cable_peer_type=poweroutlet_ct, _cable_peer_id__in=outlet_ids).aggregate( maximum_draw_total=Sum("maximum_draw"), allocated_draw_total=Sum("allocated_draw"), ) numerator = utilization["allocated_draw_total"] or 0 denominator = utilization["maximum_draw_total"] or 0 ret = { "allocated": utilization["allocated_draw_total"] or 0, "maximum": utilization["maximum_draw_total"] or 0, "outlet_count": len(outlet_ids), "legs": [], "utilization_data": UtilizationData( numerator=numerator, denominator=denominator, ), } # Calculate per-leg aggregates for three-phase feeds if getattr(self._cable_peer, "phase", None) == PowerFeedPhaseChoices.PHASE_3PHASE: # Setup numerator and denominator for later display. for leg, leg_name in PowerOutletFeedLegChoices: outlet_ids = PowerOutlet.objects.filter( power_port=self, feed_leg=leg).values_list("pk", flat=True) utilization = PowerPort.objects.filter( _cable_peer_type=poweroutlet_ct, _cable_peer_id__in=outlet_ids).aggregate( maximum_draw_total=Sum("maximum_draw"), allocated_draw_total=Sum("allocated_draw"), ) ret["legs"].append({ "name": leg_name, "allocated": utilization["allocated_draw_total"] or 0, "maximum": utilization["maximum_draw_total"] or 0, "outlet_count": len(outlet_ids), }) return ret if self.connected_endpoint and hasattr(self.connected_endpoint, "available_power"): denominator = self.connected_endpoint.available_power or 0 else: denominator = 0 # Default to administratively defined values return { "allocated": self.allocated_draw or 0, "maximum": self.maximum_draw or 0, "outlet_count": PowerOutlet.objects.filter(power_port=self).count(), "legs": [], "utilization_data": UtilizationData(numerator=self.allocated_draw or 0, denominator=denominator), }