Skip to content

Commit

Permalink
feat: add todo history calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Oct 17, 2024
1 parent 2f2359a commit 7d53e18
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 159 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"chartjs",
"chrono",
"combak",
"datelike",
"dicebear",
"docme",
"docmeup",
Expand Down
80 changes: 80 additions & 0 deletions src/add_missing_days.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::types::TodoHistory;
use chrono::{Datelike, Duration, NaiveDate};
use std::collections::HashSet;

pub fn add_missing_days(
mut todo_history_data: Vec<TodoHistory>,
months: u32,
current_todo_count: usize,
) -> Vec<TodoHistory> {
if todo_history_data.is_empty() {
let current_date = chrono::Utc::now().naive_utc().date();
let last_date = current_date
.checked_add_signed(Duration::days(30 * months as i64))
.unwrap_or(current_date);

let mut current_date_iter = current_date;
let mut updated_history = Vec::new();

while current_date_iter <= last_date {
updated_history.push(TodoHistory {
date: current_date_iter.format("%Y-%m-%d").to_string(),
count: current_todo_count,
});
current_date_iter = current_date_iter
.checked_add_signed(Duration::days(1))
.unwrap();
}

return updated_history;
}

let mut existing_dates = HashSet::new();

for entry in &todo_history_data {
let date = NaiveDate::parse_from_str(&entry.date, "%Y-%m-%d")
.expect("Invalid date format in history");
existing_dates.insert(date);
}

todo_history_data.sort_by(|a, b| a.date.cmp(&b.date));

let first_date = NaiveDate::parse_from_str(
&todo_history_data.first().unwrap().date,
"%Y-%m-%d",
)
.expect("Invalid date format in history");

let last_date = first_date
.checked_add_signed(Duration::days(30 * months as i64))
.unwrap_or(first_date);

let mut current_date = first_date;
let mut previous_commit_count = todo_history_data.first().unwrap().count;

while current_date <= last_date {
if !existing_dates.contains(&current_date) {
todo_history_data.push(TodoHistory {
date: current_date.format("%Y-%m-%d").to_string(),
count: previous_commit_count,
});
} else {
let matching_entry = todo_history_data.iter().find(|e| {
let date =
NaiveDate::parse_from_str(&e.date, "%Y-%m-%d").unwrap();
date.year() == current_date.year()
&& date.month() == current_date.month()
});
if let Some(entry) = matching_entry {
previous_commit_count = entry.count;
}
}

current_date =
current_date.checked_add_signed(Duration::days(1)).unwrap();
}

todo_history_data.sort_by(|a, b| b.date.cmp(&a.date));

todo_history_data
}
10 changes: 0 additions & 10 deletions src/get_diff.rs

This file was deleted.

4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod add_missing_days;
pub mod blame;
pub mod check_git_repository;
pub mod copy_dir_recursive;
pub mod exec;
pub mod get_comments;
pub mod get_current_directory;
pub mod get_current_exe_path;
pub mod get_diff;
pub mod get_files_list;
pub mod get_history;
pub mod get_line_from_position;
Expand All @@ -14,4 +14,6 @@ pub mod get_todoctor_version;
pub mod identify_supported_file;
pub mod identify_todo_comment;
pub mod prepare_blame_data;
pub mod remove_duplicate_dates;
pub mod todo_keywords;
pub mod types;
Loading

0 comments on commit 7d53e18

Please sign in to comment.