from microsetta_private_api.repo.transaction import Transaction
from microsetta_private_api.repo.vioscreen_repo import (
    VioscreenSessionRepo, VioscreenMPedsRepo)
from datetime import datetime
import json


def _to_dt(mon, day, year):
    return datetime(month=mon, day=day, year=year)


VIOSCREEN_SESSION = VioscreenSession(sessionId='0087da64cdcb41ad800c23531d1198f2',  # noqa
                                     username='******',
                                     protocolId=1234,
                                     status='something',
                                     startDate=_to_dt(1, 1, 1970),
                                     endDate=None,
                                     cultureCode='foo',
                                     created=_to_dt(1, 1, 1970),
                                     modified=_to_dt(1, 1, 1970))


def get_data_path(filename):
    package = 'microsetta_private_api/model/tests'
    return package + '/data/%s' % filename


class TestMPedsRepo(unittest.TestCase):

    def test_insert_mpeds_does_not_exist(self):
        with Transaction() as t:
Exemple #2
0
    def test_from_vioscreen(self):
        # a little helper method to simplify expected object instantiation
        def norm(ts):
            return pd.to_datetime(ts).tz_localize('US/Pacific')

        exp = [
            VioscreenSession(sessionId="000ada854d4f45f5abda90ccade7f0a8",
                             username="******",
                             protocolId=344,
                             status="Finished",
                             startDate=norm("2014-10-08T18:55:12.747"),
                             endDate=norm("2014-10-08T18:57:07.503"),
                             cultureCode="en-US",
                             created=norm("2014-10-08T18:55:07.96"),
                             modified=norm("2017-07-29T03:56:04.22")),
            VioscreenSession(sessionId="01013a5b4fa243a3b94db37f41ab4589",
                             username="******",
                             protocolId=344,
                             status="Finished",
                             startDate=norm("2014-11-09T01:06:57.237"),
                             endDate=norm("2014-11-09T01:18:22.65"),
                             cultureCode="en-US",
                             created=norm("2014-11-09T01:06:43.5"),
                             modified=norm("2017-07-29T03:44:07.817")),
            VioscreenSession(sessionId="010d7e02c6d242d29426992eb5be487f",
                             username="******",
                             protocolId=344,
                             status="Finished",
                             startDate=norm("2015-01-10T06:39:35.19"),
                             endDate=norm("2015-01-10T7:25:52.607"),
                             cultureCode="en-US",
                             created=norm("2015-01-10T06:39:06.187"),
                             modified=norm("2017-07-29T02:38:41.857")),
            VioscreenSession(sessionId="00737d1b445547ffa180aac38c19e18b",
                             username="******",
                             protocolId=344,
                             status="Started",
                             startDate=norm("2015-09-02T21:51:59.993"),
                             endDate=None,
                             cultureCode="en-US",
                             created=norm("2015-09-02T21:50:19.887"),
                             modified=norm("2016-06-16T7:17:57.86")),
            VioscreenSession(sessionId="0126a1104e434cd88bcff3e3ffb23c9a",
                             username="******",
                             protocolId=344,
                             status="Finished",
                             startDate=norm("2015-11-17T9:05:14.757"),
                             endDate=norm("2015-11-17T9:24:35.723"),
                             cultureCode="en-US",
                             created=norm("2015-11-17T9:04:53.623"),
                             modified=norm("2017-07-29T00:47:03.423"))
        ]

        USERS_DATA = []
        SESSIONS_DATA = []

        with open(get_data_path("users.data")) as data:
            USERS_DATA = json.load(data)

        with open(get_data_path("sessions.data")) as data:
            SESSIONS_DATA = json.load(data)

        for e, sessions_data in zip(exp, SESSIONS_DATA):
            users_data = [obj for obj in USERS_DATA if obj['username']
                          == sessions_data['username']][0]
            obs = VioscreenSession.from_vioscreen(sessions_data, users_data)
            self.assertEqual(e.__dict__, obs.__dict__)
