示例#1
0
 def get_criteria_pack(self, criteria_pack_name):
     pack = CriterionPackDBManager().get_criterion_pack_by_name(
         criteria_pack_name)
     if not pack:
         pack = CriterionPackDBManager().get_criterion_pack_by_name(
             "SimplePack")
     return BaseCriterionPack.from_dict(pack.to_dict())
示例#2
0
def get_criteria_pack(pack_name):
    if not check_access():
        return {}, 404
    db_criterion_pack = CriterionPackDBManager().get_criterion_pack_by_name(
        pack_name)
    if db_criterion_pack:
        return db_criterion_pack.to_dict()
    else:
        return {}, 200
示例#3
0
def get_critetia_pack(pack_name):
    if not is_admin():
        return {}, 404
    pack = CriterionPackDBManager().get_criterion_pack_by_name(pack_name)
    if not pack:
        return {}, 404
    data = pack.to_dict()
    data['info'] = dumps(
        {
            'name':
            data['name'],
            'criterions': [[name, data['criterion_weights'].get(name, 0)]
                           for name in data['criterions']]
        },
        indent=2,
        ensure_ascii=False)
    return render_template('packs.html', data=data)
示例#4
0
def update_criteria_pack(pack_name):
    if not is_admin():
        return {}, 404

    pack_dict, msg = try_load_pack_dict(request.form.get('parameters'))
    if msg:
        return {'message': msg}, 200

    feedback = request.form.get('feedback', '<h4>No feedback</h4>')

    # will be created if doesn't exist
    db_pack = CriterionPackDBManager().add_pack_from_names(
        pack_dict['name'],
        (critetion[0] for critetion in pack_dict['criterions']),
        weights=dict(pack_dict['criterions']),
        feedback=feedback)

    logger.info(f"Updated criteria pack {db_pack.name}")
    return {
        'message': 'OK',
        'name': db_pack.name,
        'time': int(db_pack.last_update.timestamp() * 1000)
    }, 200
示例#5
0
 def run(self):
     while True:
         try:
             training_id = TrainingsToProcessDBManager().extract_training_id_to_process()
             if not training_id:
                 sleep(10)
                 continue
             logger.info('Extracted training with training_id = {}.'.format(training_id))
             training_db = TrainingsDBManager().get_training(training_id)
             if training_db is None:
                 TrainingsDBManager().change_training_status_by_training_id(
                     training_id, TrainingStatus.PROCESSING_FAILED
                 )
                 verdict = 'Training with training_id = {} was not found.'.format(training_id)
                 TrainingsDBManager().append_verdict(training_id, verdict)
                 TrainingsDBManager().set_score(training_id, 0)
                 logger.warning(verdict)
                 continue
             TrainingsDBManager().change_training_status_by_training_id(training_id, TrainingStatus.PROCESSING)
             audio_file = DBManager().get_file(training_db.audio_id)
             if audio_file is None:
                 TrainingsDBManager().change_training_status_by_training_id(
                     training_id, TrainingStatus.PROCESSING_FAILED
                 )
                 verdict = 'Audio file with audio_id = {}, training_id = {} was not found.'\
                     .format(training_db.audio_id, training_id)
                 TrainingsDBManager().append_verdict(training_id, verdict)
                 TrainingsDBManager().set_score(training_id, 0)
                 logger.warning(verdict)
                 continue
             audio = Audio.from_json_file(audio_file)
             audio_file.close()
             presentation_file = DBManager().get_file(training_db.presentation_id)
             if presentation_file is None:
                 TrainingsDBManager().change_training_status_by_training_id(
                     training_id, TrainingStatus.PROCESSING_FAILED
                 )
                 verdict = 'Presentation file with presentation_id = {}, training_id = {} was not found.'\
                     .format(training_db.presentation_id, training_id)
                 TrainingsDBManager().append_verdict(training_id, verdict)
                 TrainingsDBManager().set_score(training_id, 0)
                 logger.warning(verdict)
                 continue
             presentation = Presentation.from_json_file(presentation_file)
             presentation_file.close()
             criteria_pack_id = training_db.criteria_pack_id
             criteria_pack = CriteriaPackFactory().get_criteria_pack(criteria_pack_id)
             criteria_pack_db = CriterionPackDBManager().get_criterion_pack_by_name(criteria_pack.name)
             feedback_evaluator_id = training_db.feedback_evaluator_id
             feedback_evaluator = FeedbackEvaluatorFactory().get_feedback_evaluator(feedback_evaluator_id)(criteria_pack_db.criterion_weights)
             training = Training(training_id, audio, presentation, criteria_pack, feedback_evaluator)
             try:
                 feedback = training.evaluate_feedback()
             except Exception as e:
                 TrainingsDBManager().change_training_status_by_training_id(
                     training_id, TrainingStatus.PROCESSING_FAILED
                 )
                 verdict = 'Feedback evaluation for a training with training_id = {} has failed.\n{}'\
                     .format(training_id, e)
                 TrainingsDBManager().append_verdict(training_id, verdict)
                 TrainingsDBManager().set_score(training_id, 0)
                 logger.warning(verdict)
                 continue
             TrainingsDBManager().set_score(training_id, feedback.score)
             TrainingsDBManager().change_training_status_by_training_id(training_id, PresentationStatus.PROCESSED)
             task_attempt_id = training_db.task_attempt_id
             TaskAttemptsDBManager().update_scores(task_attempt_id, training_id, feedback.score)
         except Exception as e:
             logger.error('Unknown exception.\n{}'.format(e))
