Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit Unfinished Vioscreen Sessions #527

Merged
merged 8 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/conda_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ psycopg2
flask
natsort
pycryptodome
pandas
pandas < 2.0.0
8 changes: 4 additions & 4 deletions microsetta_private_api/celery_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ def __call__(self, *args, **kwargs):
"task": "microsetta_private_api.util.vioscreen.refresh_headers",
"schedule": 55 * 60
},
# "update_vioscreen_sessions": {
# "task": "microsetta_private_api.util.vioscreen.update_session_detail", # noqa
# "schedule": 60 * 60 * 24 # every 24 hours
# },
"update_vioscreen_sessions": {
"task": "microsetta_private_api.util.vioscreen.update_session_detail", # noqa
"schedule": 60 * 60 * 24 # every 24 hours
},
"poll_daklapack_orders": {
"task": "microsetta_private_api.admin.daklapack_polling.poll_dak_orders", # noqa
"schedule": 60 * 60 * 4 # every 4 hours
Expand Down
39 changes: 31 additions & 8 deletions microsetta_private_api/repo/tests/test_vioscreen_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from microsetta_private_api.repo.transaction import Transaction
from microsetta_private_api.repo.vioscreen_repo import (VioscreenSessionRepo,
VioscreenPercentEnergyRepo) # noqa
from datetime import datetime
from datetime import datetime, date
from copy import copy


Expand All @@ -19,6 +19,7 @@ def _to_dt(mon, day, year):
BARCODE_UUID_FOR_VIOSESSION = '66ec7d9a-400d-4d71-bce8-fdf79d2be554'
BARCODE_UUID_NOTIN_REGISTRY = 'edee4af9-65b2-4ed1-ba66-5bf58383005e'

today = date.today()
VIOSCREEN_SESSION = VioscreenSession(sessionId='a session',
username='a user',
protocolId=1234,
Expand All @@ -27,7 +28,9 @@ def _to_dt(mon, day, year):
endDate=None,
cultureCode='foo',
created=_to_dt(1, 1, 1970),
modified=_to_dt(1, 1, 1970))
modified=_to_dt(
today.month, today.day, today.year
))

VIOSCREEN_PERCENT_ENERGY_COMPONENTS = [
VioscreenPercentEnergyComponent('%mfatot',
Expand Down Expand Up @@ -127,7 +130,11 @@ def test_get_unfinished_sessions(self):
with Transaction() as t:
r = VioscreenSessionRepo(t)
cur = t.cursor()
cur.execute("SELECT vio_id FROM ag.vioscreen_registry")
cur.execute("""SELECT DISTINCT(vio_id)
FROM ag.vioscreen_registry vr
INNER JOIN ag.ag_login_surveys als
ON vr.vio_id = als.survey_id
AND als.creation_time >= (CURRENT_DATE - 30)""")
exp = {r[0] for r in cur.fetchall()}
obs = r.get_unfinished_sessions()
self.assertEqual({r.username for r in obs}, exp)
Expand All @@ -138,7 +145,11 @@ def test_get_unfinished_sessions(self):
self.assertTrue(obs)

# our base session is still unfinished (no end date)
cur.execute("SELECT vio_id FROM ag.vioscreen_registry")
cur.execute("""SELECT DISTINCT(vio_id)
FROM ag.vioscreen_registry vr
INNER JOIN ag.ag_login_surveys als
ON vr.vio_id = als.survey_id
AND als.creation_time >= (CURRENT_DATE - 30)""")
exp = {r[0] for r in cur.fetchall()}
obs = r.get_unfinished_sessions()
self.assertEqual({r.username for r in obs}, exp)
Expand All @@ -160,7 +171,11 @@ def test_get_unfinished_sessions_multiple(self):
with Transaction() as t:
r = VioscreenSessionRepo(t)
cur = t.cursor()
cur.execute("SELECT vio_id FROM ag.vioscreen_registry")
cur.execute("""SELECT DISTINCT(vio_id)
FROM ag.vioscreen_registry vr
INNER JOIN ag.ag_login_surveys als
ON vr.vio_id = als.survey_id
AND als.creation_time >= (CURRENT_DATE - 30)""")
exp = {r[0] for r in cur.fetchall()}
obs = r.get_unfinished_sessions()
self.assertEqual({r.username for r in obs}, exp)
Expand All @@ -177,7 +192,11 @@ def test_get_unfinished_sessions_multiple(self):
self.assertTrue(obs)

# our sessions are unfinished
cur.execute("SELECT vio_id FROM ag.vioscreen_registry")
cur.execute("""SELECT DISTINCT(vio_id)
FROM ag.vioscreen_registry vr
INNER JOIN ag.ag_login_surveys als
ON vr.vio_id = als.survey_id
AND als.creation_time >= (CURRENT_DATE - 30)""")
exp = {r[0] for r in cur.fetchall()}
obs = r.get_unfinished_sessions()
self.assertEqual({r.username for r in obs}, exp)
Expand All @@ -189,9 +208,13 @@ def test_get_unfinished_sessions_multiple(self):
self.assertTrue(obs)

# one session is finished, and we only actually understand the
# sematics of a single session anyway, so under our current
# semantics of a single session anyway, so under our current
# operating assumptions, this users FFQ is now complete
cur.execute("SELECT vio_id FROM ag.vioscreen_registry")
cur.execute("""SELECT DISTINCT(vio_id)
FROM ag.vioscreen_registry vr
INNER JOIN ag.ag_login_surveys als
ON vr.vio_id = als.survey_id
AND als.creation_time >= (CURRENT_DATE - 30)""")
exp = {r[0] for r in cur.fetchall()}
obs = r.get_unfinished_sessions()
self.assertEqual({r.username for r in obs},
Expand Down
12 changes: 8 additions & 4 deletions microsetta_private_api/repo/vioscreen_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,20 @@ def get_unfinished_sessions(self):
with self._transaction.cursor() as cur:
# criteria 1, vio_ids which are not in vioscreen_sessions
cur.execute("""SELECT distinct(vio_id)
FROM ag.vioscreen_registry
WHERE vio_id NOT IN (
FROM ag.vioscreen_registry vr
INNER JOIN ag.ag_login_surveys als
ON vr.vio_id = als.survey_id
WHERE vr.vio_id NOT IN (
SELECT distinct(username)
FROM ag.vioscreen_sessions)""")
FROM ag.vioscreen_sessions)
AND als.creation_time >= (CURRENT_DATE - 30)""")
not_in_vioscreen_sessions = [VioscreenSession.from_registry(u[0])
for u in cur.fetchall()]

# criteria 2 and 3
cur.execute(f"""SELECT {self._sql_cols}
FROM ag.vioscreen_sessions""")
FROM ag.vioscreen_sessions
WHERE modified >= (CURRENT_DATE - 30)""")

# array_agg doesn't work over these datatypes... ugh. this
# almost certainly could be done better directly within SQL
Expand Down