def update_session_detail():
    # The interaction with the API is occuring within a celery task
    # and the recommendation from celery is to avoid depending on synchronous
    # tasks from within a task. As such, we'll use non-async calls here. See
    # http://docs.celeryq.org/en/latest/userguide/tasks.html#task-synchronous-subtasks

    # HOWEVER, we could implement a watch and monitor child tasks, but
    # not sure if that would be a particular benefit here
    vio_api = VioscreenAdminAPI(perform_async=False)
    current_task.update_state(state="PROGRESS",
                              meta={
                                  "completion": 0,
                                  "status": "PROGRESS",
                                  "message": "Gathering unfinished sessions..."
                              })

    # obtain our current unfinished sessions to check
    with Transaction() as t:
        r = VioscreenSessionRepo(t)
        unfinished_sessions = r.get_unfinished_sessions()

    failed_sessions = []
    n_to_get = len(unfinished_sessions)
    n_updated = 0
    for idx, sess in enumerate(unfinished_sessions, 1):
        updated = []

        # Out of caution, we'll wrap the external resource interaction within
        # a blanket try/except
        try:
            if sess.sessionId is None:
                # a session requires a mix of information from Vioscreen's
                # representation of a user and a session
                user_detail = vio_api.user(sess.username)
                details = vio_api.sessions(sess.username)

                # account for the possibility of a user having multiple
                # sessions
                updated.extend([
                    VioscreenSession.from_vioscreen(detail, user_detail)
                    for detail in details
                ])
            else:
                # update our model inplace
                update = vio_api.session_detail(sess.sessionId)
                if update.status != sess.status:
                    updated.append(sess.update_from_vioscreen(update))
        except Exception as e:  # noqa
            failed_sessions.append((sess.sessionId, str(e)))
            continue

        # commit as we go along to avoid holding any individual transaction
        # open for a long period
        if len(updated) > 0:
            with Transaction() as t:
                r = VioscreenSessionRepo(t)

                for update in updated:
                    r.upsert_session(update)
                t.commit()
                n_updated += len(updated)

        current_task.update_state(state="PROGRESS",
                                  meta={
                                      "completion": (idx / n_to_get) * 100,
                                      "status": "PROGRESS",
                                      "message": "Gathering session data..."
                                  })

    current_task.update_state(state="SUCCESS",
                              meta={
                                  "completion": n_to_get,
                                  "status": "SUCCESS",
                                  "message": f"{n_updated} sessions updated"
                              })

    if len(failed_sessions) > 0:
        # ...and let's make Daniel feel bad about not having a better means to
        # log what hopefully never occurs
        payload = ''.join(
            ['%s : %s\n' % (repr(s), m) for s, m in failed_sessions])
        send_email(SERVER_CONFIG['pester_email'], "pester_daniel", {
            "what": "Vioscreen sessions failed",
            "content": payload
        }, EN_US)
def _to_dt(mon, day, year):
    return datetime(month=mon, day=day, year=year)


VIOSCREEN_USERNAME1 = '3379e14164fac0ed'

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

VIOSCREEN_SESSION = VioscreenSession(sessionId='a session',
                                     username='******',
                                     protocolId=1234,
                                     status='something',
                                     startDate=_to_dt(1, 1, 1970),
                                     endDate=None,
                                     cultureCode='foo',
                                     created=_to_dt(1, 1, 1970),
                                     modified=_to_dt(1, 1, 1970))

VIOSCREEN_PERCENT_ENERGY_COMPONENTS = [
    VioscreenPercentEnergyComponent('%mfatot', 'foo', 'bar', '%', 10),
]
VIOSCREEN_PERCENT_ENERGY = \
    VioscreenPercentEnergy(sessionId='a session',
                           energy_components=VIOSCREEN_PERCENT_ENERGY_COMPONENTS)  # noqa


class VioscreenSessions(unittest.TestCase):
    def test_upsert_session_does_not_exist(self):
 def session_detail(self, session_id):
     sess_data = self.get('sessions/%s/detail' % session_id)
     user_data = self.user(sess_data['username'])
     return VioscreenSession.from_vioscreen(sess_data, user_data)