Skip to content

Commit

Permalink
multi ffqs fix (#581)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ayobi authored Oct 24, 2024
1 parent 2ad5ea3 commit fdf2a7f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
20 changes: 15 additions & 5 deletions microsetta_private_api/admin/sample_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions microsetta_private_api/repo/tests/test_vioscreen_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -265,33 +268,33 @@ 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)
r.upsert_session(session)

# 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'))


Expand Down
43 changes: 30 additions & 13 deletions microsetta_private_api/repo/vioscreen_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit fdf2a7f

Please sign in to comment.