From 026915f870783e073e16c31c24f616b8c0a51179 Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Fri, 19 Jul 2024 09:44:12 +0300 Subject: [PATCH] feat: evaluate time tracked recursively --- src/task.rs | 2 +- src/tasks.rs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/task.rs b/src/task.rs index 479ccbd..e192cd3 100644 --- a/src/task.rs +++ b/src/task.rs @@ -82,7 +82,7 @@ impl Task { } /// Total time this task has been active. - /// Todo: Recursive + /// TODO: Consider caching pub(crate) fn time_tracked(&self) -> u64 { let mut total = 0; let mut start: Option = None; diff --git a/src/tasks.rs b/src/tasks.rs index 3749cc6..b1b46db 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -35,6 +35,13 @@ impl Tasks { tasks.iter().filter_map(|id| self.tasks.get(id)).collect() } + /// Total time this task and its subtasks have been active + fn total_time_tracked(&self, task: &EventId) -> u64 { + self.tasks.get(task).map_or(0, |t| { + t.time_tracked() + t.children.iter().map(|e| self.total_time_tracked(e)).sum::() + }) + } + pub(crate) fn set_filter(&mut self, view: Vec) { self.view = view } @@ -62,6 +69,7 @@ impl Tasks { .iter() .map(|p| match p.as_str() { "path" => self.taskpath(Some(task.event.id)), + "ttime" => self.total_time_tracked(&task.event.id).to_string(), prop => task.get(prop).unwrap_or(String::new()), }) .collect::>()