fix: obtain children on the fly

This commit is contained in:
xeruf 2024-09-22 16:24:07 +02:00
parent 7755967a7a
commit 753afad2fd
2 changed files with 25 additions and 18 deletions

View File

@ -329,7 +329,7 @@ async fn main() -> Result<()> {
trace!("All Root Tasks:\n{}", relays.iter().map(|(url, tasks)| trace!("All Root Tasks:\n{}", relays.iter().map(|(url, tasks)|
format!("{}: [{}]", format!("{}: [{}]",
url.as_ref().map(ToString::to_string).unwrap_or(LOCAL_RELAY_NAME.to_string()), url.as_ref().map(ToString::to_string).unwrap_or(LOCAL_RELAY_NAME.to_string()),
tasks.children_of(None).map(|id| tasks.get_task_title(id)).join("; "))).join("\n")); tasks.children_for(None).map(|task| tasks.get_task_title(task.get_id())).join("; "))).join("\n"));
println!(); println!();
let tasks = relays.get(&selected_relay).unwrap(); let tasks = relays.get(&selected_relay).unwrap();
let prompt = format!( let prompt = format!(

View File

@ -1,4 +1,4 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::io::{stdout, Error, Write}; use std::io::{stdout, Error, Write};
use std::iter::{empty, once, FusedIterator}; use std::iter::{empty, once, FusedIterator};
@ -265,13 +265,13 @@ impl Tasks {
} }
fn total_progress(&self, id: &EventId) -> Option<f32> { fn total_progress(&self, id: &EventId) -> Option<f32> {
self.get_by_id(id).and_then(|t| match t.pure_state() { self.get_by_id(id).and_then(|task| match task.pure_state() {
State::Closed => None, State::Closed => None,
State::Done => Some(1.0), State::Done => Some(1.0),
_ => { _ => {
let mut sum = 0f32; let mut sum = 0f32;
let mut count = 0; let mut count = 0;
for prog in t.children.iter().filter_map(|e| self.total_progress(e)) { for prog in self.children_ids_for(task.get_id()).filter_map(|e| self.total_progress(e)) {
sum += prog; sum += prog;
count += 1; count += 1;
} }
@ -330,7 +330,7 @@ impl Tasks {
fn resolve_tasks<'a>( fn resolve_tasks<'a>(
&'a self, &'a self,
iter: impl Iterator<Item=&'a EventId>, iter: impl Iterator<Item=&'a Task>,
sparse: bool, sparse: bool,
) -> Vec<&'a Task> { ) -> Vec<&'a Task> {
self.resolve_tasks_rec(iter, sparse, self.depth) self.resolve_tasks_rec(iter, sparse, self.depth)
@ -338,15 +338,14 @@ impl Tasks {
fn resolve_tasks_rec<'a>( fn resolve_tasks_rec<'a>(
&'a self, &'a self,
iter: impl Iterator<Item=&'a EventId>, iter: impl Iterator<Item=&'a Task>,
sparse: bool, sparse: bool,
depth: usize, depth: usize,
) -> Vec<&'a Task> { ) -> Vec<&'a Task> {
iter.filter_map(|id| self.get_by_id(id)) iter.flat_map(move |task| {
.flat_map(move |task| {
let new_depth = depth - 1; let new_depth = depth - 1;
if new_depth > 0 { if new_depth > 0 {
let mut children = self.resolve_tasks_rec(task.children.iter(), sparse, new_depth); let mut children = self.resolve_tasks_rec(self.children_of(&task), sparse, new_depth);
if !children.is_empty() { if !children.is_empty() {
if !sparse { if !sparse {
children.push(task); children.push(task);
@ -381,10 +380,18 @@ impl Tasks {
self.get_position_ref().and_then(|id| self.get_by_id(id)) self.get_position_ref().and_then(|id| self.get_by_id(id))
} }
pub(crate) fn children_of<'a>(&'a self, id: Option<&'a EventId>) -> impl Iterator<Item=&EventId> + 'a { pub(crate) fn children_of<'a>(&'a self, task: &'a Task) -> impl Iterator<Item=&Task> + 'a {
self.children_for(Some(task.get_id()))
}
pub(crate) fn children_for<'a>(&'a self, id: Option<&'a EventId>) -> impl Iterator<Item=&Task> + 'a {
self.tasks self.tasks
.values() .values()
.filter(move |t| t.parent_id() == id) .filter(move |t| t.parent_id() == id)
}
pub(crate) fn children_ids_for<'a>(&'a self, id: &'a EventId) -> impl Iterator<Item=&EventId> + 'a {
self.children_for(Some(id))
.map(|t| t.get_id()) .map(|t| t.get_id())
} }
@ -401,10 +408,10 @@ impl Tasks {
} }
pub(crate) fn filtered_tasks<'a>(&'a self, position: Option<&'a EventId>, sparse: bool) -> Vec<&'a Task> { pub(crate) fn filtered_tasks<'a>(&'a self, position: Option<&'a EventId>, sparse: bool) -> Vec<&'a Task> {
let mut current = self.resolve_tasks(self.children_of(position), sparse); let mut current = self.resolve_tasks(self.children_for(position), sparse);
if current.is_empty() { if current.is_empty() {
if !self.tags.is_empty() { if !self.tags.is_empty() {
let mut children = self.children_of(self.get_position_ref()).peekable(); let mut children = self.children_for(self.get_position_ref()).peekable();
if children.peek().is_some() { if children.peek().is_some() {
current = self.resolve_tasks_rec(children, true, 9); current = self.resolve_tasks_rec(children, true, 9);
if sparse { if sparse {
@ -510,15 +517,16 @@ impl Tasks {
} }
fn get_property(&self, task: &Task, str: &str) -> String { fn get_property(&self, task: &Task, str: &str) -> String {
let mut children = self.children_of(task).peekable();
let progress = let progress =
self.total_progress(task.get_id()) self.total_progress(task.get_id())
.filter(|_| !task.children.is_empty()); .filter(|_| children.peek().is_some());
let prog_string = progress.map_or(String::new(), |p| format!("{:2.0}%", p * 100.0)); let prog_string = progress.map_or(String::new(), |p| format!("{:2.0}%", p * 100.0));
match str { match str {
"subtasks" => { "subtasks" => {
let mut total = 0; let mut total = 0;
let mut done = 0; let mut done = 0;
for subtask in task.children.iter().filter_map(|id| self.get_by_id(id)) { for subtask in children {
let state = subtask.pure_state(); let state = subtask.pure_state();
total += &(state != State::Closed).into(); total += &(state != State::Closed).into();
done += &(state == State::Done).into(); done += &(state == State::Done).into();
@ -816,10 +824,9 @@ impl Tasks {
let mut tags = Vec::with_capacity(2); let mut tags = Vec::with_capacity(2);
tags.push(self.make_event_tag_from_id(*pos, MARKER_PARENT)); tags.push(self.make_event_tag_from_id(*pos, MARKER_PARENT));
self.get_by_id(pos) self.get_by_id(pos)
.map(|t| { .map(|task| {
if t.pure_state() == State::Procedure { if task.pure_state() == State::Procedure {
t.children.iter() self.children_of(task)
.filter_map(|id| self.get_by_id(id))
.max() .max()
.map(|t| tags.push(self.make_event_tag(&t.event, MARKER_DEPENDS))); .map(|t| tags.push(self.make_event_tag(&t.event, MARKER_DEPENDS)));
} }