Skip to content

Commit fc232c0

Browse files
committed
refactor(effort): extract CalculateWeightedAverage in EvaluationReportService
Replace four near-identical N5..N1 weighted-average + divide-by-zero guards in the summary and detail builders with a single helper.
1 parent 3008ece commit fc232c0

1 file changed

Lines changed: 15 additions & 32 deletions

File tree

web/Areas/Effort/Services/EvaluationReportService.cs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -333,36 +333,22 @@ private static EvalSummaryReport BuildEvalSummaryReport(EvalSummaryReport report
333333
.Select(instrGroup =>
334334
{
335335
var instrRows = instrGroup.ToList();
336-
var totalPts = instrRows.Sum(r =>
337-
r.N5 * 5m + r.N4 * 4m + r.N3 * 3m + r.N2 * 2m + r.N1 * 1m);
338-
var totalResponses = instrRows.Sum(r => r.N5 + r.N4 + r.N3 + r.N2 + r.N1);
339-
var weightedAvg = totalResponses > 0
340-
? Math.Round(totalPts / totalResponses, 2)
341-
: 0m;
342-
343336
return new EvalInstructorSummary
344337
{
345338
MothraId = instrGroup.Key,
346339
Instructor = instrRows[0].Instructor,
347-
WeightedAverage = weightedAvg,
340+
WeightedAverage = CalculateWeightedAverage(instrRows),
348341
TotalResponses = instrRows.Sum(r => r.NumResponses),
349342
TotalEnrolled = instrRows.Sum(r => r.NumEnrolled)
350343
};
351344
})
352345
.ToList();
353346

354-
// Department average: weighted across all courses in the department
355-
var deptTotalPts = deptGroup.Sum(r =>
356-
r.N5 * 5m + r.N4 * 4m + r.N3 * 3m + r.N2 * 2m + r.N1 * 1m);
357-
var deptTotalResponses = deptGroup.Sum(r => r.N5 + r.N4 + r.N3 + r.N2 + r.N1);
358-
359347
return new EvalDepartmentGroup
360348
{
361349
Department = deptGroup.Key,
362350
Instructors = instructors,
363-
DepartmentAverage = deptTotalResponses > 0
364-
? Math.Round(deptTotalPts / deptTotalResponses, 2)
365-
: 0m,
351+
DepartmentAverage = CalculateWeightedAverage(deptGroup),
366352
TotalResponses = deptGroup.Sum(r => r.NumResponses)
367353
};
368354
})
@@ -371,6 +357,17 @@ private static EvalSummaryReport BuildEvalSummaryReport(EvalSummaryReport report
371357
return report;
372358
}
373359

360+
/// <summary>
361+
/// Compute the weighted average of an N5..N1 rating distribution over a row collection,
362+
/// rounded to 2 decimals. Returns 0 when there are no responses to avoid divide-by-zero.
363+
/// </summary>
364+
private static decimal CalculateWeightedAverage(IEnumerable<EvalRawRow> rows)
365+
{
366+
var totalPts = rows.Sum(r => r.N5 * 5m + r.N4 * 4m + r.N3 * 3m + r.N2 * 2m + r.N1 * 1m);
367+
var totalResponses = rows.Sum(r => r.N5 + r.N4 + r.N3 + r.N2 + r.N1);
368+
return totalResponses > 0 ? Math.Round(totalPts / totalResponses, 2) : 0m;
369+
}
370+
374371
private EvalDetailReport BuildEvalDetailReport(EvalDetailReport report, List<EvalRawRow> rows)
375372
{
376373
if (rows.Count == 0)
@@ -404,11 +401,6 @@ private EvalDetailReport BuildEvalDetailReport(EvalDetailReport report, List<Eva
404401
})
405402
.ToList();
406403

407-
// Instructor weighted average: totalPts / totalResponses
408-
var totalPts = instrRows.Sum(r =>
409-
r.N5 * 5m + r.N4 * 4m + r.N3 * 3m + r.N2 * 2m + r.N1 * 1m);
410-
var totalResponses = instrRows.Sum(r => r.N5 + r.N4 + r.N3 + r.N2 + r.N1);
411-
412404
// Instructor median from combined distribution
413405
var totalN1 = instrRows.Sum(r => r.N1);
414406
var totalN2 = instrRows.Sum(r => r.N2);
@@ -421,26 +413,17 @@ private EvalDetailReport BuildEvalDetailReport(EvalDetailReport report, List<Eva
421413
MothraId = instrGroup.Key,
422414
Instructor = instrRows[0].Instructor,
423415
Courses = courses,
424-
InstructorAverage = totalResponses > 0
425-
? Math.Round(totalPts / totalResponses, 2)
426-
: 0m,
416+
InstructorAverage = CalculateWeightedAverage(instrRows),
427417
InstructorMedian = CalculateMedian(totalN1, totalN2, totalN3, totalN4, totalN5)
428418
};
429419
})
430420
.ToList();
431421

432-
// Department weighted average
433-
var deptTotalPts = deptGroup.Sum(r =>
434-
r.N5 * 5m + r.N4 * 4m + r.N3 * 3m + r.N2 * 2m + r.N1 * 1m);
435-
var deptTotalResponses = deptGroup.Sum(r => r.N5 + r.N4 + r.N3 + r.N2 + r.N1);
436-
437422
return new EvalDetailDepartmentGroup
438423
{
439424
Department = deptGroup.Key,
440425
Instructors = instructors,
441-
DepartmentAverage = deptTotalResponses > 0
442-
? Math.Round(deptTotalPts / deptTotalResponses, 2)
443-
: 0m
426+
DepartmentAverage = CalculateWeightedAverage(deptGroup)
444427
};
445428
})
446429
.ToList();

0 commit comments

Comments
 (0)