|
| 1 | +use std::{collections::BTreeMap, ops::AddAssign}; |
| 2 | + |
1 | 3 | use ::octocrab::models::{Author, teams::RequestedTeam}; |
2 | 4 | use anyhow::Context; |
3 | 5 | use axum::{ |
4 | 6 | Json, |
5 | 7 | extract::{OriginalUri, Path, State}, |
6 | 8 | response::IntoResponse, |
7 | 9 | }; |
| 10 | +use chrono::Utc; |
8 | 11 | use futures::future::join_all; |
9 | 12 | use http::HeaderMap; |
10 | 13 | use indexmap::IndexMap; |
@@ -284,3 +287,47 @@ pub async fn fetch_attendance( |
284 | 287 | } |
285 | 288 | Ok(Json(registered_attendance)) |
286 | 289 | } |
| 290 | + |
| 291 | +#[derive(Serialize)] |
| 292 | +pub struct ExpectedAttendance { |
| 293 | + course: String, |
| 294 | + cohort: String, |
| 295 | + region: crate::newtypes::Region, |
| 296 | + expected_classes: usize, |
| 297 | +} |
| 298 | + |
| 299 | +pub async fn expected_attendance( |
| 300 | + State(server_state): State<ServerState>, |
| 301 | +) -> Json<Vec<ExpectedAttendance>> { |
| 302 | + let now = Utc::now(); |
| 303 | + |
| 304 | + let mut expected_attendance = Vec::new(); |
| 305 | + for (course, course_info) in server_state.config.courses { |
| 306 | + for (cohort, schedule) in course_info.batches { |
| 307 | + let mut region_to_expected_classes: BTreeMap<crate::newtypes::Region, usize> = |
| 308 | + BTreeMap::new(); |
| 309 | + for (_module_name, sprints) in schedule.sprints { |
| 310 | + for sprint in sprints { |
| 311 | + for (region, date) in sprint { |
| 312 | + let start_time = region.class_start_time(&date); |
| 313 | + if start_time < now { |
| 314 | + region_to_expected_classes |
| 315 | + .entry(region) |
| 316 | + .or_default() |
| 317 | + .add_assign(1); |
| 318 | + } |
| 319 | + } |
| 320 | + } |
| 321 | + } |
| 322 | + for (region, expected_classes) in region_to_expected_classes { |
| 323 | + expected_attendance.push(ExpectedAttendance { |
| 324 | + course: course.clone(), |
| 325 | + cohort: cohort.clone(), |
| 326 | + region, |
| 327 | + expected_classes, |
| 328 | + }) |
| 329 | + } |
| 330 | + } |
| 331 | + } |
| 332 | + Json(expected_attendance) |
| 333 | +} |
0 commit comments