Skip to content

Update attendance formatting #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
2 changes: 2 additions & 0 deletions src/graphql/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub struct Member {
pub struct AttendanceRecord {
pub name: String,
pub year: i32,
#[serde(rename = "groupId")]
pub group_id: i32,
#[serde(rename = "isPresent")]
pub is_present: bool,
#[serde(rename = "timeIn")]
Expand Down
1 change: 1 addition & 0 deletions src/graphql/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ pub async fn fetch_attendance() -> anyhow::Result<Vec<AttendanceRecord>> {
attendanceByDate(date: "{}") {{
name,
year,
groupId,
isPresent,
timeIn,
}}
Expand Down
48 changes: 35 additions & 13 deletions src/tasks/lab_attendance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,46 @@ fn format_attendance_list(title: &str, list: &[AttendanceRecord]) -> String {
);
}

let mut by_year: HashMap<i32, Vec<&str>> = HashMap::new();
let mut by_group: HashMap<i32, HashMap<i32, Vec<&str>>> = HashMap::new();
for record in list {
if record.year >= 1 && record.year <= 3 {
by_year.entry(record.year).or_default().push(&record.name);
}
by_group
.entry(record.group_id)
.or_default()
.entry(record.year)
.or_default()
.push(&record.name);
}

let mut result = format!("# {}\n", title);

for year in 1..=3 {
if let Some(names) = by_year.get(&year) {
if !names.is_empty() {
result.push_str(&format!("### Year {}\n", year));

for name in names {
result.push_str(&format!("- {}\n", name));
let mut group_ids: Vec<i32> = by_group.keys().cloned().collect();
group_ids.sort();

for group_id in group_ids {
result.push_str(&format!("## Group {}\n", group_id));

let by_year = &by_group[&group_id];
let mut years: Vec<i32> = by_year.keys().cloned().collect();
years.sort();

let mut group_counts: Vec<(i32, usize)> = by_group
.iter()
.map(|(&group_id, year_map)| {
let total_count = year_map.values().map(|names| names.len()).sum();
(group_id, total_count)
})
.collect();
group_counts.sort_by(|a, b| b.1.cmp(&a.1));

for year in 1..=3 {
if let Some(names) = by_year.get(&year) {
if !names.is_empty() {
let mut sorted_names = names.clone();
sorted_names.sort();

for name in sorted_names {
result.push_str(&format!("- {}\n", name));
}
}
result.push('\n');
}
}
}
Expand Down