feat: revamp time tracking with own kind
This commit is contained in:
parent
484c05dbee
commit
5c7793f4a3
16
src/main.rs
16
src/main.rs
|
@ -19,8 +19,8 @@ use crate::tasks::Tasks;
|
||||||
mod task;
|
mod task;
|
||||||
mod tasks;
|
mod tasks;
|
||||||
|
|
||||||
static TASK_KIND: u64 = 1621;
|
const TASK_KIND: u64 = 1621;
|
||||||
static TRACKING_KIND: u64 = 1650;
|
const TRACKING_KIND: u64 = 1650;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct EventSender {
|
struct EventSender {
|
||||||
|
@ -33,6 +33,9 @@ impl EventSender {
|
||||||
or_print(self.tx.send(event.clone()));
|
or_print(self.tx.send(event.clone()));
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
pub(crate) fn pubkey(&self) -> PublicKey {
|
||||||
|
self.keys.public_key()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn or_print<T, U: Display>(result: Result<T, U>) -> Option<T> {
|
fn or_print<T, U: Display>(result: Result<T, U>) -> Option<T> {
|
||||||
|
@ -366,14 +369,7 @@ async fn main() {
|
||||||
}
|
}
|
||||||
println!();
|
println!();
|
||||||
|
|
||||||
// TODO optionally continue
|
tasks.move_to(None);
|
||||||
tasks.update_state("", |t| {
|
|
||||||
if t.pure_state() == State::Active {
|
|
||||||
Some(State::Open)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
});
|
|
||||||
drop(tasks);
|
drop(tasks);
|
||||||
|
|
||||||
info!("Submitting pending changes...");
|
info!("Submitting pending changes...");
|
||||||
|
|
25
src/task.rs
25
src/task.rs
|
@ -102,28 +102,6 @@ impl Task {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Total time this task has been active.
|
|
||||||
/// TODO: Consider caching
|
|
||||||
pub(crate) fn time_tracked(&self) -> u64 {
|
|
||||||
let mut total = 0;
|
|
||||||
let mut start: Option<Timestamp> = None;
|
|
||||||
for state in self.states() {
|
|
||||||
match state.state {
|
|
||||||
State::Active => start = start.or(Some(state.time)),
|
|
||||||
_ => {
|
|
||||||
if let Some(stamp) = start {
|
|
||||||
total += (state.time - stamp).as_u64();
|
|
||||||
start = None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Some(start) = start {
|
|
||||||
total += (Timestamp::now() - start).as_u64();
|
|
||||||
}
|
|
||||||
total
|
|
||||||
}
|
|
||||||
|
|
||||||
fn filter_tags<P>(&self, predicate: P) -> Option<String>
|
fn filter_tags<P>(&self, predicate: P) -> Option<String>
|
||||||
where
|
where
|
||||||
P: FnMut(&&Tag) -> bool,
|
P: FnMut(&&Tag) -> bool,
|
||||||
|
@ -143,9 +121,6 @@ impl Task {
|
||||||
"parentid" => self.parent_id().map(|i| i.to_string()),
|
"parentid" => self.parent_id().map(|i| i.to_string()),
|
||||||
"state" => self.state().map(|s| s.to_string()),
|
"state" => self.state().map(|s| s.to_string()),
|
||||||
"name" => Some(self.event.content.clone()),
|
"name" => Some(self.event.content.clone()),
|
||||||
"time" => Some(self.time_tracked().div(60))
|
|
||||||
.filter(|t| t > &0)
|
|
||||||
.map(|t| format!("{}m", t)),
|
|
||||||
"desc" => self.descriptions().last().cloned(),
|
"desc" => self.descriptions().last().cloned(),
|
||||||
"description" => Some(self.descriptions().join(" ")),
|
"description" => Some(self.descriptions().join(" ")),
|
||||||
"hashtags" => self.filter_tags(|tag| {
|
"hashtags" => self.filter_tags(|tag| {
|
||||||
|
|
150
src/tasks.rs
150
src/tasks.rs
|
@ -1,18 +1,17 @@
|
||||||
use std::collections::{BTreeSet, HashMap};
|
use std::collections::{BTreeSet, HashMap};
|
||||||
use std::fmt::{Display, Formatter, write};
|
|
||||||
use std::io::{Error, stdout, Write};
|
use std::io::{Error, stdout, Write};
|
||||||
use std::iter::{once, Sum};
|
use std::iter::once;
|
||||||
use std::ops::Add;
|
use std::ops::{Div, Rem};
|
||||||
|
|
||||||
use chrono::{Local, TimeZone};
|
use chrono::{Local, TimeZone};
|
||||||
use chrono::LocalResult::Single;
|
use chrono::LocalResult::Single;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use log::{debug, error, info, trace, warn};
|
use log::{debug, error, info, trace, warn};
|
||||||
use nostr_sdk::{Event, EventBuilder, EventId, Keys, Kind, Tag};
|
use nostr_sdk::{Event, EventBuilder, EventId, Kind, PublicKey, Tag, Timestamp};
|
||||||
use nostr_sdk::Tag::Hashtag;
|
use nostr_sdk::Tag::Hashtag;
|
||||||
|
|
||||||
use crate::{EventSender, TASK_KIND};
|
use crate::{EventSender, TASK_KIND, TRACKING_KIND};
|
||||||
use crate::task::{State, Task};
|
use crate::task::{State, Task};
|
||||||
|
|
||||||
type TaskMap = HashMap<EventId, Task>;
|
type TaskMap = HashMap<EventId, Task>;
|
||||||
|
@ -20,6 +19,8 @@ type TaskMap = HashMap<EventId, Task>;
|
||||||
pub(crate) struct Tasks {
|
pub(crate) struct Tasks {
|
||||||
/// The Tasks
|
/// The Tasks
|
||||||
tasks: TaskMap,
|
tasks: TaskMap,
|
||||||
|
/// History of active tasks by PubKey
|
||||||
|
history: HashMap<PublicKey, BTreeSet<Event>>,
|
||||||
/// The task properties currently visible
|
/// The task properties currently visible
|
||||||
pub(crate) properties: Vec<String>,
|
pub(crate) properties: Vec<String>,
|
||||||
/// Negative: Only Leaf nodes
|
/// Negative: Only Leaf nodes
|
||||||
|
@ -43,6 +44,7 @@ impl Tasks {
|
||||||
pub(crate) fn from(sender: EventSender) -> Self {
|
pub(crate) fn from(sender: EventSender) -> Self {
|
||||||
Tasks {
|
Tasks {
|
||||||
tasks: Default::default(),
|
tasks: Default::default(),
|
||||||
|
history: Default::default(),
|
||||||
properties: vec![
|
properties: vec![
|
||||||
"state".into(),
|
"state".into(),
|
||||||
"progress".into(),
|
"progress".into(),
|
||||||
|
@ -74,15 +76,74 @@ impl Tasks {
|
||||||
self.position
|
self.position
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Total time this task and its subtasks have been active
|
/// Ids of all subtasks found for id, including itself
|
||||||
fn total_time_tracked(&self, id: &EventId) -> u64 {
|
fn get_subtasks(&self, id: EventId) -> Vec<EventId> {
|
||||||
self.get_by_id(id).map_or(0, |t| {
|
let mut children = Vec::with_capacity(32);
|
||||||
t.time_tracked()
|
let mut index = 0;
|
||||||
+ t.children
|
|
||||||
.iter()
|
children.push(id);
|
||||||
.map(|e| self.total_time_tracked(e))
|
while index < children.len() {
|
||||||
.sum::<u64>()
|
self.tasks.get(&children[index]).map(|t| {
|
||||||
})
|
children.reserve(t.children.len());
|
||||||
|
for child in t.children.iter() {
|
||||||
|
children.push(child.clone());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
children
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Total time tracked on this task by the current user.
|
||||||
|
pub(crate) fn time_tracked(&self, id: &EventId) -> u64 {
|
||||||
|
let mut total = 0;
|
||||||
|
let mut start: Option<Timestamp> = None;
|
||||||
|
for event in self.history.get(&self.sender.pubkey()).into_iter().flatten() {
|
||||||
|
match event.tags.first() {
|
||||||
|
Some(Tag::Event {
|
||||||
|
event_id,
|
||||||
|
..
|
||||||
|
}) if event_id == id => {
|
||||||
|
start = start.or(Some(event.created_at))
|
||||||
|
}
|
||||||
|
_ => if let Some(stamp) = start {
|
||||||
|
total += (event.created_at - stamp).as_u64();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(start) = start {
|
||||||
|
total += (Timestamp::now() - start).as_u64();
|
||||||
|
}
|
||||||
|
total
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Total time tracked on this task and its subtasks by all users.
|
||||||
|
/// TODO needs testing!
|
||||||
|
fn total_time_tracked(&self, id: EventId) -> u64 {
|
||||||
|
let mut total = 0;
|
||||||
|
|
||||||
|
let children = self.get_subtasks(id);
|
||||||
|
for user in self.history.values() {
|
||||||
|
let mut start: Option<Timestamp> = None;
|
||||||
|
for event in user {
|
||||||
|
match event.tags.first() {
|
||||||
|
Some(Tag::Event {
|
||||||
|
event_id,
|
||||||
|
..
|
||||||
|
}) if children.contains(event_id) => {
|
||||||
|
start = start.or(Some(event.created_at))
|
||||||
|
}
|
||||||
|
_ => if let Some(stamp) = start {
|
||||||
|
total += (event.created_at - stamp).as_u64();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(start) = start {
|
||||||
|
total += (Timestamp::now() - start).as_u64();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
total
|
||||||
}
|
}
|
||||||
|
|
||||||
fn total_progress(&self, id: &EventId) -> Option<f32> {
|
fn total_progress(&self, id: &EventId) -> Option<f32> {
|
||||||
|
@ -244,7 +305,7 @@ impl Tasks {
|
||||||
}
|
}
|
||||||
_ => state.time.to_human_datetime(),
|
_ => state.time.to_human_datetime(),
|
||||||
},
|
},
|
||||||
t.time_tracked() / 60
|
self.time_tracked(t.get_id()) / 60
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
writeln!(lock, "{}", t.descriptions().join("\n"))?;
|
writeln!(lock, "{}", t.descriptions().join("\n"))?;
|
||||||
|
@ -278,14 +339,8 @@ impl Tasks {
|
||||||
.map_or(String::new(), |p| format!("{:2}%", p * 100.0)),
|
.map_or(String::new(), |p| format!("{:2}%", p * 100.0)),
|
||||||
"path" => self.get_task_path(Some(task.event.id)),
|
"path" => self.get_task_path(Some(task.event.id)),
|
||||||
"rpath" => self.relative_path(task.event.id),
|
"rpath" => self.relative_path(task.event.id),
|
||||||
"rtime" => {
|
"time" => display_time("MMMm", self.time_tracked(task.get_id())),
|
||||||
let time = self.total_time_tracked(&task.event.id);
|
"rtime" => display_time("HH:MMm", self.total_time_tracked(*task.get_id())),
|
||||||
if time > 60 {
|
|
||||||
format!("{:02}:{:02}", time / 3600, time / 60 % 60)
|
|
||||||
} else {
|
|
||||||
String::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
prop => task.get(prop).unwrap_or(String::new()),
|
prop => task.get(prop).unwrap_or(String::new()),
|
||||||
})
|
})
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
|
@ -322,21 +377,15 @@ impl Tasks {
|
||||||
if id == self.position {
|
if id == self.position {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// TODO: erases previous state comment - do not track active via state
|
|
||||||
self.update_state("", |s| {
|
|
||||||
if s.pure_state() == State::Active {
|
|
||||||
Some(State::Open)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
});
|
|
||||||
self.position = id;
|
self.position = id;
|
||||||
self.update_state("", |s| {
|
self.sender.submit(
|
||||||
if s.pure_state() == State::Open {
|
EventBuilder::new(
|
||||||
Some(State::Active)
|
Kind::from(TRACKING_KIND),
|
||||||
} else {
|
"",
|
||||||
None
|
id.iter().map(|id| Tag::event(id.clone())),
|
||||||
}
|
)
|
||||||
|
).map(|e| {
|
||||||
|
self.add(e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -373,10 +422,14 @@ impl Tasks {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add(&mut self, event: Event) {
|
pub(crate) fn add(&mut self, event: Event) {
|
||||||
if event.kind.as_u64() == 1621 {
|
match event.kind.as_u64() {
|
||||||
self.add_task(event)
|
TASK_KIND => self.add_task(event),
|
||||||
} else {
|
TRACKING_KIND =>
|
||||||
self.add_prop(&event)
|
match self.history.get_mut(&event.pubkey) {
|
||||||
|
Some(c) => { c.insert(event); }
|
||||||
|
None => { self.history.insert(event.pubkey, BTreeSet::from([event])); }
|
||||||
|
},
|
||||||
|
_ => self.add_prop(&event),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -391,7 +444,7 @@ impl Tasks {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_prop(&mut self, event: &Event) {
|
fn add_prop(&mut self, event: &Event) {
|
||||||
self.referenced_tasks(&event, |t| {
|
self.referenced_tasks(&event, |t| {
|
||||||
t.props.insert(event.clone());
|
t.props.insert(event.clone());
|
||||||
});
|
});
|
||||||
|
@ -445,8 +498,18 @@ impl Tasks {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn display_time(format: &str, secs: u64) -> String {
|
||||||
|
Some(secs / 60)
|
||||||
|
.filter(|t| t > &0)
|
||||||
|
.map_or(String::new(), |mins| format
|
||||||
|
.replace("HH", &format!("{:02}", mins.div(60)))
|
||||||
|
.replace("MM", &format!("{:02}", mins.rem(60)))
|
||||||
|
.replace("MMM", &format!("{:3}", mins)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn join_tasks<'a>(
|
pub(crate) fn join_tasks<'a>(
|
||||||
iter: impl Iterator<Item = &'a Task>,
|
iter: impl Iterator<Item=&'a Task>,
|
||||||
include_last_id: bool,
|
include_last_id: bool,
|
||||||
) -> Option<String> {
|
) -> Option<String> {
|
||||||
let tasks: Vec<&Task> = iter.collect();
|
let tasks: Vec<&Task> = iter.collect();
|
||||||
|
@ -489,6 +552,7 @@ impl<'a> Iterator for ParentIterator<'a> {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_depth() {
|
fn test_depth() {
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
|
use nostr_sdk::Keys;
|
||||||
|
|
||||||
let (tx, _rx) = mpsc::channel();
|
let (tx, _rx) = mpsc::channel();
|
||||||
let mut tasks = Tasks::from(EventSender {
|
let mut tasks = Tasks::from(EventSender {
|
||||||
|
|
Loading…
Reference in New Issue