Skip to content

Commit

Permalink
use vio_id instead of source, update query
Browse files Browse the repository at this point in the history
  • Loading branch information
ayobi committed Sep 17, 2024
1 parent 34cda80 commit c0c97c2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
4 changes: 2 additions & 2 deletions microsetta_private_api/admin/sample_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def per_sample(project, barcodes, strip_sampleid):
template_repo.get_vioscreen_id_if_exists(account.id,
source.id,
None)
if source:
if vio_id:
ffq_complete, ffq_taken, _ = \
vs_repo.get_ffq_status_by_source(source.id)
vs_repo.get_ffq_status_by_vio_id(vio_id)
else:
ffq_complete = False
ffq_taken = False
Expand Down
33 changes: 10 additions & 23 deletions microsetta_private_api/repo/tests/test_vioscreen_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,47 +268,34 @@ def test_get_ffq_status_by_sample(self):
r.upsert_session(session_copy)
session = r.get_sessions_by_username(VIOSCREEN_USERNAME1)[0]

cur = t.cursor()
source_id_query = """
SELECT source_id
FROM ag.vioscreen_registry
WHERE sample_id = %s
"""
cur.execute(source_id_query, (BARCODE_UUID_FOR_VIOSESSION,))
source_id = cur.fetchone()

obs = r.get_ffq_status_by_source(SOURCE_ID_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_source(source_id)
self.assertEqual(obs, [(True, True, 'Finished'),
(True, True, 'Finished')])
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_source(source_id)
self.assertEqual(obs, [(False, True, 'Started'),
(False, True, 'Started')])
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_source(source_id)
self.assertEqual(obs, [(False, False, 'New'),
(False, False, 'New')])
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_source(source_id)
self.assertEqual(obs, [(False, True, 'Review'),
(False, True, 'Review')])
obs = r.get_ffq_status_by_vio_id(VIOSCREEN_USERNAME1)
self.assertEqual(obs, (False, True, 'Review'))


if __name__ == '__main__':
Expand Down
41 changes: 29 additions & 12 deletions microsetta_private_api/repo/vioscreen_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def get_unfinished_sessions(self):

return not_in_vioscreen_sessions + incomplete_sessions

def get_ffq_status_by_source(self, source_uuid):
def get_ffq_status_by_vio_id(self, vio_id):
"""Obtain the FFQ status for a given source
Parameters
Expand All @@ -186,21 +186,38 @@ def get_ffq_status_by_source(self, source_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 source_id=%s""", (source_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)
else:
results = []
for status in res:
is_complete = status[0] == 'Finished'
is_taken = status[0] in ('Started', 'Review', 'Finished')
results.append((is_complete, is_taken, status[0]))
return results
status = res[0]
is_complete = status == 'Finished'
is_taken = status in ('Started', 'Review', 'Finished')
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 c0c97c2

Please sign in to comment.