exclude pause time in calculations

This commit is contained in:
kageru 2020-04-20 14:54:47 +02:00
parent eacc10d625
commit b2a2d29216

View File

@ -2,11 +2,7 @@ use super::listview::ListView;
use itertools::Itertools; use itertools::Itertools;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::from_reader; use serde_json::from_reader;
use std::collections; use std::{collections, default, fmt, fs, io, iter};
use std::default;
use std::fmt;
use std::fs;
use std::io;
use time::{Duration, OffsetDateTime, Time}; use time::{Duration, OffsetDateTime, Time};
pub struct TimeSheet { pub struct TimeSheet {
@ -16,6 +12,8 @@ pub struct TimeSheet {
pub editing_time: bool, pub editing_time: bool,
} }
const PAUSE_TEXTS: [&str; 3] = ["lunch", "mittag", "pause"];
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
pub struct TimePoint { pub struct TimePoint {
text: String, text: String,
@ -68,10 +66,10 @@ impl TimeSheet {
&self.times[self.selected] &self.times[self.selected]
} }
pub fn time_by_tasks(&self) -> String { fn grouped_times(&self) -> impl Iterator<Item = (String, Duration)> {
self.times self.times
.iter() .iter()
.chain(std::iter::once(&TimePoint::new("end"))) .chain(iter::once(&TimePoint::new("end")))
.tuple_windows() .tuple_windows()
.map(|(prev, next)| (prev.text.clone(), next.time - prev.time)) .map(|(prev, next)| (prev.text.clone(), next.time - prev.time))
// Fold into a map to group by description. // Fold into a map to group by description.
@ -82,21 +80,19 @@ impl TimeSheet {
map map
}) })
.into_iter() .into_iter()
.filter(|(_, duration)| duration.whole_seconds() > 1) .filter(|(text, _)| !PAUSE_TEXTS.contains(&text.as_str()))
}
pub fn time_by_tasks(&self) -> String {
self.grouped_times()
.map(|(text, duration)| format!("{}: {}", text, format_duration(&duration))) .map(|(text, duration)| format!("{}: {}", text, format_duration(&duration)))
.join(" | ") .join(" | ")
} }
pub fn sum_as_str(&self) -> String { pub fn sum_as_str(&self) -> String {
let total = self let total = self
.times .grouped_times()
.iter() .fold(Duration::zero(), |total, (_, d)| total + d);
.map(|tp| tp.time)
.chain(std::iter::once(OffsetDateTime::now_local().time()))
.tuple_windows()
.fold(Duration::zero(), |total, (last, next)| {
total + (next - last)
});
format_duration(&total) format_duration(&total)
} }
} }