示例#6
0
def get_all_criterion_packs():
    if not is_admin():
        return {}, 404

    packs = CriterionPackDBManager().get_all_criterion_packs()
    return {'packs': packs, 'message': 'OK'}
示例#7
0
def view_training_statistics(training_id: str):
    """
    Route to show page with statistics of a training by its identifier

    :param training_id: Training identifier
    :return: Page with statistics of the training with the given identifier, or
        a dictionary with an explanation and 404 HTTP return code if something went wrong, or
        an empty dictionary with 404 HTTP return code if the file was not found or access was denied.
    """
    if not check_access({'_id': ObjectId(training_id)}):
        return {}, 404
    training_statistics, training_statistics_status_code = get_training_statistics(training_id)
    if training_statistics.get('message') != 'OK':
        return training_statistics, training_statistics_status_code
    criteria_pack_db = CriterionPackDBManager().get_criterion_pack_by_name(training_statistics['criteria_pack_id'])
    feedback = training_statistics['feedback']
    feedback_evaluator_id = training_statistics['feedback_evaluator_id']
    feedback_evaluator = FeedbackEvaluatorFactory().get_feedback_evaluator(feedback_evaluator_id)(criteria_pack_db.criterion_weights)
    criteria_results = feedback.get('criteria_results', {})
    if 'score' in feedback:
        feedback_str = '{} = {}'.format(t("Оценка за тренировку"),'{:.2f}'.format(feedback.get('score')))
        results_as_sum_str = feedback_evaluator.get_result_as_sum_str(criteria_results)
        if results_as_sum_str:
            feedback_str += ' = {}'.format(results_as_sum_str)
    else:
        feedback_str = t("Тренировка обрабатывается. Обновите страницу.")
    if criteria_results is not None:
        criteria_results_str = '\n'.join('{} = {}{}'.format(
            name,
            '{:.2f}'.format(result.get('result')),
            '' if not result.get('verdict', '')  else (', ' + result.get('verdict')),
        ) for (name, result) in criteria_results.items())
    else:
        criteria_results_str = ''
    criteria_results_str = '<br>'.join((criteria_results_str.replace('\n', '<br>'), criteria_pack_db.feedback))
    if 'verdict' in feedback:
        verdict_str = feedback.get('verdict').replace('\n', '\\n')
    else:
        verdict_str = ''
    training_status = training_statistics['training_status']
    training_status_str = TrainingStatus.russian.get(training_status, '')
    if training_status_str:
        training_status_str = '{}: {}'.format(t("Статус"), t(training_status_str))
    audio_status = training_statistics['audio_status']
    audio_status_str = AudioStatus.russian.get(audio_status, '')
    if audio_status_str:
        audio_status_str = '{}: {}'.format(t("Статус"), t(audio_status_str))
    presentation_status = training_statistics['presentation_status']
    presentation_status_str = PresentationStatus.russian.get(presentation_status, '')
    if presentation_status_str:
        presentation_status_str = '{}: {}'.format(t("Статус"), t(presentation_status_str))
    remaining_processing_time_estimation = training_statistics['remaining_processing_time_estimation']
    if remaining_processing_time_estimation and remaining_processing_time_estimation > 0:
        remaining_processing_time_estimation_str = '{}: {} с.'.format(t("Ожидаемое время обработки"),
            time.strftime("%M:%S", time.gmtime(remaining_processing_time_estimation)),
        )
    else:
        remaining_processing_time_estimation_str = ''
    return render_template(
        'training/statistics.html',
        title='{}: {}'.format(t("Статистика тренировки с ID"), training_id),
        training_id=training_id,
        presentation_file_id=training_statistics['presentation_file_id'],
        presentation_file_name=training_statistics['presentation_file_name'],
        presentation_record_file_id=training_statistics['presentation_record_file_id'],
        feedback=feedback_str,
        verdict=verdict_str,
        training_status=training_status_str,
        audio_status=audio_status_str,
        presentation_status=presentation_status_str,
        remaining_processing_time_estimation=remaining_processing_time_estimation_str,
        criteria_results=criteria_results_str.replace('\n', '\\n').replace('\'', '').replace('"', ''),
        slides_time=training_statistics['slides_time'],
    ), 200
