summaryrefslogtreecommitdiffstats
path: root/bin/domain/imag-timetrack/src/cont.rs
blob: cb28d78828181e550112f7702840872aa282d19c (plain)
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
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

use std::cmp::Ord;

use filters::filter::Filter;
use itertools::Itertools;
use chrono::NaiveDateTime;
use anyhow::Result;
use anyhow::Error;
use resiter::Filter as RFilter;

use libimagtimetrack::store::TimeTrackStore;
use libimagtimetrack::iter::filter::*;
use libimagtimetrack::timetracking::TimeTracking;

use libimagrt::runtime::Runtime;

pub fn cont(rt: &Runtime) -> Result<()> {
    let groups = rt.store()
        .get_timetrackings()?
        .filter_ok(|e| has_end_time.filter(&e))

        // It is very unfortunate that we have to collect() here, but this is the least complex
        // solution for the problem of grouping elements of an Iterator<Item = Result<_>>.
        .collect::<Result<Vec<_>>>()?
        .into_iter()
        .group_by(|elem| match elem.get_end_datetime() { // Now group them by the end time
            Err(_)       => NaiveDateTime::from_timestamp(0, 0), // placeholder
            Ok(Some(dt)) => dt,
            Ok(None)     => {
                // error. We expect all of them having an end-time.
                error!("Has no end time, but should be filtered out: {:?}", elem);
                error!("This is a bug. Please report.");
                error!("Will panic now");
                panic!("Unknown bug")
            }
        });

    // sort the trackings by key, so by end datetime
    let elements = {
        let mut v = vec![];
        for (key, value) in groups.into_iter() {
            v.push((key, value));
        }

        v.into_iter()
        .sorted_by(|t1, t2| {
            let (k1, _) = *t1;
            let (k2, _) = *t2;
            Ord::cmp(&k1, &k2)
        })

        // get the last one, which should be the highest one
        .last() // -> Option<_>
    };

    match elements {
        Some((_, trackings)) => {
            // and then, for all trackings
             trackings
                 .collect::<Vec<_>>()
                 .into_iter()
                 .map(|tracking| {
                     debug!("Having tracking: {:?}", tracking);

                    // create a new tracking with the same tag
                     let val = tracking
                         .get_timetrack_tag()
                         .and_then(|tag| rt.store().create_timetracking_now(&tag))
                         .map(|_| 0)?;

                     rt.report_touched(tracking.get_location()).map_err(Error::from).map(|_| val)
                 })
                .collect::<Result<Vec<_>>>()
                .map(|_| ())
        },

        None => {
            info!("No trackings to continue");
            Ok(())
        },
    }
}