Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/bin/trainee-tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ async fn main() {
"/api/attendance",
get(trainee_tracker::endpoints::fetch_attendance),
)
.route(
"/api/expected-attendance",
get(trainee_tracker::endpoints::expected_attendance),
)
.layer(session_layer)
.with_state(server_state);

Expand Down
12 changes: 2 additions & 10 deletions src/course.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use crate::{
sheets::SheetsClient,
};
use anyhow::Context;
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
use chrono_tz::Tz;
use chrono::{NaiveDate, Utc};
use email_address::EmailAddress;
use futures::future::join_all;
use indexmap::{IndexMap, IndexSet};
Expand Down Expand Up @@ -800,14 +799,7 @@ fn get_trainee_module_attendance(
.collect::<Vec<chrono::NaiveDate>>();
let attendance = match dates.as_slice() {
[date] => {
let start_time = DateTime::<Tz>::from_naive_utc_and_offset(
NaiveDateTime::new(
date.clone(),
NaiveTime::from_hms_opt(10, 00, 00).expect("TODO"),
),
region.timezone().offset_from_utc_date(date),
)
.to_utc();
let start_time = region.class_start_time(date);
let attendance = module_attendance
.attendance
.get(sprint_index)
Expand Down
47 changes: 47 additions & 0 deletions src/endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::{collections::BTreeMap, ops::AddAssign};

use ::octocrab::models::{Author, teams::RequestedTeam};
use anyhow::Context;
use axum::{
Json,
extract::{OriginalUri, Path, State},
response::IntoResponse,
};
use chrono::Utc;
use futures::future::join_all;
use http::HeaderMap;
use indexmap::IndexMap;
Expand Down Expand Up @@ -284,3 +287,47 @@ pub async fn fetch_attendance(
}
Ok(Json(registered_attendance))
}

#[derive(Serialize)]
pub struct ExpectedAttendance {
course: String,
cohort: String,
region: crate::newtypes::Region,
expected_classes: usize,
}

pub async fn expected_attendance(
State(server_state): State<ServerState>,
) -> Json<Vec<ExpectedAttendance>> {
let now = Utc::now();

let mut expected_attendance = Vec::new();
for (course, course_info) in server_state.config.courses {
for (cohort, schedule) in course_info.batches {
let mut region_to_expected_classes: BTreeMap<crate::newtypes::Region, usize> =
BTreeMap::new();
for (_module_name, sprints) in schedule.sprints {
for sprint in sprints {
for (region, date) in sprint {
let start_time = region.class_start_time(&date);
if start_time < now {
region_to_expected_classes
.entry(region)
.or_default()
.add_assign(1);
}
}
}
}
for (region, expected_classes) in region_to_expected_classes {
expected_attendance.push(ExpectedAttendance {
course: course.clone(),
cohort: cohort.clone(),
region,
expected_classes,
})
}
}
}
Json(expected_attendance)
}
13 changes: 13 additions & 0 deletions src/newtypes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{fmt::Display, str::FromStr};

use case_insensitive_string::CaseInsensitiveString;
use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
use email_address::EmailAddress;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -52,4 +53,16 @@ impl Region {
pub fn as_str(&self) -> &str {
self.0.as_str()
}

pub fn class_start_time(&self, date: &NaiveDate) -> DateTime<Utc> {
let offset = self.timezone().offset_from_utc_date(date);
DateTime::<chrono_tz::Tz>::from_naive_utc_and_offset(
NaiveDateTime::new(
*date,
NaiveTime::from_hms_opt(10, 00, 00).expect("Known time failed to parse"),
),
offset,
)
.to_utc()
}
}