2022-08-31 01:19:15 +01:00
|
|
|
from .models import Entry
|
2022-08-31 14:50:02 +01:00
|
|
|
from .tools.data import load
|
2022-08-29 17:09:07 +01:00
|
|
|
from .tools.quiz import compile_results, evaluate_answers, render_questions
|
2022-08-24 15:55:53 +01:00
|
|
|
|
2022-08-29 17:09:07 +01:00
|
|
|
from flask import Blueprint, jsonify, request
|
2022-08-31 14:50:02 +01:00
|
|
|
|
2022-08-31 14:50:16 +01:00
|
|
|
import numpy
|
2022-08-24 15:55:53 +01:00
|
|
|
|
|
|
|
views = Blueprint(
|
|
|
|
name='views',
|
|
|
|
import_name=__name__
|
|
|
|
)
|
|
|
|
|
2022-08-31 00:48:13 +01:00
|
|
|
@views.route('/questions/')
|
|
|
|
def _questions():
|
|
|
|
return render_questions()
|
2022-08-24 15:55:53 +01:00
|
|
|
|
2022-08-29 17:09:07 +01:00
|
|
|
@views.route('/submit/', methods=['POST'])
|
2022-08-24 15:55:53 +01:00
|
|
|
def _submit():
|
2022-08-29 17:09:07 +01:00
|
|
|
answers = request.json
|
|
|
|
scores = evaluate_answers(answers)
|
|
|
|
results = compile_results(results=scores)
|
2022-08-31 01:19:15 +01:00
|
|
|
new_entry = Entry(answers=answers, results=results)
|
|
|
|
new_entry.add()
|
2022-08-29 17:09:07 +01:00
|
|
|
return jsonify(results)
|
2022-08-24 15:55:53 +01:00
|
|
|
|
2022-08-31 14:50:16 +01:00
|
|
|
@views.route('/count/')
|
|
|
|
def _count():
|
|
|
|
return jsonify(len(Entry.query.all()))
|
|
|
|
|
|
|
|
@views.route('/playbooks/')
|
|
|
|
def _playbooks():
|
|
|
|
playbooks = dict.fromkeys(load('playbooks.json'), 0)
|
|
|
|
for entry in Entry.query.all():
|
|
|
|
for _playbook in entry.results['playbooks']: playbooks[_playbook] += 1
|
|
|
|
return playbooks
|
|
|
|
|
|
|
|
@views.route('/answers/')
|
|
|
|
def _answers():
|
2022-09-01 11:56:15 +01:00
|
|
|
answers = { }
|
|
|
|
for index, question in enumerate(render_questions()):
|
|
|
|
answers[index] = { }
|
|
|
|
for _index, answer in enumerate(question['answers']): answers[index][_index] = 0
|
2022-08-31 14:50:16 +01:00
|
|
|
for entry in Entry.query.all():
|
|
|
|
for index, answer in enumerate(entry.answers):
|
|
|
|
if type(answer) is list:
|
2022-09-01 11:56:15 +01:00
|
|
|
for option in answer: answers[index][option] += 1
|
2022-08-31 14:50:16 +01:00
|
|
|
else:
|
|
|
|
answers[index][answer] += 1
|
|
|
|
return list(answers.values())
|
|
|
|
|
|
|
|
@views.route('/scores/')
|
|
|
|
def _scores():
|
|
|
|
playbooks = { playbook: list() for playbook in load('playbooks.json') }
|
|
|
|
for playbook, scores in playbooks.items():
|
|
|
|
for entry in Entry.query.all():
|
|
|
|
score = entry.results['all_playbooks'][playbook]
|
|
|
|
percentage_score = 100 * score/entry.results['max_score']
|
|
|
|
scores.append(percentage_score)
|
|
|
|
output = { playbook: dict() for playbook in playbooks }
|
|
|
|
for playbook, scores in playbooks.items():
|
|
|
|
output[playbook]['mean'] = numpy.mean(scores)
|
|
|
|
output[playbook]['median'] = numpy.median(scores)
|
|
|
|
output[playbook]['standard_deviation'] = numpy.std(scores)
|
|
|
|
return output
|