|
| 1 | +use crate::hours::FileStats; |
| 2 | +use crate::hours::LineStats; |
| 3 | +use crate::hours::WorkByEmail; |
| 4 | +use crate::hours::WorkByPerson; |
| 5 | +use git::bstr::BStr; |
| 6 | +use git_repository as git; |
| 7 | +use itertools::Itertools; |
| 8 | +use std::collections::hash_map::Entry; |
| 9 | +use std::collections::HashMap; |
| 10 | + |
| 11 | +const MINUTES_PER_HOUR: f32 = 60.0; |
| 12 | +pub const HOURS_PER_WORKDAY: f32 = 8.0; |
| 13 | + |
| 14 | +pub fn estimate_hours( |
| 15 | + commits: &[(u32, git::actor::SignatureRef<'static>)], |
| 16 | + stats: &[(u32, FileStats, LineStats)], |
| 17 | +) -> WorkByEmail { |
| 18 | + assert!(!commits.is_empty()); |
| 19 | + const MAX_COMMIT_DIFFERENCE_IN_MINUTES: f32 = 2.0 * MINUTES_PER_HOUR; |
| 20 | + const FIRST_COMMIT_ADDITION_IN_MINUTES: f32 = 2.0 * MINUTES_PER_HOUR; |
| 21 | + |
| 22 | + let hours_for_commits = commits.iter().map(|t| &t.1).rev().tuple_windows().fold( |
| 23 | + 0_f32, |
| 24 | + |hours, (cur, next): (&git::actor::SignatureRef<'_>, &git::actor::SignatureRef<'_>)| { |
| 25 | + let change_in_minutes = (next |
| 26 | + .time |
| 27 | + .seconds_since_unix_epoch |
| 28 | + .saturating_sub(cur.time.seconds_since_unix_epoch)) as f32 |
| 29 | + / MINUTES_PER_HOUR; |
| 30 | + if change_in_minutes < MAX_COMMIT_DIFFERENCE_IN_MINUTES { |
| 31 | + hours + change_in_minutes as f32 / MINUTES_PER_HOUR |
| 32 | + } else { |
| 33 | + hours + (FIRST_COMMIT_ADDITION_IN_MINUTES / MINUTES_PER_HOUR) |
| 34 | + } |
| 35 | + }, |
| 36 | + ); |
| 37 | + |
| 38 | + let author = &commits[0].1; |
| 39 | + let (files, lines) = (!stats.is_empty()) |
| 40 | + .then(|| { |
| 41 | + commits |
| 42 | + .iter() |
| 43 | + .map(|t| &t.0) |
| 44 | + .fold((FileStats::default(), LineStats::default()), |mut acc, id| match stats |
| 45 | + .binary_search_by(|t| t.0.cmp(id)) |
| 46 | + { |
| 47 | + Ok(idx) => { |
| 48 | + let t = &stats[idx]; |
| 49 | + acc.0.add(&t.1); |
| 50 | + acc.1.add(&t.2); |
| 51 | + acc |
| 52 | + } |
| 53 | + Err(_) => acc, |
| 54 | + }) |
| 55 | + }) |
| 56 | + .unwrap_or_default(); |
| 57 | + WorkByEmail { |
| 58 | + name: author.name, |
| 59 | + email: author.email, |
| 60 | + hours: FIRST_COMMIT_ADDITION_IN_MINUTES / 60.0 + hours_for_commits, |
| 61 | + num_commits: commits.len() as u32, |
| 62 | + files, |
| 63 | + lines, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +pub fn deduplicate_identities(persons: &[WorkByEmail]) -> Vec<WorkByPerson> { |
| 68 | + let mut email_to_index = HashMap::<&'static BStr, usize>::with_capacity(persons.len()); |
| 69 | + let mut name_to_index = HashMap::<&'static BStr, usize>::with_capacity(persons.len()); |
| 70 | + let mut out = Vec::<WorkByPerson>::with_capacity(persons.len()); |
| 71 | + for person_by_email in persons { |
| 72 | + match email_to_index.entry(person_by_email.email) { |
| 73 | + Entry::Occupied(email_entry) => { |
| 74 | + out[*email_entry.get()].merge(person_by_email); |
| 75 | + name_to_index.insert(&person_by_email.name, *email_entry.get()); |
| 76 | + } |
| 77 | + Entry::Vacant(email_entry) => match name_to_index.entry(&person_by_email.name) { |
| 78 | + Entry::Occupied(name_entry) => { |
| 79 | + out[*name_entry.get()].merge(person_by_email); |
| 80 | + email_entry.insert(*name_entry.get()); |
| 81 | + } |
| 82 | + Entry::Vacant(name_entry) => { |
| 83 | + let idx = out.len(); |
| 84 | + name_entry.insert(idx); |
| 85 | + email_entry.insert(idx); |
| 86 | + out.push(person_by_email.into()); |
| 87 | + } |
| 88 | + }, |
| 89 | + } |
| 90 | + } |
| 91 | + out |
| 92 | +} |
0 commit comments