django-orchestra/orchestra/apps/plans/rating.py

153 lines
4.8 KiB
Python
Raw Normal View History

2014-09-15 15:36:24 +00:00
import sys
from django.utils.translation import string_concat, ugettext_lazy as _
2014-09-26 15:05:20 +00:00
from orchestra.utils.python import AttrDict
2014-09-15 15:36:24 +00:00
def _compute(rates, metric):
value = 0
num = len(rates)
accumulated = 0
barrier = 1
next_barrier = None
2014-09-15 15:36:24 +00:00
end = False
ix = 0
steps = []
while ix < num and not end:
fold = 1
# Multiple contractions
while ix < num-1 and rates[ix] == rates[ix+1]:
ix += 1
fold += 1
2014-09-15 15:36:24 +00:00
if ix+1 == num:
quantity = metric - accumulated
next_barrier = quantity
2014-09-15 15:36:24 +00:00
else:
quantity = rates[ix+1].quantity - rates[ix].quantity
next_barrier = quantity
if rates[ix+1].price > rates[ix].price:
quantity *= fold
2014-09-15 15:36:24 +00:00
if accumulated+quantity > metric:
quantity = metric - accumulated
end = True
price = rates[ix].price
2014-09-26 15:05:20 +00:00
steps.append(AttrDict(**{
2014-09-15 15:36:24 +00:00
'quantity': quantity,
'price': price,
'barrier': barrier,
2014-09-15 15:36:24 +00:00
}))
accumulated += quantity
barrier += next_barrier
2014-09-15 15:36:24 +00:00
value += quantity*price
ix += 1
return value, steps
2014-09-26 10:38:50 +00:00
def _prepend_missing(rates):
"""
Support for incomplete rates
When first rate (quantity=5, price=10) defaults to nominal_price
"""
if rates:
first = rates[0]
if first.quantity == 0:
first.quantity = 1
elif first.quantity > 1:
if not isinstance(rates, list):
rates = list(rates)
service = first.service
rate_class = type(first)
rates.insert(0,
rate_class(service=service, plan=first.plan, quantity=1, price=service.nominal_price)
)
return rates
2014-09-15 15:36:24 +00:00
def step_price(rates, metric):
# Step price
group = []
minimal = (sys.maxint, [])
for plan, rates in rates.group_by('plan').iteritems():
2014-09-26 10:38:50 +00:00
rates = _prepend_missing(rates)
2014-09-15 15:36:24 +00:00
value, steps = _compute(rates, metric)
if plan.is_combinable:
group.append(steps)
else:
minimal = min(minimal, (value, steps), key=lambda v: v[0])
if len(group) == 1:
value, steps = _compute(rates, metric)
minimal = min(minimal, (value, steps), key=lambda v: v[0])
elif len(group) > 1:
# Merge
steps = []
for rates in group:
steps += rates
steps.sort(key=lambda s: s.price)
result = []
counter = 0
value = 0
ix = 0
targets = []
while counter < metric:
barrier = steps[ix].barrier
if barrier <= counter+1:
price = steps[ix].price
quantity = steps[ix].quantity
if quantity + counter > metric:
quantity = metric - counter
else:
for target in targets:
if counter + quantity >= target:
quantity = (counter+quantity+1) - target
steps[ix].quantity -= quantity
if not steps[ix].quantity:
steps.pop(ix)
break
else:
steps.pop(ix)
counter += quantity
value += quantity*price
if result and result[-1].price == price:
result[-1].quantity += quantity
else:
2014-09-26 15:05:20 +00:00
result.append(AttrDict(quantity=quantity, price=price))
2014-09-15 15:36:24 +00:00
ix = 0
targets = []
else:
targets.append(barrier)
ix += 1
minimal = min(minimal, (value, result), key=lambda v: v[0])
return minimal[1]
step_price.verbose_name = _("Step price")
step_price.help_text = _("All price rates with a lower metric are applied.")
2014-09-15 15:36:24 +00:00
def match_price(rates, metric):
candidates = []
selected = False
prev = None
2014-09-26 10:38:50 +00:00
rates = _prepend_missing(rates.distinct())
for rate in rates:
2014-09-23 16:23:36 +00:00
if prev:
if prev.plan != rate.plan:
if not selected and prev.quantity <= metric:
candidates.append(prev)
selected = False
if not selected and rate.quantity > metric:
if prev.quantity <= metric:
candidates.append(prev)
selected = True
2014-09-15 15:36:24 +00:00
prev = rate
if not selected and prev.quantity <= metric:
candidates.append(prev)
candidates.sort(key=lambda r: r.price)
2014-09-23 16:23:36 +00:00
if candidates:
2014-09-26 15:05:20 +00:00
return [AttrDict(**{
2014-09-23 16:23:36 +00:00
'quantity': metric,
'price': candidates[0].price,
})]
return None
match_price.verbose_name = _("Match price")
match_price.help_text = _("Only the rate with inmediate inferior metric is applied.")