|
| 1 | +use std::collections::{btree_map::Entry, BTreeMap}; |
| 2 | + |
| 3 | +use anyhow::Context; |
| 4 | +use chrono::{NaiveDate, Utc}; |
| 5 | +use serde::Serialize; |
| 6 | +use tracing::warn; |
| 7 | + |
| 8 | +use crate::{ |
| 9 | + sheets::{cell_date, cell_string, SheetsClient}, |
| 10 | + Error, |
| 11 | +}; |
| 12 | + |
| 13 | +pub struct MentoringRecords { |
| 14 | + records: BTreeMap<String, MentoringRecord>, |
| 15 | +} |
| 16 | + |
| 17 | +impl MentoringRecords { |
| 18 | + pub fn get(&self, name: &str) -> Option<MentoringRecord> { |
| 19 | + self.records.get(name).cloned() |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Clone, Debug, Serialize)] |
| 24 | +pub struct MentoringRecord { |
| 25 | + pub last_date: NaiveDate, |
| 26 | +} |
| 27 | + |
| 28 | +impl MentoringRecord { |
| 29 | + pub fn is_recent(&self) -> bool { |
| 30 | + let now = Utc::now().date_naive(); |
| 31 | + let time_since = now.signed_duration_since(self.last_date); |
| 32 | + time_since.num_days() <= 14 |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +pub async fn get_mentoring_records( |
| 37 | + client: SheetsClient, |
| 38 | + mentoring_records_sheet_id: &str, |
| 39 | +) -> Result<MentoringRecords, Error> { |
| 40 | + let data = client |
| 41 | + .get(mentoring_records_sheet_id, true, &[]) |
| 42 | + .await |
| 43 | + .map_err(|err| { |
| 44 | + err.with_context(|| { |
| 45 | + format!( |
| 46 | + "Failed to get spreadsheet with ID {}", |
| 47 | + mentoring_records_sheet_id |
| 48 | + ) |
| 49 | + }) |
| 50 | + })?; |
| 51 | + let expected_sheet_title = "Feedback"; |
| 52 | + let sheet = data |
| 53 | + .body |
| 54 | + .sheets |
| 55 | + .into_iter() |
| 56 | + .find(|sheet| { |
| 57 | + sheet |
| 58 | + .properties |
| 59 | + .as_ref() |
| 60 | + .map(|properties| properties.title.as_str()) |
| 61 | + == Some(expected_sheet_title) |
| 62 | + }) |
| 63 | + .ok_or_else(|| { |
| 64 | + Error::Fatal(anyhow::anyhow!( |
| 65 | + "Couldn't find sheet '{}' in spreadsheet with ID {}", |
| 66 | + expected_sheet_title, |
| 67 | + mentoring_records_sheet_id |
| 68 | + )) |
| 69 | + })?; |
| 70 | + |
| 71 | + let mut mentoring_records = MentoringRecords { |
| 72 | + records: BTreeMap::new(), |
| 73 | + }; |
| 74 | + |
| 75 | + for sheet_data in sheet.data { |
| 76 | + if sheet_data.start_column != 0 || sheet_data.start_row != 0 { |
| 77 | + return Err(Error::Fatal(anyhow::anyhow!( |
| 78 | + "Start column and row were {} and {}, expected 0 and 0", |
| 79 | + sheet_data.start_column, |
| 80 | + sheet_data.start_row |
| 81 | + ))); |
| 82 | + } |
| 83 | + |
| 84 | + for (row_number, row) in sheet_data.row_data.into_iter().enumerate() { |
| 85 | + let cells = row.values; |
| 86 | + if cells.len() < 6 { |
| 87 | + warn!( |
| 88 | + "Parsing mentoring data from Google Sheet with ID {}: Not enough columns for row {} - expected at least 6, got {} containing: {}", |
| 89 | + mentoring_records_sheet_id, |
| 90 | + row_number, |
| 91 | + cells.len(), |
| 92 | + format!("{:#?}", cells), |
| 93 | + ); |
| 94 | + continue; |
| 95 | + } |
| 96 | + if row_number == 0 { |
| 97 | + let headings = cells |
| 98 | + .iter() |
| 99 | + .take(6) |
| 100 | + .enumerate() |
| 101 | + .map(|(col_number, cell)| { |
| 102 | + cell_string(cell) |
| 103 | + .with_context(|| format!("Failed to get row 0 column {}", col_number)) |
| 104 | + }) |
| 105 | + .collect::<Result<Vec<_>, _>>()?; |
| 106 | + if headings != ["Name", "Region", "Date", "Staff", "Status", "Notes"] { |
| 107 | + return Err(Error::Fatal(anyhow::anyhow!( |
| 108 | + "Mentoring data sheet contained wrong headings: {}", |
| 109 | + headings.join(", ") |
| 110 | + ))); |
| 111 | + } |
| 112 | + } else { |
| 113 | + if cells[0].effective_value.is_none() { |
| 114 | + break; |
| 115 | + } |
| 116 | + let name = cell_string(&cells[0]) |
| 117 | + .with_context(|| format!("Failed to read name from row {}", row_number + 1))?; |
| 118 | + let date = cell_date(&cells[2]) |
| 119 | + .with_context(|| format!("Failed to parse date from row {}", row_number + 1))?; |
| 120 | + let entry = mentoring_records.records.entry(name); |
| 121 | + match entry { |
| 122 | + Entry::Vacant(entry) => { |
| 123 | + entry.insert(MentoringRecord { last_date: date }); |
| 124 | + } |
| 125 | + Entry::Occupied(mut entry) => { |
| 126 | + if entry.get().last_date < date { |
| 127 | + entry.insert(MentoringRecord { last_date: date }); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + Ok(mentoring_records) |
| 135 | +} |
0 commit comments