avoid additional db calls to get tasks and json data w/ burnettk

This commit is contained in:
jasquat 2023-03-13 15:23:12 -04:00
parent e867dcbe4e
commit a221662fa8
No known key found for this signature in database
1 changed files with 37 additions and 20 deletions

View File

@ -613,14 +613,37 @@ class ProcessInstanceProcessor:
).first() ).first()
bpmn_process_dict = {"data": json_data.data, "tasks": {}} bpmn_process_dict = {"data": json_data.data, "tasks": {}}
bpmn_process_dict.update(bpmn_process.properties_json) bpmn_process_dict.update(bpmn_process.properties_json)
tasks = TaskModel.query.filter_by(bpmn_process_id=bpmn_process.id).all() if get_tasks:
for task in tasks: tasks = TaskModel.query.filter_by(bpmn_process_id=bpmn_process.id).all()
json_data = JsonDataModel.query.filter_by(hash=task.json_data_hash).first() cls._get_tasks_dict(tasks, bpmn_process_dict)
bpmn_process_dict["tasks"][task.guid] = task.properties_json
bpmn_process_dict["tasks"][task.guid]["data"] = json_data.data
return bpmn_process_dict return bpmn_process_dict
@classmethod
def _get_tasks_dict(
cls,
tasks: list[TaskModel],
spiff_bpmn_process_dict: dict,
bpmn_subprocess_id_to_guid_mappings: Optional[dict] = None,
) -> None:
json_data_hashes = set()
for task in tasks:
json_data_hashes.add(task.json_data_hash)
json_data_records = JsonDataModel.query.filter(JsonDataModel.hash.in_(json_data_hashes)).all() # type: ignore
json_data_mappings = {}
for json_data_record in json_data_records:
json_data_mappings[json_data_record.hash] = json_data_record.data
for task in tasks:
tasks_dict = spiff_bpmn_process_dict["tasks"]
if bpmn_subprocess_id_to_guid_mappings:
bpmn_subprocess_guid = bpmn_subprocess_id_to_guid_mappings[
task.bpmn_process_id
]
tasks_dict = spiff_bpmn_process_dict["subprocesses"][
bpmn_subprocess_guid
]["tasks"]
tasks_dict[task.guid] = task.properties_json
tasks_dict[task.guid]["data"] = json_data_mappings[task.json_data_hash]
@classmethod @classmethod
def _get_full_bpmn_process_dict( def _get_full_bpmn_process_dict(
cls, process_instance_model: ProcessInstanceModel cls, process_instance_model: ProcessInstanceModel
@ -655,9 +678,11 @@ class ProcessInstanceProcessor:
bpmn_subprocesses = BpmnProcessModel.query.filter_by( bpmn_subprocesses = BpmnProcessModel.query.filter_by(
parent_process_id=bpmn_process.id parent_process_id=bpmn_process.id
).all() ).all()
bpmn_subprocess_ids = {} bpmn_subprocess_id_to_guid_mappings = {}
for bpmn_subprocess in bpmn_subprocesses: for bpmn_subprocess in bpmn_subprocesses:
bpmn_subprocess_ids[bpmn_subprocess.id] = bpmn_subprocess.guid bpmn_subprocess_id_to_guid_mappings[bpmn_subprocess.id] = (
bpmn_subprocess.guid
)
single_bpmn_process_dict = cls._get_bpmn_process_dict( single_bpmn_process_dict = cls._get_bpmn_process_dict(
bpmn_subprocess bpmn_subprocess
) )
@ -666,19 +691,11 @@ class ProcessInstanceProcessor:
] = single_bpmn_process_dict ] = single_bpmn_process_dict
tasks = TaskModel.query.filter( tasks = TaskModel.query.filter(
TaskModel.bpmn_process_id.in_(bpmn_subprocess_ids.keys()) # type: ignore TaskModel.bpmn_process_id.in_(bpmn_subprocess_id_to_guid_mappings.keys()) # type: ignore
).all() ).all()
for task in tasks: cls._get_tasks_dict(
bpmn_subprocess_guid = bpmn_subprocess_ids[task.bpmn_process_id] tasks, spiff_bpmn_process_dict, bpmn_subprocess_id_to_guid_mappings
json_data = JsonDataModel.query.filter_by( )
hash=task.json_data_hash
).first()
spiff_bpmn_process_dict["subprocesses"][bpmn_subprocess_guid][
"tasks"
][task.guid] = task.properties_json
spiff_bpmn_process_dict["subprocesses"][bpmn_subprocess_guid][
"tasks"
][task.guid]["data"] = json_data.data
return spiff_bpmn_process_dict return spiff_bpmn_process_dict