Skip to content

Commit fdf2a7f

Browse files
authored
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
1 parent 2ad5ea3 commit fdf2a7f

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

microsetta_private_api/admin/sample_summary.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,24 @@ def per_sample(project, barcodes, strip_sampleid):
4949
barcode_project = '; '.join(sorted(all_projects))
5050

5151
if source is not None and source_type == Source.SOURCE_TYPE_HUMAN:
52-
5352
vio_id = template_repo.get_vioscreen_id_if_exists(account.id,
5453
source.id,
5554
sample.id)
55+
# fall back on matching with source id
56+
if not vio_id:
57+
vio_id = \
58+
template_repo.get_vioscreen_id_if_exists(account.id,
59+
source.id,
60+
None)
61+
if vio_id:
62+
ffq_complete, ffq_taken, _ = \
63+
vs_repo.get_ffq_status_by_vio_id(vio_id)
64+
else:
65+
ffq_complete = False
66+
ffq_taken = False
67+
else:
68+
ffq_complete = False
69+
ffq_taken = False
5670

5771
# at least one sample has been observed that "is_microsetta",
5872
# described in the barcodes.project_barcode table, but which is
@@ -80,10 +94,6 @@ def per_sample(project, barcodes, strip_sampleid):
8094
sample_date = None
8195
sample_time = None
8296

83-
ffq_complete, ffq_taken, _ = vs_repo.get_ffq_status_by_sample(
84-
sample.id
85-
)
86-
8797
summary = {
8898
"sampleid": None if strip_sampleid else barcode,
8999
"project": barcode_project,

microsetta_private_api/repo/tests/test_vioscreen_sessions.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ def _to_dt(mon, day, year):
1818
# in ag_test db, this uuid corresponds to VIOSCREEN_USERNAME1
1919
BARCODE_UUID_FOR_VIOSESSION = '66ec7d9a-400d-4d71-bce8-fdf79d2be554'
2020
BARCODE_UUID_NOTIN_REGISTRY = 'edee4af9-65b2-4ed1-ba66-5bf58383005e'
21+
SOURCE_ID_FOR_VIOSESSION = '6d343527-ab68-4e01-9ef7-16943dc5cee0'
22+
SOURCE_ID_NOTIN_REGISTRY = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
23+
2124

2225
today = date.today()
2326
VIOSCREEN_SESSION = VioscreenSession(sessionId='a session',
@@ -265,33 +268,33 @@ def test_get_ffq_status_by_sample(self):
265268
r.upsert_session(session_copy)
266269
session = r.get_sessions_by_username(VIOSCREEN_USERNAME1)[0]
267270

268-
obs = r.get_ffq_status_by_sample(BARCODE_UUID_NOTIN_REGISTRY)
269-
self.assertEqual(obs, (False, False, None))
271+
obs = r.get_ffq_status_by_vio_id(VIOSCREEN_USERNAME1)
272+
self.assertEqual(obs, (False, False, 'something'))
270273

271274
session.status = 'Finished'
272275
session.endDate = _to_dt(2, 1, 1970)
273276
r.upsert_session(session)
274277

275278
# enumerate the empirically observed states from vioscreen
276279
# (is_complete, has_taken, exact_status)
277-
obs = r.get_ffq_status_by_sample(BARCODE_UUID_FOR_VIOSESSION)
280+
obs = r.get_ffq_status_by_vio_id(VIOSCREEN_USERNAME1)
278281
self.assertEqual(obs, (True, True, 'Finished'))
279282

280283
session.status = 'Started'
281284
session.endDate = None
282285
r.upsert_session(session)
283286

284-
obs = r.get_ffq_status_by_sample(BARCODE_UUID_FOR_VIOSESSION)
287+
obs = r.get_ffq_status_by_vio_id(VIOSCREEN_USERNAME1)
285288
self.assertEqual(obs, (False, True, 'Started'))
286289

287290
session.status = 'New'
288291
r.upsert_session(session)
289-
obs = r.get_ffq_status_by_sample(BARCODE_UUID_FOR_VIOSESSION)
292+
obs = r.get_ffq_status_by_vio_id(VIOSCREEN_USERNAME1)
290293
self.assertEqual(obs, (False, False, 'New'))
291294

292295
session.status = 'Review'
293296
r.upsert_session(session)
294-
obs = r.get_ffq_status_by_sample(BARCODE_UUID_FOR_VIOSESSION)
297+
obs = r.get_ffq_status_by_vio_id(VIOSCREEN_USERNAME1)
295298
self.assertEqual(obs, (False, True, 'Review'))
296299

297300

microsetta_private_api/repo/vioscreen_repo.py

+30-13
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ def get_unfinished_sessions(self):
168168

169169
return not_in_vioscreen_sessions + incomplete_sessions
170170

171-
def get_ffq_status_by_sample(self, sample_uuid):
172-
"""Obtain the FFQ status for a given sample
171+
def get_ffq_status_by_vio_id(self, vio_id):
172+
"""Obtain the FFQ status for a given source
173173
174174
Parameters
175175
----------
176-
sample_uuid : UUID4
176+
source_uuid : UUID4
177177
The UUID to check the status of
178178
179179
Returns
@@ -186,21 +186,38 @@ def get_ffq_status_by_sample(self, sample_uuid):
186186
if there is no FFQ associated with the sample.
187187
"""
188188
with self._transaction.cursor() as cur:
189-
cur.execute("""SELECT status
189+
cur.execute("""SELECT source_id
190+
FROM ag.vioscreen_registry
191+
WHERE vio_id = %s""", (vio_id, ))
192+
193+
source_res = cur.fetchone()
194+
195+
if source_res is None:
196+
return (False, False, None)
197+
198+
source_id = source_res[0]
199+
200+
cur.execute("""SELECT vs.status, akb.sample_date,
201+
akb.sample_time, vs.startdate
190202
FROM ag.vioscreen_sessions AS vs
191203
JOIN ag.vioscreen_registry AS vr
192-
ON vs.username=vr.vio_id
193-
WHERE sample_id=%s""", (sample_uuid, ))
194-
res = cur.fetchall()
195-
if len(res) == 0:
204+
ON vs.username = vr.vio_id
205+
LEFT JOIN ag.ag_kit_barcodes AS akb
206+
ON vr.source_id = akb.source_id
207+
WHERE vr.source_id = %s
208+
ORDER BY ABS(EXTRACT(EPOCH FROM
209+
(akb.sample_date - vs.startdate)))
210+
LIMIT 1""", (source_id, ))
211+
212+
res = cur.fetchone()
213+
214+
if res is None:
196215
return (False, False, None)
197-
elif len(res) == 1:
198-
status = res[0][0]
216+
else:
217+
status = res[0]
199218
is_complete = status == 'Finished'
200219
is_taken = status in ('Started', 'Review', 'Finished')
201-
return (is_complete, is_taken, status)
202-
else:
203-
raise ValueError("A sample should not have multiple FFQs")
220+
return is_complete, is_taken, status
204221

205222
def get_missing_ffqs(self):
206223
"""The set of valid sessions which lack FFQ data

0 commit comments

Comments
 (0)