refactor: improve number rounding and handling in analytics calculations
This commit is contained in:
@@ -39,7 +39,11 @@ export async function POST({ setHeaders, request }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function sumArrObject<T>(arr: T[], key: keyof T) {
|
function sumArrObject<T>(arr: T[], key: keyof T) {
|
||||||
return arr.reduce((sum, item) => sum + (item[key] as number), 0);
|
return arr.reduce((sum, item) => sum + ((item[key] as number) || 0), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function roundNumber(a: number, b: number) {
|
||||||
|
return Math.round(a / b) * 100 || 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getLastLogin(userId: string): Promise<string | undefined> {
|
async function getLastLogin(userId: string): Promise<string | undefined> {
|
||||||
@@ -99,14 +103,14 @@ async function getAudienceData(userId: string, orgId: string): Promise<UserAnaly
|
|||||||
const totalEarnedPoints = sumArrObject(exercisesStats, 'score');
|
const totalEarnedPoints = sumArrObject(exercisesStats, 'score');
|
||||||
const totalPoints = sumArrObject(exercisesStats, 'totalPoints');
|
const totalPoints = sumArrObject(exercisesStats, 'totalPoints');
|
||||||
|
|
||||||
const averageGrade = Math.round((totalEarnedPoints / totalPoints) * 100);
|
const averageGrade = roundNumber(totalEarnedPoints, totalPoints);
|
||||||
const lessonsCompleted = courseProgress?.lessons_completed || 0;
|
const lessonsCompleted = courseProgress?.lessons_completed || 0;
|
||||||
const lessonsCount = courseProgress?.lessons_count || 0;
|
const lessonsCount = courseProgress?.lessons_count || 0;
|
||||||
|
|
||||||
audienceAnalytics.courses.push({
|
audienceAnalytics.courses.push({
|
||||||
...course,
|
...course,
|
||||||
...courseProgress,
|
...courseProgress,
|
||||||
progress_percentage: Math.round((lessonsCompleted / lessonsCount) * 100),
|
progress_percentage: roundNumber(lessonsCompleted, lessonsCount),
|
||||||
average_grade: averageGrade
|
average_grade: averageGrade
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -119,10 +123,10 @@ async function getAudienceData(userId: string, orgId: string): Promise<UserAnaly
|
|||||||
(acc, course) => acc + course.lessons_completed,
|
(acc, course) => acc + course.lessons_completed,
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
const overallCourseProgress = Math.round((completedLessons / totalLessons) * 100);
|
const overallCourseProgress = roundNumber(completedLessons, totalLessons);
|
||||||
|
|
||||||
const allGrades = sumArrObject(audienceAnalytics.courses, 'average_grade');
|
const allGrades = sumArrObject(audienceAnalytics.courses, 'average_grade');
|
||||||
const overallAverageGrade = Math.round(allGrades / audienceAnalytics.courses.length);
|
const overallAverageGrade = roundNumber(allGrades, audienceAnalytics.courses.length);
|
||||||
|
|
||||||
audienceAnalytics.overallCourseProgress = overallCourseProgress;
|
audienceAnalytics.overallCourseProgress = overallCourseProgress;
|
||||||
audienceAnalytics.overallAverageGrade = overallAverageGrade || 0;
|
audienceAnalytics.overallAverageGrade = overallAverageGrade || 0;
|
||||||
@@ -177,7 +181,7 @@ async function getStudentAnalyticsData(
|
|||||||
const totalEarnedPoints = sumArrObject(userExercisesStats, 'score');
|
const totalEarnedPoints = sumArrObject(userExercisesStats, 'score');
|
||||||
const totalPoints = sumArrObject(userExercisesStats, 'totalPoints');
|
const totalPoints = sumArrObject(userExercisesStats, 'totalPoints');
|
||||||
|
|
||||||
userCourseAnalytics.averageGrade = Math.round((totalEarnedPoints / totalPoints) * 100);
|
userCourseAnalytics.averageGrade = roundNumber(totalEarnedPoints, totalPoints);
|
||||||
userCourseAnalytics.userExercisesStats = userExercisesStats;
|
userCourseAnalytics.userExercisesStats = userExercisesStats;
|
||||||
|
|
||||||
const completedLessons = lessonCompletions.filter((lesson) => lesson.completed);
|
const completedLessons = lessonCompletions.filter((lesson) => lesson.completed);
|
||||||
@@ -185,8 +189,9 @@ async function getStudentAnalyticsData(
|
|||||||
userCourseAnalytics.totalExercises = exerciseResponse.data[0].exercises_count;
|
userCourseAnalytics.totalExercises = exerciseResponse.data[0].exercises_count;
|
||||||
userCourseAnalytics.completedExercises = exerciseResponse.data[0].exercises_completed;
|
userCourseAnalytics.completedExercises = exerciseResponse.data[0].exercises_completed;
|
||||||
|
|
||||||
userCourseAnalytics.progressPercentage = Math.round(
|
userCourseAnalytics.progressPercentage = roundNumber(
|
||||||
(completedLessons.length / lessonCompletions.length) * 100
|
completedLessons.length,
|
||||||
|
lessonCompletions.length
|
||||||
);
|
);
|
||||||
|
|
||||||
return userCourseAnalytics;
|
return userCourseAnalytics;
|
||||||
@@ -202,7 +207,7 @@ async function fetchUserExercisesStats(
|
|||||||
.select(
|
.select(
|
||||||
`
|
`
|
||||||
lesson!inner (
|
lesson!inner (
|
||||||
title,exercise!inner (
|
title,exercise!left (
|
||||||
id,title,lesson_id,created_at,question (points),
|
id,title,lesson_id,created_at,question (points),
|
||||||
submission!left (
|
submission!left (
|
||||||
id,total,status_id,submitted_by,groupmember!inner (id,profile_id)
|
id,total,status_id,submitted_by,groupmember!inner (id,profile_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user