Skip to content

Commit

Permalink
CU-8692nnx2z: resubmit all validated docs on startup, default is true.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomolopolis committed Sep 26, 2023
1 parent 009279a commit d39bd79
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 18 deletions.
5 changes: 4 additions & 1 deletion envs/env
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ CONCEPT_SEARCH_SERVICE_PORT=8983
### DB backup dir ###
DB_DIR=/home/api/db
DB_PATH=${DB_DIR}/db.sqlite3 # currently only supports sqlite3 dbs
DB_BACKUP_DIR=/home/api/db-backup
DB_BACKUP_DIR=/home/api/db-backup

# Resubmit all on startup
RESUBMIT_ALL_ON_STARTUP=1
3 changes: 3 additions & 0 deletions envs/env-prod
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ CONCEPT_SEARCH_SERVICE_PORT=8983
DB_DIR=/home/api/db
DB_PATH=${DB_DIR}/db.sqlite3 # currently only supports sqlite3 dbs
DB_BACKUP_DIR= # should be set to 'good' backup location

# Resubmit all on startup
RESUBMIT_ALL_ON_STARTUP=1
2 changes: 2 additions & 0 deletions webapp/api/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

default_app_config = 'api.apps.ApiConfig'
29 changes: 27 additions & 2 deletions webapp/api/api/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import logging
import os

from django.apps import AppConfig

logger = logging.getLogger(__name__)


class ApiConfig(AppConfig):
name = 'api'

class AnnoappConfig(AppConfig):
name = 'annoapp'
def ready(self):
from api.views import _submit_document
from api.models import ProjectAnnotateEntities
resubmit_all = os.environ.get('RESUBMIT_ALL_ON_STARTUP', False)
if resubmit_all:
logger.info('Found env var RESUBMIT_ALL_ON_STARTUP is True. '
'Attempting to resubmit all currently submitted state documents')
projects = ProjectAnnotateEntities.objects.all()
for project in projects:
validated_docs = project.validated_documents.all()
if len(validated_docs):
for doc in validated_docs:
try:
_submit_document(project, doc)
logger.info("Submitted doc: %s", doc.name)
except Exception as e:
logger.error("Failed to re-submit doc on startup with exception %s", e)
logger.info("Finished resubmitting Project %s", project.name)
logger.info("MedCATTrainer App API ready...")
36 changes: 21 additions & 15 deletions webapp/api/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,29 +382,19 @@ def import_cdb_concepts(request):
return Response({'message': 'submitted cdb import job.'})


@api_view(http_method_names=['POST'])
def submit_document(request):
# Get project id
p_id = request.data['project_id']
d_id = request.data['document_id']

# Get project and the right version of cat
project = ProjectAnnotateEntities.objects.get(id=p_id)
document = Document.objects.get(id=d_id)

cat = get_medcat(CDB_MAP=CDB_MAP, VOCAB_MAP=VOCAB_MAP,
CAT_MAP=CAT_MAP, project=project)

def _submit_document(project: ProjectAnnotateEntities, document: Document):
if project.train_model_on_submit:
try:
cat = get_medcat(CDB_MAP=CDB_MAP, VOCAB_MAP=VOCAB_MAP,
CAT_MAP=CAT_MAP, project=project)
train_medcat(cat, project, document)
except Exception as e:
if project.vocab.id:
if len(VOCAB_MAP[project.vocab.id].unigram_table) == 0:
return HttpResponseServerError('Vocab is missing the unigram table. On the vocab instance '
return Exception('Vocab is missing the unigram table. On the vocab instance '
'use vocab.make_unigram_table() to build')
else:
return HttpResponseServerError(e.message)
raise e

# Add cuis to filter if they did not exist
cuis = []
Expand All @@ -423,6 +413,22 @@ def submit_document(request):
project.cuis += ',' + ','.join(extra_doc_cuis)
project.save()


@api_view(http_method_names=['POST'])
def submit_document(request):
# Get project id
p_id = request.data['project_id']
d_id = request.data['document_id']

# Get project and the right version of cat
project = ProjectAnnotateEntities.objects.get(id=p_id)
document = Document.objects.get(id=d_id)

try:
_submit_document(project, document)
except Exception as e:
HttpResponseServerError(e.message)

return Response({'message': 'Document submited successfully'})


Expand Down
2 changes: 2 additions & 0 deletions webapp/scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ if [ $LOAD_EXAMPLES ]; then
fi

uwsgi --http-timeout 360s --http :8000 --master --chdir /home/api/ --module core.wsgi


0 comments on commit d39bd79

Please sign in to comment.