import { Calculator, QuestionCalculator } from "./"; import { ExamStat, ExamQuestionFact, QuestionStat } from "../models"; import { SafePromise, Exception } from "@rolesvillesoftware/tools/dist"; import { Logger } from "../Logger"; export class QuestionsCalculator extends Calculator { private async getExamQuestions(exam: ExamStat): Promise { const result = this.processResults(await SafePromise.run(() => this.context.examQuestionFact .where((i, b) => i.examDimId == b.examDimId, exam) .execute() .toPromise() )); return result.toArray(); } async calculate(input?: ExamStat): Promise { const questionStats = new Array(); if (input == null) { return null; } Logger.log(`Loading questions`); const questions = this.processResults(await SafePromise.run(() => this.getExamQuestions(input))); if (questions == null || questions.length === 0) { throw new Exception(`No questions to process for exam ${input.id}`); } const calculator = Calculator.initialize(QuestionCalculator, this.context); Logger.log(`Exam questions: ${questions.length}`); for (const question of questions) { Logger.log(`\tCalculating question ${question.questionNo} - ${question.questionDimId} - ${question.id}`); var questionStat = this.processResults(await SafePromise.run(() => calculator.calculate({ examStat: input, question: question}))); questionStats.push(questionStat); } Logger.log(`Calculating FEV and PEV`); Logger.log(`Calculating question statistics completed`); return questionStats.filter(item => item != null); } }