Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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 @@ -100,6 +100,10 @@ async fn main() {
"/groups/slack.csv",
get(trainee_tracker::frontend::list_slack_groups_csv),
)
.route(
"/api/attendance",
get(trainee_tracker::endpoints::fetch_attendance),
)
.layer(session_layer)
.with_state(server_state);

Expand Down
68 changes: 68 additions & 0 deletions src/endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::BTreeMap;

use ::octocrab::models::{teams::RequestedTeam, Author};
use anyhow::Context;
use axum::{
Expand All @@ -15,6 +17,7 @@
newtypes::GithubLogin,
octocrab::{all_pages, octocrab},
prs::{fill_in_reviewers, get_prs, PrWithReviews},
register::{get_register, Attendance},
sheets::sheets_client,
Error, ServerState,
};
Expand Down Expand Up @@ -207,3 +210,68 @@
.map(|trainee| trainee.region.clone()),
}))
}

type SprintAttendance = BTreeMap<String, Vec<Attendance>>;
type ModuleAttendance = BTreeMap<String, SprintAttendance>;
type BatchAttendance = BTreeMap<String, ModuleAttendance>;
type CourseAttendance = BTreeMap<String, BatchAttendance>;

#[derive(Serialize)]
pub struct AttendanceResponse {
courses: CourseAttendance,
}

pub async fn fetch_attendance(
session: Session,
State(server_state): State<ServerState>,
OriginalUri(original_uri): OriginalUri,
) -> Result<Json<AttendanceResponse>, Error> {

Check warning on line 228 in src/endpoints.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/trainee-tracker/trainee-tracker/src/endpoints.rs
let all_courses = &server_state.config.courses;
let sheets_client = sheets_client(&session, server_state.clone(), original_uri.clone()).await?;

let mut courses: CourseAttendance = BTreeMap::new();
let mut register_futures = Vec::new();
for (course_name, course_info) in all_courses {
for batch_name in course_info.batches.keys() {
let course_schedule = server_state
.config
.get_course_schedule_with_register_sheet_id(course_name.clone(), batch_name)
.ok_or_else(|| Error::Fatal(anyhow::anyhow!("Course not found: {course_name}")))?;
let register_future = get_register(
sheets_client.clone(),
course_schedule.register_sheet_id.clone(),
course_schedule.course_schedule.start,

Check warning on line 243 in src/endpoints.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/trainee-tracker/trainee-tracker/src/endpoints.rs
course_schedule.course_schedule.end,
);
register_futures.push(async move { (course_name.clone(), batch_name.clone(), register_future.await) });
}
}
let register_info = join_all(register_futures).await;

Check warning on line 250 in src/endpoints.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/trainee-tracker/trainee-tracker/src/endpoints.rs
for (course_name, batch_name, register_result) in register_info {
let register = register_result?;
let modules = register.modules
.into_iter()
.map(|(module_name, sprint_info)|{
(
module_name,
sprint_info
.attendance
.into_iter()
.enumerate()
.map(|(sprint_number, sprint_info)| {
(
format!("Sprint-{}", sprint_number + 1),

Check warning on line 264 in src/endpoints.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/trainee-tracker/trainee-tracker/src/endpoints.rs
sprint_info.into_values().collect(),
)
}).collect()
)
})
.collect();
courses
.entry(course_name)

Check warning on line 272 in src/endpoints.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/trainee-tracker/trainee-tracker/src/endpoints.rs
.or_default()
.insert(batch_name, modules);
}
Ok(Json(AttendanceResponse { courses }))
}
3 changes: 2 additions & 1 deletion src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Context;
use chrono::{DateTime, NaiveDate, Utc};
use email_address::EmailAddress;
use indexmap::IndexMap;
use serde::Serialize;
use sheets::types::{CellData, GridData};
use tracing::warn;

Expand All @@ -24,7 +25,7 @@ pub struct ModuleAttendance {
pub attendance: Vec<IndexMap<EmailAddress, Attendance>>,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize)]
pub struct Attendance {
pub name: String,
pub email: EmailAddress,
Expand Down
Loading