forge
Explore
Log in

Don't you dare go Hollow

Summary statistics for graded results

Browse files at this commit

eb282a08846f91620b0c8d7569574ece93fc4a0fAhmet Kaan GümüşParent: ebc1bd2
  • feat
    • summary statistics for graded results
Show full message
✨ feat(exams): summary statistics for graded results

GET /exams/{id}/statistics (teacher+) reports the graded count and the
mean, min, and max of an exam's marks. Statistics are null while
nothing is graded; a missing exam is a 404, not an empty report.
src/web/exams.rsmodified+50 -0
@@ -25,6 +25,7 @@ pub fn routes() -> OpenApiRouter<AppState> {
25 .routes(routes!(grade, list_results))25 .routes(routes!(grade, list_results))
26 .routes(routes!(my_result))26 .routes(routes!(my_result))
27 .routes(routes!(remove_result))27 .routes(routes!(remove_result))
28+ .routes(routes!(exam_statistics))
28 }29 }
29 30
30 #[derive(Deserialize, ToSchema)]31 #[derive(Deserialize, ToSchema)]
@@ -66,6 +67,17 @@ impl ExamResultResponse {
66 }67 }
67 }68 }
68 69
70+#[derive(Serialize, ToSchema)]
71+struct ExamStatisticsResponse {
72+ exam: String,
73+ /// Number of graded results.
74+ graded: u64,
75+ /// Plain mean of the graded marks; `null` while nothing is graded.
76+ average: Option<f64>,
77+ min: Option<i64>,
78+ max: Option<i64>,
79+}
80+
69 /// The course an exam belongs to. A dangling reference means the course-delete81 /// The course an exam belongs to. A dangling reference means the course-delete
70 /// cascade was violated — surface it loudly as a 500, not a user-facing 404.82 /// cascade was violated — surface it loudly as a 500, not a user-facing 404.
71 async fn course_of(exam: &Exam, db: &Database) -> Result<Course, AppError> {83 async fn course_of(exam: &Exam, db: &Database) -> Result<Course, AppError> {
@@ -372,3 +384,41 @@ async fn remove_result(
372 }384 }
373 Ok(StatusCode::NO_CONTENT)385 Ok(StatusCode::NO_CONTENT)
374 }386 }
387+
388+/// Summary statistics for an exam's graded results. Requires teacher+.
389+#[utoipa::path(
390+ get,
391+ path = "/{id}/statistics",
392+ tag = "exams",
393+ security(("session_cookie" = [])),
394+ params(("id" = String, Path, description = "Exam id")),
395+ responses(
396+ (status = 200, description = "The exam's mark statistics", body = ExamStatisticsResponse),
397+ (status = 401, description = "Not authenticated", body = ErrorResponse),
398+ (status = 403, description = "Requires teacher role or higher", body = ErrorResponse),
399+ (status = 404, description = "Exam not found", body = ErrorResponse),
400+ ),
401+)]
402+async fn exam_statistics(
403+ State(st): State<AppState>,
404+ _teacher: RequireTeacher,
405+ Path(id): Path<String>,
406+) -> Result<Json<ExamStatisticsResponse>, AppError> {
407+ let exam_id = ExamId::from_key(&id);
408+ // Exam must exist — a missing exam is a 404, not an empty statistic.
409+ Exam::read(&exam_id, &st.db)
410+ .await?
411+ .ok_or(AppError::NotFound)?;
412+ let results = ExamResult::list_for_exam(&exam_id, &st.db).await?;
413+
414+ let marks: Vec<i64> = results.iter().map(|r| r.get_mark().as_i64()).collect();
415+ let average =
416+ (!marks.is_empty()).then(|| marks.iter().sum::<i64>() as f64 / marks.len() as f64);
417+ Ok(Json(ExamStatisticsResponse {
418+ exam: exam_id.key().to_string(),
419+ graded: marks.len() as u64,
420+ average,
421+ min: marks.iter().min().copied(),
422+ max: marks.iter().max().copied(),
423+ }))
424+}