2020-11-18 20:34:50 +00:00
|
|
|
from crc import session
|
|
|
|
from crc.api.common import ApiError
|
|
|
|
from crc.models.data_store import DataStoreModel
|
|
|
|
from crc.models.workflow import WorkflowModel
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
class DataStoreBase(object):
|
|
|
|
|
|
|
|
def overwritten(self, value, prev_value):
|
|
|
|
if prev_value is None:
|
|
|
|
overwritten = False
|
|
|
|
else:
|
|
|
|
if prev_value == value:
|
|
|
|
overwritten = False
|
|
|
|
else:
|
|
|
|
overwritten = True
|
|
|
|
return overwritten
|
|
|
|
|
2021-04-26 13:41:14 +00:00
|
|
|
|
|
|
|
def set_validate_common(self, study_id, workflow_id, user_id, script_name, file_id, *args):
|
2020-11-18 20:34:50 +00:00
|
|
|
self.check_args_2(args, script_name)
|
|
|
|
workflow = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first()
|
2021-04-26 13:41:14 +00:00
|
|
|
self.get_prev_value(study_id=study_id, user_id=user_id, file_id=file_id, key=args[0])
|
2020-11-18 20:34:50 +00:00
|
|
|
|
|
|
|
def check_args(self, args, maxlen=1, script_name='study_data_get'):
|
|
|
|
if len(args) < 1 or len(args) > maxlen:
|
|
|
|
raise ApiError(code="missing_argument",
|
|
|
|
message=f"The {script_name} script takes either one or two arguments, "
|
|
|
|
f"starting with the key and an optional default")
|
|
|
|
|
|
|
|
def check_args_2(self, args, script_name='study_data_set'):
|
|
|
|
if len(args) != 2:
|
|
|
|
raise ApiError(code="missing_argument",
|
|
|
|
message=f"The {script_name} script takes two arguments, starting with the key and a "
|
|
|
|
"value for the key")
|
|
|
|
|
2021-04-26 13:55:28 +00:00
|
|
|
def get_prev_value(self, study_id, user_id, key, file_id):
|
|
|
|
study = session.query(DataStoreModel).filter_by(study_id=study_id,
|
|
|
|
user_id=user_id,
|
|
|
|
file_id=file_id,
|
|
|
|
key=key).first()
|
2020-11-18 20:34:50 +00:00
|
|
|
return study
|
|
|
|
|
2021-04-26 13:55:28 +00:00
|
|
|
def set_data_common(self,
|
|
|
|
task_id,
|
|
|
|
study_id,
|
|
|
|
user_id,
|
|
|
|
workflow_id,
|
|
|
|
workflow_spec_id,
|
|
|
|
script_name,
|
|
|
|
file_id,
|
|
|
|
*args,
|
|
|
|
**kwargs):
|
2020-11-18 20:34:50 +00:00
|
|
|
|
|
|
|
self.check_args_2(args, script_name=script_name)
|
2021-04-26 13:55:28 +00:00
|
|
|
study = self.get_prev_value(study_id=study_id,
|
|
|
|
user_id=user_id,
|
|
|
|
file_id=file_id,
|
|
|
|
key=args[0])
|
2020-11-18 20:34:50 +00:00
|
|
|
if workflow_spec_id is None and workflow_id is not None:
|
|
|
|
workflow = session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first()
|
|
|
|
workflow_spec_id = workflow.workflow_spec_id
|
|
|
|
if study is not None:
|
|
|
|
prev_value = study.value
|
|
|
|
else:
|
|
|
|
prev_value = None
|
|
|
|
study = DataStoreModel(key=args[0], value=args[1],
|
|
|
|
study_id=study_id,
|
|
|
|
task_id=task_id,
|
|
|
|
user_id=user_id, # Make this available to any User
|
2021-04-26 13:55:28 +00:00
|
|
|
file_id=file_id,
|
2020-11-18 20:34:50 +00:00
|
|
|
workflow_id=workflow_id,
|
|
|
|
spec_id=workflow_spec_id)
|
|
|
|
study.value = args[1]
|
2021-04-29 14:25:28 +00:00
|
|
|
study.last_updated = datetime.utcnow()
|
2020-11-18 20:34:50 +00:00
|
|
|
overwritten = self.overwritten(study.value, prev_value)
|
|
|
|
session.add(study)
|
|
|
|
session.commit()
|
|
|
|
return {'new_value': study.value,
|
|
|
|
'old_value': prev_value,
|
|
|
|
'overwritten': overwritten}
|
|
|
|
|
2021-04-26 13:55:28 +00:00
|
|
|
def get_data_common(self, study_id, user_id, script_name, file_id=None, *args):
|
2020-11-18 20:34:50 +00:00
|
|
|
self.check_args(args, 2, script_name)
|
2021-04-26 13:55:28 +00:00
|
|
|
study = session.query(DataStoreModel).filter_by(study_id=study_id,
|
|
|
|
user_id=user_id,
|
|
|
|
file_id=file_id,
|
|
|
|
key=args[
|
|
|
|
0]).first()
|
2020-11-18 20:34:50 +00:00
|
|
|
if study:
|
|
|
|
return study.value
|
|
|
|
else:
|
|
|
|
return args[1]
|
|
|
|
|
2021-04-26 13:55:28 +00:00
|
|
|
def get_multi_common(self, study_id, user_id, file_id=None):
|
|
|
|
study = session.query(DataStoreModel).filter_by(study_id=study_id,
|
|
|
|
user_id=user_id,
|
|
|
|
file_id=file_id)
|
2020-11-18 20:34:50 +00:00
|
|
|
return study
|