From 2ab96b16a04c98683d181b1031bbe163cd662fe8 Mon Sep 17 00:00:00 2001 From: alicia pritchett Date: Thu, 17 Mar 2022 17:20:42 -0400 Subject: [PATCH] Update the study model to include a progress (integer) --- crc/models/study.py | 7 +++++-- crc/services/study_service.py | 15 +++++++++++++++ migrations/versions/8f0d445dd297_.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/8f0d445dd297_.py diff --git a/crc/models/study.py b/crc/models/study.py index 26ceb0bd..80eddfcf 100644 --- a/crc/models/study.py +++ b/crc/models/study.py @@ -66,6 +66,7 @@ class StudyModel(db.Model): events_history = db.relationship("StudyEvent", cascade="all, delete, delete-orphan") short_name = db.Column(db.String, nullable=True) proposal_name = db.Column(db.String, nullable=True) + progress = db.Column(db.Integer, nullable=True) def update_from_protocol_builder(self, study: ProtocolBuilderCreatorStudy, user_id): self.title = study.TITLE @@ -186,7 +187,7 @@ class Study(object): id=None, status=None, progress_status=None, irb_status=None, short_name=None, proposal_name=None, comment="", sponsor="", ind_number="", categories=[], files=[], approvals=[], enrollment_date=None, events_history=[], - last_activity_user="",last_activity_date =None,create_user_display="", **argsv): + last_activity_user="",last_activity_date =None,create_user_display="", progress=0, **argsv): self.id = id self.user_uid = user_uid self.create_user_display = create_user_display @@ -210,6 +211,7 @@ class Study(object): self.events_history = events_history self.short_name = short_name self.proposal_name = proposal_name + self.progress = progress @classmethod def from_model(cls, study_model: StudyModel): @@ -274,13 +276,14 @@ class StudySchema(ma.Schema): events_history = fields.List(fields.Nested('StudyEventSchema'), dump_only=True) short_name = fields.String(allow_none=True) proposal_name = fields.String(allow_none=True) + progress = fields.Integer(allow_none=True) class Meta: model = Study additional = ["id", "title", "short_title", "last_updated", "primary_investigator_id", "user_uid", "sponsor", "ind_number", "files", "enrollment_date", "create_user_display", "last_activity_date", "last_activity_user", - "events_history", "short_name", "proposal_name"] + "events_history", "short_name", "proposal_name", "progress"] unknown = INCLUDE @marshmallow.post_load diff --git a/crc/services/study_service.py b/crc/services/study_service.py index 44cf75f6..728bff6e 100755 --- a/crc/services/study_service.py +++ b/crc/services/study_service.py @@ -74,6 +74,7 @@ class StudyService(object): studies.append(study) return studies + @staticmethod def get_study(study_id, categories: List[WorkflowSpecCategory], study_model: StudyModel = None, master_workflow_results=None): @@ -105,8 +106,21 @@ class StudyService(object): study.warnings = StudyService._update_status_of_workflow_meta(workflow_metas, master_workflow_results) category.workflows = workflow_metas + # Calculate study progress and return it as a integer out of a hundred + completed_wfs = 0 + total_wfs = 0 + for category in study.categories: + for workflow in category.workflows: + total_wfs +=1 + if workflow.status == WorkflowStatus.complete: + completed_wfs += 1 + if total_wfs > 0: + study.progress = int((completed_wfs/total_wfs)*100) + else: + study.progress = 0 return study + @staticmethod def _get_workflow_metas(study_id, category): # Add in the Workflows for each category @@ -266,6 +280,7 @@ class StudyService(object): g.doc_statuses[study_id] = StudyService.__get_documents_status(study_id) return g.doc_statuses[study_id] + @staticmethod def __get_documents_status(study_id): """Returns a list of documents related to the study, and any file information diff --git a/migrations/versions/8f0d445dd297_.py b/migrations/versions/8f0d445dd297_.py new file mode 100644 index 00000000..48424f58 --- /dev/null +++ b/migrations/versions/8f0d445dd297_.py @@ -0,0 +1,28 @@ +"""empty message + +Revision ID: 8f0d445dd297 +Revises: 28752ce0775c +Create Date: 2022-03-17 16:51:29.873798 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '8f0d445dd297' +down_revision = '28752ce0775c' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('study', sa.Column('progress', sa.Integer(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column('study', 'progress') + # ### end Alembic commands ###