Commit 7bebb074 authored by Ossi Laine's avatar Ossi Laine
Browse files

Optimized file generation while exporting data to csv

parent 5e1b87d8
...@@ -1062,24 +1062,25 @@ def start_download_csv(): ...@@ -1062,24 +1062,25 @@ def start_download_csv():
def download_csv(meta): def download_csv(meta):
exp_id = meta["exp_id"] exp_id = meta["exp_id"]
data = generate_csv(exp_id)
# error handling # error handling
'''
if isinstance(data, Exception): if isinstance(data, Exception):
emit('timeout', {'exc': str(data)}) emit('timeout', {'exc': str(data)})
return return
'''
# create temporary file # create temporary file
fd, path = mkstemp() fd, path = mkstemp()
with os.fdopen(fd, 'w') as tmp: with os.fdopen(fd, 'w') as tmp:
tmp.write(data)
tmp.flush()
# return path and filename to front so user can start downloading if generate_csv(exp_id, tmp):
filename = "experiment_{}_{}".format(
exp_id, date.today().strftime("%Y-%m-%d")) # return path and filename to front so user can start downloading
path = path.split('/')[-1] filename = "experiment_{}_{}".format(
emit('file_ready', {'path': path, 'filename': filename}) exp_id, date.today().strftime("%Y-%m-%d"))
path = path.split('/')[-1]
emit('file_ready', {'path': path, 'filename': filename})
@socketio.on('end', namespace="/download_csv") @socketio.on('end', namespace="/download_csv")
......
...@@ -109,7 +109,7 @@ def map_answers_to_questions(answers, questions): ...@@ -109,7 +109,7 @@ def map_answers_to_questions(answers, questions):
@timeit @timeit
def generate_csv(exp_id): def generate_csv(exp_id, file_handle):
# answer sets with participant ids # answer sets with participant ids
participants = answer_set.query.filter_by( participants = answer_set.query.filter_by(
...@@ -129,8 +129,6 @@ def generate_csv(exp_id): ...@@ -129,8 +129,6 @@ def generate_csv(exp_id):
embody_questions = embody_question.query.filter_by( embody_questions = embody_question.query.filter_by(
experiment_idexperiment=exp_id).all() experiment_idexperiment=exp_id).all()
csv = ''
# create CSV-header # create CSV-header
header = 'participant id;' header = 'participant id;'
header += ';'.join([str(count) + '. bg_question: ' + q.background_question.strip() header += ';'.join([str(count) + '. bg_question: ' + q.background_question.strip()
...@@ -146,7 +144,7 @@ def generate_csv(exp_id): ...@@ -146,7 +144,7 @@ def generate_csv(exp_id):
header += ';' + ';'.join(['page' + str(idx) + '_' + str(count) + '. embody_question: ' + header += ';' + ';'.join(['page' + str(idx) + '_' + str(count) + '. embody_question: ' +
question.picture.strip() for count, question in enumerate(embody_questions, 1)]) question.picture.strip() for count, question in enumerate(embody_questions, 1)])
csv += header + '\r\n' file_handle.write(header + '\r\n')
# filter empty answer_sets # filter empty answer_sets
participants = list(filter(lambda participant: True if int( participants = list(filter(lambda participant: True if int(
...@@ -167,16 +165,17 @@ def generate_csv(exp_id): ...@@ -167,16 +165,17 @@ def generate_csv(exp_id):
try: try:
emit('progress', {'done': nth, 'from': len_participants}) emit('progress', {'done': nth, 'from': len_participants})
data = future.result() data = future.result()
csv += data + '\r\n' file_handle.write(data + '\n')
except Exception as exc: except Exception as exc:
print('generated an exception: {}'.format(exc)) print('generated an exception: {}'.format(exc))
return exc emit('timeout', {'exc': str(data)})
return False
return csv
# file_handle.flush()
return True
def generate_answer_row(participant, pages, questions, embody_questions): def generate_answer_row(participant, pages, questions, embody_questions):
# TODO: refactor
with app.app_context(): with app.app_context():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment