netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.33k stars 2.6k forks source link

Recursive Power Calculation #18048

Open xarmin-dev opened 2 days ago

xarmin-dev commented 2 days ago

NetBox version

v4.1.6

Feature type

Change to existing functionality

Triage priority

I volunteer to perform this work (if approved)

Proposed functionality

I am proposing an alternative solution to previously proposed recursive power calculations, that does not require database modification or Many-to-Many models, and is hopefully less complex in nature.

I recently delved into the NetBox code after using it at my MSP business for a year or so, so apologies for any incorrect nomenclature in the below.

Here is proposed simplified pseudocode, modifying the get_power_draw function:

def get_power_draw(self, depth_context=0, powerports_context=None):

  if self.allocated_draw is not None: return self_power_utilization
  if depth_context > MAX_POWER_CALCULATION_DEPTH: return 0

  if not powerports_context: powerports_context = []

  total = 0
  powerports = self.get_downstream_powerports()

  for port in powerports:
    if powerport.device.identifier in powerports_context: break

    powerports_context.append(powerport.device.identifier)
    total += powerport.get_power_draw(depth_context + 1, powerports_context)['allocated']

  return total

Additionally, I propose modifying the UI to use the maximum_draw specified for the queried device in utilization calculations, rather than that of the Power Feed.

Advantages

Disadvantages

I am happy to make these changes and submit a pull request. It seems that this is a fairly common request in the community, and it would certainly help my own business as well.

Previous Issues (I am sure I missed some!)

Use case

Many environments outside of a datacenter are likely to use a PDU, connected to a UPS, connected to a Power Feed. Power is not accurately modeled in these situations, and can be confusing to users.

Allowing at least one deeper level of power calculation would allow NetBox users to more accurately determine and plan power usage across PDU, UPS, and Power Feeds.

Database changes

None

External dependencies

None

bctiemann commented 2 hours ago

Seems like a good approach, clear and simple (and would address a lot of piecemeal bugs as noted). Please be sure there are adequate unit tests covering loop conditions, and if we can get some statistics on what kind of performance impact this causes that would also be a good idea.