in src/core/fraud_detector_utils.py [0:0]
def try_create_variable(self, variable_name, fraud_variable_type, data_type, default_value, description,
data_source=None):
"""
Tries to create the variable if it doesnt already exist
:param variable_name:
:param fraud_variable_type:
:param data_type:
:param default_value:
:param description:
:param data_source:
:return:
"""
try:
data_source = data_source or FRAUD_DETECTOR_DATA_SOURCE_EVENT
variables_resp = self.fraud_detector_client.get_variables(name=variable_name)
self._logger.info("Existing variable: {}".format(variable_name))
assert len(variables_resp["variables"]) == 1, "Expecting just one variable"
variable = variables_resp["variables"][0]
# Verify that existing variables, potentially used by other models are not accidentally changed.
# And raise an error if that is the case
if variable["dataType"] != data_type \
or variable["dataSource"] != data_source \
or variable["variableType"] != fraud_variable_type \
or str(variable["defaultValue"]) != str(default_value):
error_message_fmt = "The variable {} already exists, but the details {},{},{},{} do not match." \
" Please change the variable name or delete the existing variable"
raise ValueError(
error_message_fmt.format(json.dumps(variable), data_type, data_source, fraud_variable_type,
default_value))
except botocore.errorfactory.ClientError as error:
# If resource not found, it means that the variable doesnt exist, so lets create it
if error.response['Error']['Code'] == 'ResourceNotFoundException':
self._logger.info("Creating variable: {}".format(variable_name))
self.fraud_detector_client.create_variable(
name=variable_name,
dataType=data_type,
dataSource=data_source,
defaultValue=default_value,
description=description,
variableType=fraud_variable_type)
else:
raise error
return variable_name