error catching for edge cases and poorly formatted params
This commit is contained in:
@ -1,32 +1,39 @@
|
|||||||
from pulp import lpSum
|
from pulp import lpSum
|
||||||
from random import randint, sample
|
from random import randint, sample
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from lib.errors.item_generation_error import ItemGenerationError
|
||||||
|
|
||||||
def build_constraints(solver_run, problem, items):
|
def build_constraints(solver_run, problem, items):
|
||||||
total_form_items = solver_run.total_form_items
|
try:
|
||||||
constraints = solver_run.constraints
|
total_form_items = solver_run.total_form_items
|
||||||
|
constraints = solver_run.constraints
|
||||||
|
|
||||||
for constraint in constraints:
|
for constraint in constraints:
|
||||||
attribute = constraint.reference_attribute
|
attribute = constraint.reference_attribute
|
||||||
min = constraint.minimum
|
min = constraint.minimum
|
||||||
max = constraint.maximum
|
max = constraint.maximum
|
||||||
|
|
||||||
if attribute.type == 'metadata':
|
if attribute.type == 'metadata':
|
||||||
con = dict(zip([item.id for item in solver_run.items],
|
con = dict(zip([item.id for item in solver_run.items],
|
||||||
[item.attribute_exists(attribute)
|
[item.attribute_exists(attribute)
|
||||||
for item in solver_run.items]))
|
for item in solver_run.items]))
|
||||||
problem += lpSum([con[item.id]
|
problem += lpSum([con[item.id]
|
||||||
* items[item.id]
|
* items[item.id]
|
||||||
for item in solver_run.items]) >= round(total_form_items * (min / 100)), f'{attribute.id} - {attribute.value} - min'
|
for item in solver_run.items]) >= round(total_form_items * (min / 100)), f'{attribute.id} - {attribute.value} - min'
|
||||||
problem += lpSum([con[item.id]
|
problem += lpSum([con[item.id]
|
||||||
* items[item.id]
|
* items[item.id]
|
||||||
for item in solver_run.items]) <= round(total_form_items * (max / 100)), f'{attribute.id} - {attribute.value} - max'
|
for item in solver_run.items]) <= round(total_form_items * (max / 100)), f'{attribute.id} - {attribute.value} - max'
|
||||||
elif attribute.type == 'bundle':
|
elif attribute.type == 'bundle':
|
||||||
bundles = solver_run.bundles(attribute.id)
|
bundles = solver_run.bundles(attribute.id)
|
||||||
total_bundles = randint(constraint.minimum, constraint.maximum)
|
total_bundles = randint(constraint.minimum, constraint.maximum)
|
||||||
selected_bundles = sample(bundles, total_bundles)
|
selected_bundles = sample(bundles, total_bundles)
|
||||||
|
|
||||||
for bundle in selected_bundles:
|
for bundle in selected_bundles:
|
||||||
problem += lpSum([items[item.id] for item in solver_run.items if getattr(item, bundle["type"], None) == bundle['id']]) == bundle['count'], f'Bundle constraint for {bundle["type"]} ({bundle["id"]})'
|
problem += lpSum([items[item.id] for item in solver_run.items if getattr(item, bundle["type"], None) == bundle['id']]) == bundle['count'], f'Bundle constraint for {bundle["type"]} ({bundle["id"]})'
|
||||||
|
|
||||||
|
|
||||||
return problem
|
return problem
|
||||||
|
except ValueError as error:
|
||||||
|
logging.error(error)
|
||||||
|
raise ItemGenerationError("Bundle min and/or max larger than bundle amount provided", error.args[0])
|
||||||
|
@ -19,6 +19,9 @@ class LoftService(Base):
|
|||||||
self.result = self.stream_to_s3_bucket()
|
self.result = self.stream_to_s3_bucket()
|
||||||
except ItemGenerationError as error:
|
except ItemGenerationError as error:
|
||||||
self.result = self.stream_to_s3_bucket(error)
|
self.result = self.stream_to_s3_bucket(error)
|
||||||
|
except TypeError as error:
|
||||||
|
logging.error(error)
|
||||||
|
self.result = self.stream_to_s3_bucket(ItemGenerationError("Provided params causing error in calculation results"))
|
||||||
|
|
||||||
def retreive_attributes_from_message(self):
|
def retreive_attributes_from_message(self):
|
||||||
logging.info('Retrieving attributes from message...')
|
logging.info('Retrieving attributes from message...')
|
||||||
|
Reference in New Issue
Block a user