Created more types of stats to be showcased:

- Total Exams per Module;
- Average Score per Module;
- Total Exercises per Type;
- Average Score per Exercise Type;
This commit is contained in:
Tiago Ribeiro
2023-04-20 10:38:55 +01:00
parent 87f7b717fc
commit e60130069d
4 changed files with 105 additions and 62 deletions

92
src/utils/stats.ts Normal file
View File

@@ -0,0 +1,92 @@
import {Module} from "@/interfaces";
import {Stat} from "@/interfaces/user";
import {capitalize} from "lodash";
import {convertCamelCaseToReadable} from "@/utils/string";
export const formatModuleTotalStats = (stats: Stat[]): {label: string; value: number}[] => {
const result: {[key in Module]: {exams: string[]; total: number}} = {
reading: {
exams: [],
total: 0,
},
listening: {
exams: [],
total: 0,
},
writing: {
exams: [],
total: 0,
},
speaking: {
exams: [],
total: 0,
},
};
stats.forEach((stat) => {
if (result[stat.module].exams)
result[stat.module] = {
exams: [...result[stat.module].exams.filter((x) => x !== stat.exam), stat.exam],
total: result[stat.module].total + 1,
};
});
return Object.keys(result).map((key) => ({label: capitalize(key), value: result[key as Module].total}));
};
export const formatModuleAverageScoreStats = (stats: Stat[]): {label: string; value: number}[] => {
const moduleScores: {[key: string]: {correct: number; total: number}} = {};
stats.forEach((stat) => {
if (stat.module in moduleScores) {
moduleScores[stat.module] = {
correct: moduleScores[stat.module].correct + stat.score.correct,
total: moduleScores[stat.module].total + stat.score.total,
};
} else {
moduleScores[stat.module] = stat.score;
}
});
return Object.keys(moduleScores).map((x) => {
const {correct, total} = moduleScores[x as keyof typeof moduleScores];
return {
label: capitalize(x),
value: correct / total,
};
});
};
export const formatExerciseTotalStats = (stats: Stat[]): {label: string; value: number}[] => {
const totalExercises = stats.map((stat) => ({
label: convertCamelCaseToReadable(stat.type),
value: stats.filter((x) => x.type === stat.type).length,
}));
return totalExercises.filter((ex, index) => totalExercises.findIndex((x) => x.label === ex.label) === index);
};
export const formatExerciseAverageScoreStats = (stats: Stat[]): {label: string; value: number}[] => {
const typeScores: {[key: string]: {correct: number; total: number}} = {};
stats.forEach((stat) => {
if (stat.type in typeScores) {
typeScores[stat.type] = {
correct: typeScores[stat.type].correct + stat.score.correct,
total: typeScores[stat.type].total + stat.score.total,
};
} else {
typeScores[stat.type] = stat.score;
}
});
return Object.keys(typeScores).map((x) => {
const {correct, total} = typeScores[x as keyof typeof typeScores];
return {
label: convertCamelCaseToReadable(x),
value: correct / total,
};
});
};