Skip to content

Commit

Permalink
Merge pull request #618 from sartography/dev
Browse files Browse the repository at this point in the history
Work on slow page load
  • Loading branch information
cullerton authored Oct 28, 2024
2 parents 5a451b9 + d7b1d38 commit 5d17d8f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 27 deletions.
62 changes: 39 additions & 23 deletions crc/services/study_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,22 @@ def get_pb_min_date():
def get_studies_for_user(self, user, categories, include_invalid=False):
"""Returns a list of all studies for the given user."""

# gets studies for the user
associated = session.query(StudyAssociated).filter_by(uid=user.uid, access=True).all()
associated_studies = [x.study_id for x in associated]
db_studies = session.query(StudyModel).filter((StudyModel.user_uid == user.uid) |
(StudyModel.id.in_(associated_studies))).all()

# add extra data to display in home page table
studies = []
for study_model in db_studies:
if include_invalid or study_model.review_type in self.VALID_REVIEW_TYPES:
studies.append(StudyService.get_study(study_model.id, categories, study_model=study_model,
process_categories=False))
for row in db_studies:
study_model = row
if study_model.review_type in self.VALID_REVIEW_TYPES:
primary_investigator = session.query(StudyAssociated).filter_by(study_id=study_model.id, role='Primary Investigator').first()
study_model.primary_investigator = primary_investigator.ldap_info.display_name if primary_investigator and primary_investigator.ldap_info else ''
study_model.last_activity_user, study_model.last_activity_date = self.get_last_user_and_date(study_model.id)
study_model.create_user_display = LdapService.user_info(study_model.user_uid).display_name
studies.append(study_model)
return studies

@staticmethod
Expand Down Expand Up @@ -114,6 +120,30 @@ def get_study_warnings(workflow_metas, status):

return warnings

@staticmethod
def get_progress_percent(study_id):
# Calculate study progress and return it as a integer out of a hundred
all_workflows = db.session.query(WorkflowModel).filter(WorkflowModel.study_id == study_id).count()
complete_workflows = db.session.query(WorkflowModel).filter(WorkflowModel.study_id == study_id).filter(WorkflowModel.status == WorkflowStatus.complete).count()
if all_workflows > 0:
return int((complete_workflows/all_workflows)*100)
else:
return 0

@staticmethod
def get_last_user_and_date(study_id):
last_event: TaskEventModel = session.query(TaskEventModel) \
.filter_by(study_id=study_id, action='COMPLETE') \
.order_by(TaskEventModel.date.desc()).first()
if last_event is None:
last_activity_user = 'Not Started'
last_activity_date = ""
else:
last_activity_user = LdapService.user_info(last_event.user_uid).display_name
last_activity_date = last_event.date

return last_activity_user, last_activity_date

@staticmethod
def get_study(study_id, categories: List[WorkflowSpecCategory], study_model: StudyModel = None,
master_workflow_results=None, process_categories=False):
Expand All @@ -123,15 +153,9 @@ def get_study(study_id, categories: List[WorkflowSpecCategory], study_model: Stu
study_model = session.query(StudyModel).filter_by(id=study_id).first()
study = Study.from_model(study_model)
study.create_user_display = LdapService.user_info(study.user_uid).display_name
last_event: TaskEventModel = session.query(TaskEventModel) \
.filter_by(study_id=study_id, action='COMPLETE') \
.order_by(TaskEventModel.date.desc()).first()
if last_event is None:
study.last_activity_user = 'Not Started'
study.last_activity_date = ""
else:
study.last_activity_user = LdapService.user_info(last_event.user_uid).display_name
study.last_activity_date = last_event.date
last_activity_user, last_activity_date = StudyService.get_last_user_and_date(study_id)
study.last_activity_user = last_activity_user
study.last_activity_date = last_activity_date
study.categories = categories
files = UserFileService.get_files_for_study(study.id)
files = (File.from_file_model(model, DocumentService.get_dictionary()) for model in files)
Expand All @@ -155,16 +179,8 @@ def get_study(study_id, categories: List[WorkflowSpecCategory], study_model: Stu
if associate.role == "Primary Investigator":
study.primary_investigator = associate.ldap_info.display_name

# Calculate study progress and return it as a integer out of a hundred
all_workflows = db.session.query(WorkflowModel).\
filter(WorkflowModel.study_id == study.id).\
count()
complete_workflows = db.session.query(WorkflowModel).\
filter(WorkflowModel.study_id == study.id).\
filter(WorkflowModel.status == WorkflowStatus.complete).\
count()
if all_workflows > 0:
study.progress = int((complete_workflows/all_workflows)*100)
# TODO: We don't use the progress bar. This should be removed.
# study.progress = StudyService.get_progress_percent(study.id)

return study

Expand Down
15 changes: 11 additions & 4 deletions tests/study/test_study_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from datetime import datetime
from unittest.mock import patch
from unittest import skip

from tests.base_test import BaseTest

Expand Down Expand Up @@ -36,6 +37,7 @@ def create_user_with_study_and_workflow(self):
db.session.commit()
return user

@skip("We don't actually use this feature.")
@patch('crc.services.protocol_builder.ProtocolBuilderService.get_study_details') # mock_details
@patch('crc.services.protocol_builder.ProtocolBuilderService.get_required_docs') # mock_docs
def test_study_progress(self, mock_docs, mock_details):
Expand Down Expand Up @@ -234,13 +236,18 @@ def test_get_user_studies_bad_review_type(self):
self.assertEqual(0, len(studies))

def test_study_associates(self):
user = self.create_user_with_study_and_workflow()
self.create_user_with_study_and_workflow()
study = db.session.query(StudyModel).first()
self.create_user(uid='lb3dp', email='lb3dp@example.com', display_name='lb3dp')
StudyService.update_study_associate(study_id=study.id, uid='lb3dp', role='Primary Investigator', send_email=True, access=True)
associates = StudyService.get_study_associates(study.id)
self.assertEquals(1, len(associates))
# get_study_associates always returns the owner of the study.
# so we should get 2 associates back.
self.assertEquals(2, len(associates))
assoc_json = StudyAssociatedSchema(many=True).dump(associates)
print(assoc_json)
self.assertEquals("Dan", assoc_json[0]['ldap_info']['given_name'])
self.assertEquals("Dan", assoc_json[1]['ldap_info']['given_name'])
self.assertEqual('Primary Investigator', assoc_json[0]['role'])
self.assertEqual('Laura Barnes', assoc_json[0]['ldap_info']['display_name'])

def test_set_category_metadata(self):
user = self.create_user_with_study_and_workflow()
Expand Down

0 comments on commit 5d17d8f

Please sign in to comment.