2019-11-18 13:23:53 +00:00
|
|
|
from flask import (abort, current_app, flash, make_response, render_template,
|
2019-11-25 09:12:39 +00:00
|
|
|
url_for)
|
2019-07-19 11:28:17 +00:00
|
|
|
from flask_login import current_user, login_required
|
2020-07-03 12:41:57 +00:00
|
|
|
from .forms import ImportResultsForm
|
2019-10-16 14:52:05 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
|
|
|
from . import services
|
2020-03-27 11:06:11 +00:00
|
|
|
from .. import db
|
2020-04-03 16:49:45 +00:00
|
|
|
from ..jobs.forms import AddFileSetupJobForm, AddNLPJobForm, AddOCRJobForm
|
2020-07-03 12:41:57 +00:00
|
|
|
from ..models import Job, JobInput, Result, ResultFile, User
|
|
|
|
from .tables import ResultTable, ResultItem
|
2019-08-06 12:27:41 +00:00
|
|
|
import json
|
2019-08-09 09:48:43 +00:00
|
|
|
import os
|
2020-07-03 12:41:57 +00:00
|
|
|
import html
|
|
|
|
from datetime import datetime
|
2019-07-19 11:28:17 +00:00
|
|
|
|
|
|
|
|
2020-02-07 14:21:59 +00:00
|
|
|
SERVICES = {'corpus_analysis': {'name': 'Corpus analysis'},
|
2020-04-09 07:53:56 +00:00
|
|
|
'file-setup': {'name': 'File setup',
|
2020-04-03 16:49:45 +00:00
|
|
|
'resources': {'mem_mb': 4096, 'n_cores': 4},
|
|
|
|
'add_job_form': AddFileSetupJobForm},
|
2020-01-07 12:03:42 +00:00
|
|
|
'nlp': {'name': 'Natural Language Processing',
|
2019-09-27 11:56:52 +00:00
|
|
|
'resources': {'mem_mb': 4096, 'n_cores': 2},
|
2019-11-08 11:21:59 +00:00
|
|
|
'add_job_form': AddNLPJobForm},
|
2019-09-27 11:56:52 +00:00
|
|
|
'ocr': {'name': 'Optical Character Recognition',
|
|
|
|
'resources': {'mem_mb': 8192, 'n_cores': 4},
|
2020-04-03 16:49:45 +00:00
|
|
|
'add_job_form': AddOCRJobForm}}
|
2019-08-06 12:27:41 +00:00
|
|
|
|
2019-08-01 06:22:17 +00:00
|
|
|
|
2019-11-08 11:21:59 +00:00
|
|
|
@services.route('/<service>', methods=['GET', 'POST'])
|
2019-08-05 13:35:18 +00:00
|
|
|
@login_required
|
2019-11-08 11:21:59 +00:00
|
|
|
def service(service):
|
|
|
|
if service not in SERVICES:
|
2019-09-24 14:55:24 +00:00
|
|
|
abort(404)
|
2020-02-07 14:21:59 +00:00
|
|
|
if service == 'corpus_analysis':
|
|
|
|
return render_template('services/{}.html.j2'.format(service),
|
|
|
|
title=SERVICES[service]['name'])
|
2020-02-18 11:01:36 +00:00
|
|
|
add_job_form = SERVICES[service]['add_job_form'](prefix='add-job-form')
|
2019-11-18 13:23:53 +00:00
|
|
|
if add_job_form.is_submitted():
|
|
|
|
if not add_job_form.validate():
|
|
|
|
return make_response(add_job_form.errors, 400)
|
2019-11-08 11:21:59 +00:00
|
|
|
service_args = []
|
|
|
|
if service == 'nlp':
|
|
|
|
service_args.append('-l {}'.format(add_job_form.language.data))
|
2020-02-13 13:41:02 +00:00
|
|
|
if add_job_form.check_encoding.data:
|
|
|
|
service_args.append('--check-encoding')
|
2019-11-08 11:21:59 +00:00
|
|
|
if service == 'ocr':
|
|
|
|
service_args.append('-l {}'.format(add_job_form.language.data))
|
2020-04-03 15:36:38 +00:00
|
|
|
if add_job_form.binarization.data:
|
|
|
|
service_args.append('--binarize')
|
2019-11-08 11:21:59 +00:00
|
|
|
job = Job(creator=current_user,
|
|
|
|
description=add_job_form.description.data,
|
|
|
|
mem_mb=SERVICES[service]['resources']['mem_mb'],
|
|
|
|
n_cores=SERVICES[service]['resources']['n_cores'],
|
|
|
|
service=service, service_args=json.dumps(service_args),
|
|
|
|
service_version=add_job_form.version.data,
|
|
|
|
status='preparing', title=add_job_form.title.data)
|
2020-02-17 13:57:24 +00:00
|
|
|
if job.service != 'corpus_analysis':
|
|
|
|
job.create_secure_filename()
|
2019-09-24 14:55:24 +00:00
|
|
|
db.session.add(job)
|
2019-08-06 12:27:41 +00:00
|
|
|
db.session.commit()
|
2019-11-08 11:21:59 +00:00
|
|
|
relative_dir = os.path.join(str(job.user_id), 'jobs', str(job.id))
|
2020-02-13 14:08:15 +00:00
|
|
|
absolut_dir = os.path.join(current_app.config['NOPAQUE_STORAGE'],
|
|
|
|
relative_dir)
|
2019-08-05 13:35:18 +00:00
|
|
|
try:
|
2019-11-08 11:21:59 +00:00
|
|
|
os.makedirs(absolut_dir)
|
2019-08-05 13:35:18 +00:00
|
|
|
except OSError:
|
2019-11-14 08:48:30 +00:00
|
|
|
job.delete()
|
2020-04-27 13:22:20 +00:00
|
|
|
flash('Internal Server Error', 'job')
|
2020-02-13 14:08:15 +00:00
|
|
|
return make_response({'redirect_url': url_for('services.service',
|
|
|
|
service=service)},
|
|
|
|
500)
|
2019-08-05 13:35:18 +00:00
|
|
|
else:
|
2019-11-08 11:21:59 +00:00
|
|
|
for file in add_job_form.files.data:
|
2019-10-16 14:52:05 +00:00
|
|
|
filename = secure_filename(file.filename)
|
2019-11-08 11:21:59 +00:00
|
|
|
file.save(os.path.join(absolut_dir, filename))
|
|
|
|
job_input = JobInput(dir=relative_dir, filename=filename,
|
|
|
|
job=job)
|
2019-10-16 14:52:05 +00:00
|
|
|
db.session.add(job_input)
|
2019-09-24 14:55:24 +00:00
|
|
|
job.status = 'submitted'
|
2019-08-12 15:03:12 +00:00
|
|
|
db.session.commit()
|
2020-04-27 13:22:20 +00:00
|
|
|
url = url_for('jobs.job', job_id=job.id)
|
|
|
|
flash('[<a href="{}">{}</a>] added'.format(url, job.title), 'job')
|
2019-11-18 13:23:53 +00:00
|
|
|
return make_response(
|
|
|
|
{'redirect_url': url_for('jobs.job', job_id=job.id)}, 201)
|
2019-11-08 11:21:59 +00:00
|
|
|
return render_template('services/{}.html.j2'.format(service),
|
|
|
|
title=SERVICES[service]['name'],
|
|
|
|
add_job_form=add_job_form)
|
2020-07-03 12:41:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@services.route('/import_results', methods=['GET', 'POST'])
|
|
|
|
@login_required
|
|
|
|
def import_results():
|
|
|
|
'''
|
|
|
|
View to import one json result file. Uses the ImportReultFileForm.
|
|
|
|
'''
|
|
|
|
# TODO: Build in a check if uploaded json is actually a result file and
|
|
|
|
# not something different
|
|
|
|
# Add the possibility to add several result files at once.
|
|
|
|
import_results_form = ImportResultsForm(prefix='add-result-file-form')
|
|
|
|
if import_results_form.is_submitted():
|
|
|
|
if not import_results_form.validate():
|
|
|
|
return make_response(import_results_form.errors, 400)
|
|
|
|
# Save the file
|
|
|
|
# result creation only happens on file save to avoid creating a result
|
|
|
|
# object in the db everytime by just visiting the import_results page
|
|
|
|
result = Result(user_id=current_user.id)
|
|
|
|
db.session.add(result)
|
|
|
|
db.session.commit()
|
|
|
|
if not (result.creator == current_user
|
|
|
|
or current_user.is_administrator()):
|
|
|
|
abort(403)
|
|
|
|
dir = os.path.join(str(result.user_id),
|
|
|
|
'results',
|
|
|
|
'corpus_analysis_results',
|
|
|
|
str(result.id))
|
|
|
|
abs_dir = os.path.join(current_app.config['NOPAQUE_STORAGE'], dir)
|
|
|
|
abs_file_path = os.path.join(abs_dir,
|
|
|
|
import_results_form.file.data.filename)
|
|
|
|
os.makedirs(abs_dir)
|
|
|
|
import_results_form.file.data.save(abs_file_path)
|
|
|
|
# Saves all needed metadata entries in one json field
|
|
|
|
with open(abs_file_path, 'r') as f:
|
|
|
|
corpus_metadata = json.load(f)
|
|
|
|
del corpus_metadata['matches']
|
|
|
|
del corpus_metadata['cpos_lookup']
|
|
|
|
result_file = ResultFile(
|
|
|
|
result_id=result.id,
|
|
|
|
dir=dir,
|
|
|
|
filename=import_results_form.file.data.filename)
|
|
|
|
result.corpus_metadata = corpus_metadata
|
|
|
|
db.session.add(result_file)
|
|
|
|
db.session.commit()
|
|
|
|
flash('Result file added!', 'result')
|
|
|
|
return make_response(
|
|
|
|
{'redirect_url': url_for('services.results')},
|
|
|
|
201)
|
|
|
|
return render_template('services/import_results.html.j2',
|
|
|
|
import_results_form=import_results_form,
|
|
|
|
title='Add corpus file')
|
|
|
|
|
|
|
|
|
|
|
|
@services.route('/results')
|
|
|
|
@login_required
|
|
|
|
def results():
|
|
|
|
'''
|
|
|
|
Shows an overview of imported results.
|
|
|
|
'''
|
|
|
|
# get all results of current user
|
|
|
|
results = User.query.get(current_user.id).results
|
|
|
|
# create table row for every result#
|
|
|
|
|
|
|
|
def __p_time(time_str):
|
|
|
|
return datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S.%f')
|
|
|
|
|
|
|
|
items = [ResultItem(r.corpus_metadata['query'],
|
|
|
|
r.corpus_metadata['match_count'],
|
|
|
|
r.corpus_metadata['corpus_name'],
|
|
|
|
__p_time(r.corpus_metadata['corpus_creation_date']),
|
|
|
|
__p_time(r.corpus_metadata['corpus_analysis_date']),
|
|
|
|
r.corpus_metadata['corpus_type'],
|
|
|
|
r.id) for r in results]
|
|
|
|
# create table with items and save it as html
|
|
|
|
table = html.unescape(ResultTable(items).__html__())
|
|
|
|
# add class=list to table body with string replacement
|
|
|
|
table = table.replace('tbody', 'tbody class=list', 1)
|
|
|
|
return render_template('services/results.html.j2',
|
|
|
|
title='Imported Results',
|
|
|
|
table=table)
|