Summary statistics for graded results
Browse files at this commiteb282a08846f91620b0c8d7569574ece93fc4a0fParent: ebc1bd2- feat
- summary statistics for graded results
1 changed file +50 -0
- src/web/exams.rs +50 -0
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-delete | 81 | /// 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 | +} | ||