Skip to content
This repository has been archived by the owner on Jul 29, 2020. It is now read-only.

Add logging for qiita pushing #255

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Knight Lab administrative portal
================================

This is the internal Knight Lab administration portal for creating, tracking, and processing general barcodes and the American Gut.
This is the internal Knight Lab administration portal for creating, tracking,
and processing general barcodes and the American Gut.
This codebase assumes that https://github.com/biocore/american-gut-web.git has
already been installed.


Installation instructions
Expand Down
45 changes: 37 additions & 8 deletions knimin/handlers/barcode_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
from knimin.handlers.access_decorators import set_access
from knimin.lib.configuration import config

import logging

handler = logging.StreamHandler()
fmt_str = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
handler.setFormatter(logging.Formatter(fmt_str))
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)


def get_qiita_client():
if config.debug:
Expand Down Expand Up @@ -218,45 +227,65 @@ class PushQiitaHandler(BaseHandler):

@concurrent.run_on_executor
def _push_to_qiita(self, study_id, samples):
# TODO: add a mutex or block to ensure a single call process at a time
cats = self.qclient.get('/api/v1/study/%s/samples/info' % study_id)
cats = cats['categories']
logger.debug('Entering PushQiitaHandler._push_to_qiita()')

try:
cats = self.qclient.get('/api/v1/study/%s/samples/info' % study_id)
except Exception as e:
logger.debug("GETing failed: " + e.msg)

cats = cats['categories']
samples = align_with_qiita_categories(samples, cats)
data = json_encode(samples)

return self.qclient.http_patch('/api/v1/study/%s/samples' % study_id,
data=data)
logger.debug('PATCHing Qiita Database')
try:
self.qclient.http_patch('/api/v1/study/%s/samples' % study_id,
data=data)
except Exception as e:
logger.debug("PATCHing failed: " + e.msg)

logger.debug('Leaving PushQiitaHandler._push_to_qiita()')

@authenticated
def get(self):
dat = {}

barcodes = db.get_unsent_barcodes_from_qiita_buffer()
status = db.get_send_qiita_buffer_status()
dat = {'status': status, "barcodes": barcodes}

self.write(json_encode(dat))
self.finish()

@authenticated
@gen.coroutine
def post(self):
barcodes = db.get_unsent_barcodes_from_qiita_buffer()
logger.debug(barcodes)

if not barcodes:
logger.debug('No barcodes were present in the buffer')
return

# certainly not a perfect mutex, however tornado is single threaded
status = db.get_send_qiita_buffer_status()
if status in ['Failed!', 'Pushing...']:
logger.debug(status)
logger.debug('Leaving(2) PushQiitaHandler.post()')
return

db.set_send_qiita_buffer_status("Pushing...")

logger.debug('Pushing to Qiita')
try:
yield self._push_to_qiita(self.study_id, barcodes)
except: # noqa
logger.debug('Push failed')
db.set_send_qiita_buffer_status("Failed!")
else:
db.mark_barcodes_sent_to_qiita(barcodes)
db.set_send_qiita_buffer_status("Idle")

db.mark_barcodes_sent_to_qiita(barcodes)
db.set_send_qiita_buffer_status("Idle")


@set_access(['Scan Barcodes'])
Expand Down
14 changes: 13 additions & 1 deletion knimin/lib/data_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
from geocoder import geocode, Location
from string_converter import converter

import logging

handler = logging.StreamHandler()
fmt_str = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
handler.setFormatter(logging.Formatter(fmt_str))
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)


class IncorrectEmailError(Exception):
pass
Expand Down Expand Up @@ -1454,7 +1463,10 @@ def mark_barcodes_sent_to_qiita(self, barcodes):
sql = """UPDATE project_qiita_buffer
SET pushed_to_qiita = 'Y'
WHERE barcode IN %s"""
self._con.execute(sql, [tuple(barcodes)])
try:
self._con.execute(sql, [tuple(barcodes)])
except Exception as e:
logger.debug(e.pgcode)

def add_barcodes_to_kit(self, ag_kit_id, num_barcodes=1):
"""Attaches barcodes to an existing american gut kit
Expand Down