list: compute running and today

This commit is contained in:
Abraham Toriz 2021-07-15 13:11:55 -05:00
parent b531f8ca20
commit 54bfa8ce8f
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
2 changed files with 28 additions and 6 deletions

View File

@ -2,7 +2,7 @@ use std::convert::TryFrom;
use std::io::Write; use std::io::Write;
use clap::ArgMatches; use clap::ArgMatches;
use chrono::{Utc, Duration}; use chrono::{Utc, Duration, Local};
use itertools::Itertools; use itertools::Itertools;
use crate::error::{Error, Result}; use crate::error::{Error, Result};
@ -41,6 +41,7 @@ impl<'a> Command<'a> for ListCommand {
{ {
// no sheet, list sheets // no sheet, list sheets
let now = Utc::now(); let now = Utc::now();
let today = Local::now().date().and_hms(0, 0, 0).with_timezone(&Utc);
let mut sheets = if args.all { let mut sheets = if args.all {
db.entries_all_visible(None, None)? db.entries_all_visible(None, None)?
} else { } else {
@ -48,6 +49,7 @@ impl<'a> Command<'a> for ListCommand {
}; };
let current = db.current_sheet()?.unwrap_or("".into()); let current = db.current_sheet()?.unwrap_or("".into());
let last = db.last_sheet()?.unwrap_or("".into());
sheets.sort_unstable_by_key(|e| e.sheet.clone()); sheets.sort_unstable_by_key(|e| e.sheet.clone());
@ -56,14 +58,28 @@ impl<'a> Command<'a> for ListCommand {
.group_by(|e| e.sheet.clone()) .group_by(|e| e.sheet.clone())
.into_iter() .into_iter()
.map(|(key, group)| { .map(|(key, group)| {
let entries: Vec<_> = group.into_iter().collect();
( (
if current == key { "*".into() } else { "".into() }, if current == key {
"*".into()
} else {
if last == key { "-".into() } else { "".into() }
},
key, key,
"running".into(),
"today".into(), format_duration(
now - entries.iter().filter(|e| e.end.is_none()).next().map(|e| e.end).flatten().unwrap_or(now)
),
format_duration(
entries.iter().filter(|e| e.start > today).fold(Duration::seconds(0), |acc, e| {
acc + (e.end.unwrap_or(now) - e.start)
})
),
// total // total
format_duration(group.fold(Duration::seconds(0), |acc, e| { format_duration(entries.into_iter().fold(Duration::seconds(0), |acc, e| {
acc + (e.end.unwrap_or(now) - e.start) acc + (e.end.unwrap_or(now) - e.start)
})), })),
) )
@ -81,7 +97,7 @@ impl<'a> Command<'a> for ListCommand {
Col::min_width(9).and_alignment(Right), Col::min_width(9).and_alignment(Right),
// today // today
Col::min_width(7).and_alignment(Right), Col::min_width(9).and_alignment(Right),
// accumulated // accumulated
Col::min_width(12).and_alignment(Right), Col::min_width(12).and_alignment(Right),

View File

@ -147,6 +147,12 @@ pub trait Database {
Ok(results.into_iter().next().map(|m| m.value)) Ok(results.into_iter().next().map(|m| m.value))
} }
fn last_sheet(&self) -> Result<Option<String>> {
let results = self.meta_query("select * from meta where key='last_sheet'", &[])?;
Ok(results.into_iter().next().map(|m| m.value))
}
fn set_current_sheet(&mut self, sheet: &str) -> Result<()> { fn set_current_sheet(&mut self, sheet: &str) -> Result<()> {
self.execute("INSERT INTO meta (key, value) VALUES ('current_sheet', ?1)", &[&sheet])?; self.execute("INSERT INTO meta (key, value) VALUES ('current_sheet', ?1)", &[&sheet])?;