示例#8
0
def view_training_greeting():
    """
    Route to view training greeting page. It shows information about the current task.

    :return: Training greeting page, or
        a dictionary with an explanation and 404 HTTP return code if task was not found, or
        an empty dictionary with 404 HTTP return code if access was denied.
    """
    user_session = check_auth()
    if not user_session:
        return {}, 404
    username = session.get('session_id')
    task_id = session.get('task_id')
    task_db = TasksDBManager().get_task(task_id)
    if task_db is None:
        return {'message': 'No task with id {}.'.format(task_id)}, 404
    task_description = task_db.task_description
    required_points = task_db.required_points
    attempt_count = task_db.attempt_count
    current_task_attempt = TaskAttemptsDBManager().get_current_task_attempt(username, task_id)
    if current_task_attempt is not None:
        training_number = len(current_task_attempt.training_scores) + 1
    else:
        training_number = 1
    if current_task_attempt is None or training_number > attempt_count:
        current_task_attempt = TaskAttemptsDBManager().add_task_attempt(
            username,
            task_id,
            user_session.tasks.get(task_id, {}).get('params_for_passback', ''),
            attempt_count,
        )
        training_number = 1
    task_attempt_count = TaskAttemptsDBManager().get_attempts_count(username, task_id)
    current_points_sum = \
        sum([score if score is not None else 0 for score in current_task_attempt.training_scores.values()])
    session['task_attempt_id'] = str(current_task_attempt.pk)
    criteria_pack_id = session.get('criteria_pack_id')
    criteria_pack = CriteriaPackFactory().get_criteria_pack(criteria_pack_id)
    criteria_pack_id = criteria_pack.name
    maximal_points = attempt_count * 1
    criteria_pack_description = criteria_pack.get_criteria_pack_weights_description(
        CriterionPackDBManager().get_criterion_pack_by_name(criteria_pack_id).criterion_weights,
    )
    # immediately create training if task has presentation 
    presentation_id, training_id = (str(task_db.presentation_id), add_training(str(task_db.presentation_id))[0].get('training_id')) if task_db.presentation_id else (None, None)

    return render_template(
        'training_greeting.html',
        task_id=task_id,
        task_description=task_description,
        current_points_sum='{:.2f}'.format(current_points_sum),
        required_points=required_points,
        maximal_points=maximal_points,
        attempt_number=task_attempt_count,
        training_number=training_number,
        attempt_count=attempt_count,
        criteria_pack_id=criteria_pack_id,
        criteria_pack_description=criteria_pack_description.replace('\n', '\\n').replace('\'', ''),
        training_id=training_id,
        presentation_id=presentation_id
    )
示例#9
0
def add_preconf_packs():
    for pack_name, criterion_info in pack_configuration.items():
        CriterionPackDBManager().add_pack_from_names(
            pack_name, (critetion[0] for critetion in criterion_info),
            weights=dict(criterion_info))