use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct TimeDistribution {
pub grid: [[u32; 7]; 24],
pub total_commits: u64,
pub peak_hour: u8,
pub peak_weekday: u8,
pub timezone: String,
pub earliest_date: Option<String>,
pub latest_date: Option<String>,
}
impl TimeDistribution {
pub fn new(timezone: String) -> Self {
Self {
grid: [[0; 7]; 24],
total_commits: 0,
peak_hour: 0,
peak_weekday: 0,
timezone,
earliest_date: None,
latest_date: None,
}
}
pub fn add(&mut self, hour: u8, weekday: u8) {
if hour < 24 && weekday < 7 {
self.grid[hour as usize][weekday as usize] += 1;
self.total_commits += 1;
}
}
pub fn finalize(&mut self) {
let mut max_count = 0u32;
let mut peak_h = 0u8;
let mut peak_w = 0u8;
for (h, row) in self.grid.iter().enumerate() {
for (w, &count) in row.iter().enumerate() {
if count > max_count {
max_count = count;
peak_h = h as u8;
peak_w = w as u8;
}
}
}
self.peak_hour = peak_h;
self.peak_weekday = peak_w;
}
}