From 1d4a7a9b94cd2e2df11a71ba34b6427b6753e931 Mon Sep 17 00:00:00 2001 From: jasquat Date: Wed, 8 Feb 2023 12:43:30 -0500 Subject: [PATCH] added additional columns to spiff_step_details w/ burnettk jbirddog --- spiffworkflow-backend/bin/recreate_db | 2 ++ .../migrations/versions/e05ca5cdc312_.py | 36 +++++++++++++++++++ .../models/spiff_step_details.py | 7 +++- .../services/process_instance_processor.py | 18 +++++++--- 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 spiffworkflow-backend/migrations/versions/e05ca5cdc312_.py diff --git a/spiffworkflow-backend/bin/recreate_db b/spiffworkflow-backend/bin/recreate_db index 39a07407..8a4e4b5a 100755 --- a/spiffworkflow-backend/bin/recreate_db +++ b/spiffworkflow-backend/bin/recreate_db @@ -50,6 +50,8 @@ if [[ "${1:-}" == "clean" ]]; then docker exec -it postgres-spiff psql -U spiffworkflow_backend spiffworkflow_backend_testing -c "create database spiffworkflow_backend_local_development;" fi fi +elif [[ "${1:-}" == "migrate" ]]; then + tasks="$tasks migrate" fi tasks="$tasks upgrade" diff --git a/spiffworkflow-backend/migrations/versions/e05ca5cdc312_.py b/spiffworkflow-backend/migrations/versions/e05ca5cdc312_.py new file mode 100644 index 00000000..8e931324 --- /dev/null +++ b/spiffworkflow-backend/migrations/versions/e05ca5cdc312_.py @@ -0,0 +1,36 @@ +"""empty message + +Revision ID: e05ca5cdc312 +Revises: ca9b79dde5cc +Create Date: 2023-02-08 12:21:41.722774 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql + +# revision identifiers, used by Alembic. +revision = 'e05ca5cdc312' +down_revision = 'ca9b79dde5cc' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('spiff_step_details', sa.Column('task_id', sa.String(length=50), nullable=False)) + op.add_column('spiff_step_details', sa.Column('bpmn_task_identifier', sa.String(length=255), nullable=False)) + op.add_column('spiff_step_details', sa.Column('engine_step_start_in_seconds', sa.DECIMAL(precision=17, scale=6), nullable=True)) + op.add_column('spiff_step_details', sa.Column('engine_step_end_in_seconds', sa.DECIMAL(precision=17, scale=6), nullable=True)) + op.drop_column('spiff_step_details', 'timestamp') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('spiff_step_details', sa.Column('timestamp', mysql.DECIMAL(precision=17, scale=6), nullable=False)) + op.drop_column('spiff_step_details', 'engine_step_end_in_seconds') + op.drop_column('spiff_step_details', 'engine_step_start_in_seconds') + op.drop_column('spiff_step_details', 'bpmn_task_identifier') + op.drop_column('spiff_step_details', 'task_id') + # ### end Alembic commands ### diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/models/spiff_step_details.py b/spiffworkflow-backend/src/spiffworkflow_backend/models/spiff_step_details.py index 0d0e7b44..5a844329 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/models/spiff_step_details.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/models/spiff_step_details.py @@ -27,4 +27,9 @@ class SpiffStepDetailsModel(SpiffworkflowBaseDBModel): ) spiff_step: int = db.Column(db.Integer, nullable=False) task_json: dict = deferred(db.Column(db.JSON, nullable=False)) # type: ignore - timestamp: float = db.Column(db.DECIMAL(17, 6), nullable=False) + task_id: str | None = db.Column(db.String(50), nullable=False) + bpmn_task_identifier: str | None = db.Column(db.String(255), nullable=False) + + # timestamp: float = db.Column(db.DECIMAL(17, 6), nullable=False) + engine_step_start_in_seconds: float | None = db.Column(db.DECIMAL(17, 6)) + engine_step_end_in_seconds: float | None = db.Column(db.DECIMAL(17, 6)) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py index d2745bf9..1641f213 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/process_instance_processor.py @@ -689,7 +689,7 @@ class ProcessInstanceProcessor: "lane_assignment_id": lane_assignment_id, } - def spiff_step_details_mapping(self, spiff_task: Optional[SpiffTask]=None) -> dict: + def spiff_step_details_mapping(self, spiff_task: Optional[SpiffTask]=None, start_in_seconds: Optional[float] = 0, end_in_seconds: Optional[float] = 0) -> dict: """SaveSpiffStepDetails.""" # bpmn_json = self.serialize() # wf_json = json.loads(bpmn_json) @@ -699,6 +699,9 @@ class ProcessInstanceProcessor: # TODO: safer to pass in task vs use last task? spiff_task = self.bpmn_process_instance.last_task + if spiff_task is None: + return {} + task_data = default_registry.convert(spiff_task.data) python_env = default_registry.convert(self._script_engine.environment.last_result()) @@ -708,13 +711,14 @@ class ProcessInstanceProcessor: "task_data": task_data, "python_env": python_env, } - return { "process_instance_id": self.process_instance_model.id, "spiff_step": self.process_instance_model.spiff_step or 1, "task_json": task_json, - "timestamp": round(time.time()), - # "completed_by_user_id": self.current_user().id, + "task_id": str(spiff_task.id), + "bpmn_task_identifier": spiff_task.task_spec.id, + "engine_step_start_in_seconds": start_in_seconds, + "engine_step_end_in_seconds": end_in_seconds, } def spiff_step_details(self) -> SpiffStepDetailsModel: @@ -1517,6 +1521,9 @@ class ProcessInstanceProcessor: # "Subprocess" } + # making a dictionary to ensure we are not shadowing variables in the other methods + current_task_start_in_seconds = {} + def should_log(task: SpiffTask) -> bool: if ( task.task_spec.spec_type in tasks_to_log @@ -1527,12 +1534,13 @@ class ProcessInstanceProcessor: def will_complete_task(task: SpiffTask) -> None: if should_log(task): + current_task_start_in_seconds['time'] = time.time() self.increment_spiff_step() def did_complete_task(task: SpiffTask) -> None: if should_log(task): self._script_engine.environment.revise_state_with_task_data(task) - step_details.append(self.spiff_step_details_mapping(task)) + step_details.append(self.spiff_step_details_mapping(task, current_task_start_in_seconds['time'], time.time())) try: self.bpmn_process_instance.refresh_waiting_tasks()