From fdf2a7f01e735bfd2c69ddc7e067fd540d048d12 Mon Sep 17 00:00:00 2001 From: Aaron O'Brien Date: Thu, 24 Oct 2024 15:34:52 -0400 Subject: [PATCH] multi ffqs fix (#581) * addresses #579 * lint and update tests * update source id for vioscreen test * update exp * test * update vioscreen test * test2 * test3 * test4 * fix vioscreen test * update per_sample based on feedback * use vio_id instead of source, update query --- .../admin/sample_summary.py | 20 ++++++--- .../repo/tests/test_vioscreen_sessions.py | 15 ++++--- microsetta_private_api/repo/vioscreen_repo.py | 43 +++++++++++++------ 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/microsetta_private_api/admin/sample_summary.py b/microsetta_private_api/admin/sample_summary.py index a64f00e2a..5b3312571 100644 --- a/microsetta_private_api/admin/sample_summary.py +++ b/microsetta_private_api/admin/sample_summary.py @@ -49,10 +49,24 @@ def per_sample(project, barcodes, strip_sampleid): barcode_project = '; '.join(sorted(all_projects)) if source is not None and source_type == Source.SOURCE_TYPE_HUMAN: - vio_id = template_repo.get_vioscreen_id_if_exists(account.id, source.id, sample.id) + # fall back on matching with source id + if not vio_id: + vio_id = \ + template_repo.get_vioscreen_id_if_exists(account.id, + source.id, + None) + if vio_id: + ffq_complete, ffq_taken, _ = \ + vs_repo.get_ffq_status_by_vio_id(vio_id) + else: + ffq_complete = False + ffq_taken = False + else: + ffq_complete = False + ffq_taken = False # at least one sample has been observed that "is_microsetta", # described in the barcodes.project_barcode table, but which is @@ -80,10 +94,6 @@ def per_sample(project, barcodes, strip_sampleid): sample_date = None sample_time = None - ffq_complete, ffq_taken, _ = vs_repo.get_ffq_status_by_sample( - sample.id - ) - summary = { "sampleid": None if strip_sampleid else barcode, "project": barcode_project, diff --git a/microsetta_private_api/repo/tests/test_vioscreen_sessions.py b/microsetta_private_api/repo/tests/test_vioscreen_sessions.py index 172ffc05e..51e5781b0 100644 --- a/microsetta_private_api/repo/tests/test_vioscreen_sessions.py +++ b/microsetta_private_api/repo/tests/test_vioscreen_sessions.py @@ -18,6 +18,9 @@ def _to_dt(mon, day, year): # in ag_test db, this uuid corresponds to VIOSCREEN_USERNAME1 BARCODE_UUID_FOR_VIOSESSION = '66ec7d9a-400d-4d71-bce8-fdf79d2be554' BARCODE_UUID_NOTIN_REGISTRY = 'edee4af9-65b2-4ed1-ba66-5bf58383005e' +SOURCE_ID_FOR_VIOSESSION = '6d343527-ab68-4e01-9ef7-16943dc5cee0' +SOURCE_ID_NOTIN_REGISTRY = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' + today = date.today() VIOSCREEN_SESSION = VioscreenSession(sessionId='a session', @@ -265,8 +268,8 @@ def test_get_ffq_status_by_sample(self): r.upsert_session(session_copy) session = r.get_sessions_by_username(VIOSCREEN_USERNAME1)[0] - obs = r.get_ffq_status_by_sample(BARCODE_UUID_NOTIN_REGISTRY) - self.assertEqual(obs, (False, False, None)) + obs = r.get_ffq_status_by_vio_id(VIOSCREEN_USERNAME1) + self.assertEqual(obs, (False, False, 'something')) session.status = 'Finished' session.endDate = _to_dt(2, 1, 1970) @@ -274,24 +277,24 @@ def test_get_ffq_status_by_sample(self): # enumerate the empirically observed states from vioscreen # (is_complete, has_taken, exact_status) - obs = r.get_ffq_status_by_sample(BARCODE_UUID_FOR_VIOSESSION) + obs = r.get_ffq_status_by_vio_id(VIOSCREEN_USERNAME1) self.assertEqual(obs, (True, True, 'Finished')) session.status = 'Started' session.endDate = None r.upsert_session(session) - obs = r.get_ffq_status_by_sample(BARCODE_UUID_FOR_VIOSESSION) + obs = r.get_ffq_status_by_vio_id(VIOSCREEN_USERNAME1) self.assertEqual(obs, (False, True, 'Started')) session.status = 'New' r.upsert_session(session) - obs = r.get_ffq_status_by_sample(BARCODE_UUID_FOR_VIOSESSION) + obs = r.get_ffq_status_by_vio_id(VIOSCREEN_USERNAME1) self.assertEqual(obs, (False, False, 'New')) session.status = 'Review' r.upsert_session(session) - obs = r.get_ffq_status_by_sample(BARCODE_UUID_FOR_VIOSESSION) + obs = r.get_ffq_status_by_vio_id(VIOSCREEN_USERNAME1) self.assertEqual(obs, (False, True, 'Review')) diff --git a/microsetta_private_api/repo/vioscreen_repo.py b/microsetta_private_api/repo/vioscreen_repo.py index 57bf4f407..55f3f1e4c 100644 --- a/microsetta_private_api/repo/vioscreen_repo.py +++ b/microsetta_private_api/repo/vioscreen_repo.py @@ -168,12 +168,12 @@ def get_unfinished_sessions(self): return not_in_vioscreen_sessions + incomplete_sessions - def get_ffq_status_by_sample(self, sample_uuid): - """Obtain the FFQ status for a given sample + def get_ffq_status_by_vio_id(self, vio_id): + """Obtain the FFQ status for a given source Parameters ---------- - sample_uuid : UUID4 + source_uuid : UUID4 The UUID to check the status of Returns @@ -186,21 +186,38 @@ def get_ffq_status_by_sample(self, sample_uuid): if there is no FFQ associated with the sample. """ with self._transaction.cursor() as cur: - cur.execute("""SELECT status + cur.execute("""SELECT source_id + FROM ag.vioscreen_registry + WHERE vio_id = %s""", (vio_id, )) + + source_res = cur.fetchone() + + if source_res is None: + return (False, False, None) + + source_id = source_res[0] + + cur.execute("""SELECT vs.status, akb.sample_date, + akb.sample_time, vs.startdate FROM ag.vioscreen_sessions AS vs JOIN ag.vioscreen_registry AS vr - ON vs.username=vr.vio_id - WHERE sample_id=%s""", (sample_uuid, )) - res = cur.fetchall() - if len(res) == 0: + ON vs.username = vr.vio_id + LEFT JOIN ag.ag_kit_barcodes AS akb + ON vr.source_id = akb.source_id + WHERE vr.source_id = %s + ORDER BY ABS(EXTRACT(EPOCH FROM + (akb.sample_date - vs.startdate))) + LIMIT 1""", (source_id, )) + + res = cur.fetchone() + + if res is None: return (False, False, None) - elif len(res) == 1: - status = res[0][0] + else: + status = res[0] is_complete = status == 'Finished' is_taken = status in ('Started', 'Review', 'Finished') - return (is_complete, is_taken, status) - else: - raise ValueError("A sample should not have multiple FFQs") + return is_complete, is_taken, status def get_missing_ffqs(self): """The set of valid sessions which lack FFQ data