1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
use chrono::prelude::*;
use chrono::Duration;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fs::{File, OpenOptions};
use std::ops::Add;
use std::path::Path;
use std::{
collections::BTreeMap,
io::{BufReader, Read, Write},
};
use crate::errors::*;
use crate::storage::WorkStorage;
fn nanoseconds(_dur: &Duration) -> i32 {
0i32
}
#[derive(Serialize, Deserialize)]
#[serde(remote = "Duration")]
struct ChronoDuration {
#[serde(getter = "Duration::num_seconds")]
secs: i64,
#[serde(getter = "nanoseconds")]
nanos: i32,
}
impl From<ChronoDuration> for Duration {
fn from(def: ChronoDuration) -> Duration {
Duration::seconds(def.secs)
}
}
impl From<Duration> for DurationDef {
fn from(dur: Duration) -> DurationDef {
Self { inner: dur }
}
}
impl From<&DurationDef> for Duration {
fn from(dur: &DurationDef) -> Self {
dur.inner
}
}
impl From<DurationDef> for Duration {
fn from(dur: DurationDef) -> Self {
dur.inner
}
}
impl ToString for DurationDef {
fn to_string(&self) -> String {
self.inner.to_string()
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Copy)]
pub(crate) struct DurationDef {
#[serde(flatten)]
#[serde(with = "ChronoDuration")]
inner: Duration,
}
impl Add for DurationDef {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
let dur = self.inner + rhs.into();
dur.into()
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct Config {
pub month_stats: u8,
pub daily_hours: Option<u8>,
}
impl Default for Config {
fn default() -> Self {
Self {
month_stats: 2,
daily_hours: None,
}
}
}
impl Default for &Config {
fn default() -> Self {
&Config {
month_stats: 2,
daily_hours: None,
}
}
}
#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub(crate) struct TimeBalance {
start: Option<DateTime<Utc>>,
breaking: Option<DateTime<Utc>>,
breaks: Vec<(DateTime<Utc>, DurationDef)>,
#[serde(skip_serializing_if = "Option::is_none")]
pub config: Option<Config>,
#[serde(rename = "account")]
time_account: BTreeMap<DateTime<Utc>, DurationDef>,
}
impl TimeBalance {
fn new() -> Self {
Self {
time_account: BTreeMap::new(),
start: None,
breaking: None,
config: None,
breaks: Vec::new(),
}
}
pub(crate) fn reset(&mut self) {
self.start = None;
self.breaks.clear();
}
pub(crate) fn cancel(&mut self) -> Result<()> {
match self.breaking {
None => self
.start
.map(|_| {
self.start = None;
})
.ok_or_else(|| eyre!(usage_err!("Nothing to cancel"))),
Some(_) => {
self.breaking = None;
Ok(())
}
}
}
pub(crate) fn start(&mut self, time: DateTime<Utc>) -> Result<(), DateTime<Utc>> {
match self.start {
None => {
self.start = Some(time);
Ok(())
}
Some(s) => Err(s),
}
}
pub(crate) fn stop(&mut self, time: DateTime<Utc>) -> Result<Duration> {
let start = self
.start
.ok_or_else(|| usage_err!("You did not start working"))?;
if let Some(b) = self.breaking {
bail!(usage_err!(
"You're on a break since {}, won't stop your current work.",
b.time().format("%H:%M")
));
}
let breaks = self.accumulate_breaks();
let duration = time
.signed_duration_since(start)
.checked_sub(&breaks)
.ok_or_else(|| usage_err!("Your break was longer than your work"))?;
self.insert(time, duration.into());
self.reset();
Ok(duration)
}
pub(crate) fn accumulate_breaks(&self) -> Duration {
self.breaks
.iter()
.fold(Duration::seconds(0), |acc, b| acc + b.1.into())
}
pub(crate) fn get_breaks(&self) -> Vec<(DateTime<Utc>, Duration)> {
self.breaks.iter().map(|(s, d)| (*s, d.into())).collect()
}
pub(crate) fn start_break(&mut self, time: DateTime<Utc>) -> Result<Duration> {
self.start
.ok_or_else(|| {
eyre!(usage_err!(
"You're not tracking your work so you can't take a break"
))
})
.map(|s| {
if self.breaking.is_none() {
self.breaking = Some(time);
Some(time.signed_duration_since(s))
} else {
None
}
})?
.ok_or_else(|| eyre!(usage_err!("You're already on a break")))
}
pub(crate) fn finish_break(&mut self, time: DateTime<Utc>) -> Result<Duration> {
self.start
.ok_or_else(|| usage_err!("You can't break if you haven't started."))?;
let break_start = self
.breaking
.ok_or_else(|| usage_err!("You're not on a break right now."))?;
let dur = time.signed_duration_since(break_start);
self.breaks.push((break_start, dur.into()));
self.breaking = None;
Ok(dur)
}
fn range(
&self,
lower: DateTime<Utc>,
upper: DateTime<Utc>,
) -> impl Iterator<Item = (&DateTime<Utc>, &DurationDef)> {
let range = lower..upper;
log::trace!("{:?} in {:?}", &range, &self.time_account);
self.time_account.range(range)
}
pub fn month_range(
&self,
year: i32,
month: Month,
) -> Result<impl Iterator<Item = (&DateTime<Utc>, &DurationDef)>> {
log::trace!("Range for month {:?}", month);
let current = Utc
.with_ymd_and_hms(year, month.number_from_month(), 1, 0, 0, 0)
.latest()
.ok_or(eyre!("Could not construct range"))?;
let days_in_m = if month.number_from_month() == 12 {
Utc.with_ymd_and_hms(year + 1, month.succ().number_from_month(), 1, 0, 0, 0)
.earliest()
.ok_or(eyre!("Could not construct range"))?
.signed_duration_since(current)
.num_days()
} else {
Utc.with_ymd_and_hms(year, month.succ().number_from_month(), 1, 0, 0, 0)
.earliest()
.ok_or(eyre!("Could not construct range"))?
.signed_duration_since(current)
.num_days()
};
log::trace!("Days in month {:?}: {}", month, days_in_m);
let lower = Utc
.with_ymd_and_hms(year, month.number_from_month(), 1, 0, 0, 0)
.earliest()
.ok_or(eyre!("Could not create range"))?;
let upper = Utc
.with_ymd_and_hms(
year,
month.number_from_month(),
days_in_m as u32,
23,
59,
59,
)
.latest()
.ok_or(eyre!("Could not create range"))?;
log::trace!("Lower: {:?}, Upper: {:?}", lower, upper);
Ok(self.range(lower, upper))
}
pub fn daily_range<T: chrono::offset::TimeZone>(
&self,
day: NaiveDate,
tz: T,
) -> Result<impl Iterator<Item = (&DateTime<Utc>, &DurationDef)>> {
log::trace!("Entries for {:?}", day);
let start = day
.and_hms_opt(0, 0, 0)
.ok_or(eyre!("Could not construct range"))?
.and_local_timezone(tz.clone())
.earliest()
.ok_or(eyre!("Could not construct range"))?
.with_timezone(&Utc);
let end = day
.and_hms_opt(23, 59, 59)
.ok_or(eyre!("Could not construct range"))?
.and_local_timezone(tz)
.latest()
.ok_or(eyre!("Could not construct range"))?
.with_timezone(&Utc);
Ok(self.range(start, end))
}
pub(crate) fn insert(&mut self, dt: DateTime<Utc>, dur: DurationDef) {
self.time_account.insert(dt, dur);
}
fn from_reader<R: Read>(reader: &mut R) -> Result<Self> {
serde_json::from_reader(reader).wrap_err(
"Failed to deserialize json. Try 'stempel migrate' to migrate to new json format",
)
}
fn write<W>(&self, writer: &mut W) -> Result<()>
where
W: Write,
{
serde_json::to_writer(writer, &self).wrap_err("Failed to serialize to json")
}
pub fn from_file<P: AsRef<Path>>(path: P, create: bool) -> Result<Self> {
match File::open(&path) {
Ok(f) => {
let mut reader = BufReader::new(f);
let s = Self::from_reader(&mut reader)?;
Ok(s)
}
Err(_) if create => Ok(TimeBalance::new()),
Err(e) => Err(e)
.wrap_err_with(|| format!("Failed to open storage '{}'", path.as_ref().display())),
}
}
pub fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
match OpenOptions::new().write(true).truncate(true).open(&path) {
Ok(mut f) => self.write(&mut f),
Err(_) => {
log::info!("Creating a new storage file {}", path.as_ref().display());
let mut f = File::create(&path).wrap_err_with(|| {
format!(
"There is no storage '{}' and creating failed",
path.as_ref().display()
)
})?;
self.write(&mut f)
}
}
}
pub fn start_state(&self) -> Option<(Duration, DateTime<Utc>)> {
if let Some(s) = self.start {
let dur = Utc::now().signed_duration_since(s);
Some((dur, s))
} else {
None
}
}
pub fn break_state(&self) -> BreakeState {
let break_sum = self.accumulate_breaks();
if self.start.is_none() {
return BreakeState {
current: None,
breaks: self.get_breaks(),
sum: break_sum,
};
}
let current = self.breaking;
let sum = Utc::now()
.signed_duration_since(current.unwrap_or_else(Utc::now))
.checked_add(&break_sum)
.unwrap_or(break_sum);
BreakeState {
current,
breaks: self.get_breaks(),
sum,
}
}
pub fn canocicalize(&mut self) -> Result<()> {
let mut current = self.time_account.iter();
let mut peek = current.clone().skip(1).peekable();
let mut merge = Vec::new();
while let (Some(ne), Some(cur)) = (peek.peek(), current.next()) {
if ne.0.date_naive() == cur.0.date_naive() {
merge.push((*cur.0, *ne.0));
}
peek.next();
}
log::trace!("Removing keys {}: {:?}", merge.len(), merge);
for (del_k, mer_k) in merge {
let added = self
.time_account
.remove(&del_k)
.ok_or(eyre!("Failed to remove duplicate element"))?;
let cur = self
.time_account
.get(&mer_k)
.ok_or(eyre!("Failed to update element"))?;
log::trace!("Adding {:?} to {:?}", added, cur);
*self
.time_account
.get_mut(&mer_k)
.ok_or(eyre!("Failed to canocicalize"))? = *cur + added;
}
Ok(())
}
pub fn calculate_overhours(&self) -> Option<Duration> {
if let Some(daily) = self.config.as_ref().unwrap_or_default().daily_hours {
let daily = Duration::hours(daily as i64);
let hours = self
.time_account
.iter()
.fold(Duration::zero(), |mut acc, (_, v)| {
let dur: Duration = v.into();
acc = acc + dur - daily;
acc
});
Some(hours)
} else {
None
}
}
}
pub(crate) struct BreakeState {
pub current: Option<DateTime<Utc>>,
pub breaks: Vec<(DateTime<Utc>, Duration)>,
pub sum: Duration,
}
impl std::fmt::Display for TimeBalance {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (s, d) in self.time_account.iter() {
let local = s.with_timezone(&Local).format("%d/%m/%Y, %H:%M");
let dur = Duration::from(d);
writeln!(
f,
"{}: {}:{}h",
local,
dur.num_hours(),
dur.num_minutes() % 60
)?;
}
Ok(())
}
}
impl TryFrom<&WorkStorage> for TimeBalance {
type Error = Error;
fn try_from(other: &WorkStorage) -> Result<Self, Self::Error> {
let start = other.try_start().map(|s| s.start).ok();
let breaking = other.try_break().map(|b| b.start).ok();
let breaks = Vec::new();
let time_account: BTreeMap<DateTime<Utc>, DurationDef> = other
.work_sets
.iter()
.filter_map(|e| {
if e.ty == crate::storage::WorkType::Work {
Some((e.start, Duration::from_std(e.duration).unwrap().into()))
} else {
None
}
})
.collect();
Ok(Self {
start,
breaking,
breaks,
config: None,
time_account,
})
}
}
#[cfg(test)]
mod tests {
use crate::storage::WorkSet;
use super::*;
#[test]
fn from_file_works() {
let naive = NaiveDate::from_ymd_opt(2021, 1, 27)
.unwrap()
.and_hms_opt(14, 19, 21)
.unwrap();
let utc_dt = DateTime::from_utc(naive, chrono::Utc);
let dur: DurationDef = Duration::seconds(10).into();
let input = r#"{"start":null,"breaking":null,"breaks":[],"account":{""#.to_string()
+ &utc_dt.to_rfc3339_opts(SecondsFormat::Secs, true)
+ r#"":{"secs":10,"nanos":0}}}"#;
println!("{}", input);
let balance = TimeBalance::from_reader(&mut input.as_bytes()).expect("Failed to serialize");
let mut expected = TimeBalance::new();
expected.insert(utc_dt, dur);
assert_eq!(balance, expected);
}
#[test]
fn to_json_works() {
let mut balance = TimeBalance::new();
let naive = NaiveDate::from_ymd_opt(2021, 1, 27)
.unwrap()
.and_hms_opt(14, 19, 21)
.unwrap();
let utc_dt = DateTime::from_utc(naive, chrono::Utc);
let dur = Duration::seconds(10).into();
balance.insert(utc_dt, dur);
let mut bytes: Vec<u8> = Vec::new();
balance.write(&mut bytes).expect("serialize works");
let json = std::str::from_utf8(&bytes).expect("Bytes represent a string.");
println!("{}", json);
let json_string = r#"{"start":null,"breaking":null,"breaks":[],"account":{""#.to_string()
+ &utc_dt.to_rfc3339_opts(SecondsFormat::Secs, true)
+ r#"":{"secs":10,"nanos":0}}}"#;
assert_eq!(json, json_string);
}
#[test]
fn cancel_break() {
let mut balance = TimeBalance::new();
assert!(balance.cancel().is_err());
balance.start(Utc::now()).expect("Starting works");
balance.start_break(Utc::now()).expect("break works");
balance.cancel().expect("Cancel of break works");
balance.cancel().expect("Cancel of start works");
assert!(balance.cancel().is_err());
}
#[test]
fn daily_range() {
let mut balance = TimeBalance::new();
let range = balance
.daily_range(Utc::now().date_naive(), Utc)
.expect("range works");
assert!(range.last().is_none());
{
let start = Utc::now();
balance
.start(start - Duration::seconds(5))
.expect("starting works");
balance.stop(start).expect("stopping works");
let range: Vec<(&DateTime<Utc>, &DurationDef)> = balance
.daily_range(Utc::now().date_naive(), Utc)
.expect("range works")
.collect();
assert_eq!(range.len(), 1);
assert_eq!(
*range.first().expect("has length 1"),
(&start, &Duration::seconds(5).into())
);
}
{
let start = Utc::now()
.date_naive()
.and_hms_opt(20, 55, 0)
.unwrap()
.and_local_timezone(Utc)
.earliest()
.unwrap();
balance.start(start).expect("Starting works");
let stop = start
.checked_add_signed(Duration::minutes(90))
.expect("adding works");
balance.stop(stop).expect("stopping works");
let range: Vec<(&DateTime<Utc>, &DurationDef)> = balance
.daily_range(Utc::now().date_naive(), Utc)
.expect("range works")
.collect();
assert_eq!(dbg!(&range).len(), 2);
assert_eq!(
*range.get(1).expect("has length 2"),
(&stop, &Duration::minutes(90).into())
);
}
}
#[test]
fn stringify() {
let dur = Duration::nanoseconds(10)
.checked_add(&Duration::seconds(1))
.expect("adding works");
let durdef = DurationDef::from(dur);
assert_eq!(durdef.to_string(), dur.to_string());
let dur_back = Duration::from(&durdef);
assert_eq!(dur_back, durdef.into());
}
#[test]
fn migrate() {
let time = Utc::now();
let start = WorkSet {
ty: crate::storage::WorkType::Start,
duration: std::time::Duration::from_secs(2),
start: time,
};
let br = WorkSet {
ty: crate::storage::WorkType::Break,
duration: std::time::Duration::from_secs(1),
start: time,
};
let work = WorkSet {
ty: crate::storage::WorkType::Work,
duration: std::time::Duration::from_secs(100),
start: time,
};
let storage = WorkStorage {
name: "test".to_string(),
work_sets: vec![start, br, work],
};
let balance: TimeBalance = TimeBalance::try_from(&storage).expect("Conversion works");
println!("{}", balance);
assert_eq!(balance.start, Some(time));
assert_eq!(balance.breaking, Some(time));
}
fn add_times(balance: &mut TimeBalance, dt: DateTime<Utc>, dur: i64) {
balance
.start(dt - Duration::minutes(dur))
.expect("starting works");
balance.stop(dt).expect("stopping works");
}
#[test]
fn canocicalize_works() {
let mut balance = TimeBalance::new();
let now = Utc.with_ymd_and_hms(2022, 1, 12, 1, 20, 30).unwrap();
add_times(&mut balance, now, 10);
add_times(&mut balance, now + Duration::seconds(10), 12);
add_times(&mut balance, now + Duration::seconds(20), 14);
add_times(&mut balance, now + Duration::seconds(30), 18);
assert_eq!(balance.time_account.len(), 4);
balance.canocicalize().expect("Works");
assert_eq!(balance.time_account.len(), 1);
let sum = balance
.time_account
.iter()
.fold(Duration::zero(), |mut acc, (_, v)| {
acc = acc.checked_add(&v.into()).unwrap();
acc
});
assert_eq!(sum, Duration::minutes(54));
}
#[test]
fn overhours_work() {
let mut balance = TimeBalance::new();
balance.config = Some(Config {
daily_hours: Some(1),
..Default::default()
});
let now = Utc.with_ymd_and_hms(2022, 1, 12, 1, 20, 30).unwrap();
add_times(&mut balance, now, 70);
log::trace!("balance: {:?}", balance);
let overhours = balance.calculate_overhours();
assert_eq!(overhours, Some(Duration::minutes(10)));
add_times(&mut balance, now + Duration::seconds(10), 12);
balance.canocicalize().expect("canocicalize works");
let overhours = balance.calculate_overhours();
assert_eq!(overhours, Some(Duration::minutes(22)));
add_times(&mut balance, now - Duration::days(20), 64);
let overhours = balance.calculate_overhours();
assert_eq!(overhours, Some(Duration::minutes(26)));
add_times(&mut balance, now + Duration::days(30), 58);
let overhours = balance.calculate_overhours();
assert_eq!(overhours, Some(Duration::minutes(24)));
}
}