Skip to content

Commit

Permalink
pool_report: floor/ceil to int
Browse files Browse the repository at this point in the history
this should make the results in the report more correct,
* floor `current_free`
* ceil used ips

make sure the numbers in the report are integers, see 1and1#198
  • Loading branch information
zeromind committed Feb 8, 2022
1 parent 2c94a08 commit 2d2aed3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
16 changes: 9 additions & 7 deletions dim/dim/commands.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os.path

from flask import current_app as app
from typing import Optional

from dim.models import UserType, Ipblock, Pool, AllocationHistory, Layer3Domain
from dim.rpc import get_user
from dim.transaction import time_function, transaction
from math import floor, ceil


def get_user_type(user_type_str):
Expand Down Expand Up @@ -48,7 +50,7 @@ def read_etc_file(filename):


@time_function
def pool_report(pool_name, prefix=None, estimate=30, warning_threshold=None, template=None):
def pool_report(pool_name: str, prefix: Optional[int] = None, estimate: int = 30, warning_threshold: Optional[int] = None, template: Optional[str] = None):
try:
if template is not None:
template_string = open(template).read()
Expand All @@ -57,7 +59,7 @@ def pool_report(pool_name, prefix=None, estimate=30, warning_threshold=None, tem
except Exception as e:
return "Missing template: " + str(e)

pool = Pool.query.filter_by(name=pool_name).first()
pool: Optional[Pool] = Pool.query.filter_by(name=pool_name).first()
if not pool:
return "WARNING: Pool %s does not exist." % pool_name
total_bits = 32 if pool.version == 4 else 128
Expand All @@ -66,7 +68,7 @@ def pool_report(pool_name, prefix=None, estimate=30, warning_threshold=None, tem
objects = "IPs"
else:
objects = "/%d blocks" % prefix
block_size = 2 ** (total_bits - prefix)
block_size: int = 2 ** (total_bits - prefix)
current_used = pool.used_ips
current_free = pool.total_ips - current_used
history_days = (1, 7, 30)
Expand All @@ -92,12 +94,12 @@ def pool_report(pool_name, prefix=None, estimate=30, warning_threshold=None, tem
prediction = "Data from {0} days ago not available.".format(estimate)

# Return report
def normalize(nr):
return nr / block_size # TODO float division? round?
usage = [normalize(current_used - ah.used_ips) if ah else 'n/a'
def normalize(nr) -> float:
return float(nr) / block_size
usage = [ceil(normalize(current_used - ah.used_ips)) if ah else 'n/a'
for ah in [pool.allocation_history(days) for days in history_days]]
keys = dict(pool_name=pool_name,
current_free=normalize(current_free),
current_free=floor(normalize(current_free)),
objects=objects,
prediction=prediction)
for i in range(len(history_days)):
Expand Down
5 changes: 3 additions & 2 deletions dim/dim/ipaddr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@


from ipaddress import ip_address, IPv4Address, IPv6Address, ip_network
from typing import Literal


def valid_block(addr):
Expand Down Expand Up @@ -66,7 +67,7 @@ def label(self, expanded=False):
return ret + "/" + str(self.prefix)

@property
def bits(self):
def bits(self) -> Literal[32, 128]:
return 32 if self.version == 4 else 128

@property
Expand All @@ -90,7 +91,7 @@ def broadcast(self):
return IP(self.address | self.hostmask, self.bits, self.version)

@property
def numhosts(self):
def numhosts(self) -> int:
return self.broadcast.address - self.network.address + 1

def __contains__(self, item):
Expand Down
8 changes: 4 additions & 4 deletions dim/dim/models/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ def update_modified(self):
self.modified_by = get_session_username()

@property
def total_ips(self):
def total_ips(self) -> int:
return sum(s.total for s in self.subnets)

@property
def used_ips(self):
def used_ips(self) -> int:
return sum(s.used for s in self.subnets)

def allocation_history(self, days_ago):
Expand Down Expand Up @@ -279,8 +279,8 @@ def free(self):
def total(self):
return self.ip.numhosts

def _used(self, only_static):
ret = 0
def _used(self, only_static) -> int:
ret: int = 0
q = db.session.query(Ipblock.prefix, func.count()).filter(Ipblock.parent == self)
if only_static:
q = q.join(IpblockStatus).filter(IpblockStatus.name == 'Static')
Expand Down

0 comments on commit 2d2aed3

Please sign in to comment.