Split quota matching functions, and fix percentage

Change-Id: I2620a6e89545f8379c01fd495331700a44028494
This commit is contained in:
Adrian Turjak 2018-06-20 15:25:57 +12:00
parent 4ee2820ebc
commit efb6c732d1
1 changed files with 21 additions and 6 deletions

View File

@ -214,7 +214,7 @@ class QuotaManager(object):
return current_quota
def get_quota_size(self, current_quota, difference_threshold=None):
def get_quota_differences(self, current_quota):
""" Gets the closest matching quota size for a given quota """
quota_differences = {}
for size, setting in settings.PROJECT_QUOTA_SIZES.items():
@ -227,7 +227,9 @@ class QuotaManager(object):
continue
if value > 0:
current = current_quota[service_name][name]
match_percentages.append(float(current) / value)
dividend = float(min(current, value))
divisor = float(max(current, value))
match_percentages.append(dividend / divisor)
elif value < 0:
# NOTE(amelia): Sub-zero quota means unlimited
if current_quota[service_name][name] < 0:
@ -242,11 +244,24 @@ class QuotaManager(object):
difference = abs(
(sum(match_percentages) / float(len(match_percentages))) - 1)
if (difference <= self.size_diff_threshold):
quota_differences[size] = difference
quota_differences[size] = difference
if len(quota_differences) > 0:
return min(quota_differences, key=quota_differences.get)
return quota_differences
def get_quota_size(self, current_quota, difference_threshold=None):
""" Gets the closest matching quota size for a given quota """
quota_differences = self.get_quota_differences(current_quota)
diff_threshold = difference_threshold or self.size_diff_threshold
quota_differences_pruned = {}
for size, difference in quota_differences.items():
if (difference <= diff_threshold):
quota_differences_pruned[size] = difference
if len(quota_differences_pruned) > 0:
return min(
quota_differences_pruned, key=quota_differences_pruned.get)
# If we don't get a match return custom which means the project will
# need admin approval for any change
return 'custom'