1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2016 - 2021, Thomas Lauf, Paul Beckingham, Federico Hernandez.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // https://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26 
27 #include <cmake.h>
28 #include <format.h>
29 #include <timew.h>
30 #include <iostream>
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 // :fill
34 //   The :fill hint is used to eliminate gaps on interval modification, and only
35 //   a single interval is affected.
36 //
37 //   Fill works by extending an interval in both directions if possible, to
38 //   about either an interval or an exclusion, while being constrained by a
39 //   filter range.
40 //
autoFill(const Rules & rules,Database & database,Interval & interval)41 void autoFill (
42   const Rules& rules,
43   Database& database,
44   Interval& interval)
45 {
46   // An empty filter allows scanning beyond interval range.
47   Interval range_filter;
48 
49   // Look backwards from interval.start to a boundary.
50   auto tracked = getTracked (database, rules, range_filter);
51   for (auto earlier = tracked.rbegin (); earlier != tracked.rend (); ++earlier)
52   {
53     if (! earlier->is_open () &&
54         earlier->end <= interval.start)
55     {
56       interval.start = earlier->end;
57         if (rules.getBoolean ("verbose"))
58         std::cout << "Backfilled "
59                   << (interval.id ? format ("@{1} ", interval.id) : "")
60                   << "to "
61                   << interval.start.toISOLocalExtended ()
62                   << "\n";
63       break;
64     }
65   }
66 
67   // If the interval is closed, scan forwards for the next boundary.
68   if (! interval.is_open ())
69   {
70     for (auto& later : tracked)
71     {
72       if (interval.end <= later.start)
73       {
74         interval.end = later.start;
75         if (rules.getBoolean ("verbose"))
76           std::cout << "Filled "
77                     << (interval.id ? format ("@{1} ", interval.id) : "")
78                     << "to "
79                     << interval.end.toISOLocalExtended ()
80                     << "\n";
81         break;
82       }
83     }
84   }
85 }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 // :adjust
89 //   To avoid disallowed overlaps, one or more intervals may be modified to
90 //   resolve the conflict according to configuration-based rules. Resolution
91 //   can involve rejection, adjustment of modified interval, or adjustment of
92 //   recorded data.
93 //
autoAdjust(bool adjust,const Rules & rules,Database & database,Interval & interval)94 static bool autoAdjust (
95   bool adjust,
96   const Rules& rules,
97   Database& database,
98   Interval& interval)
99 {
100   const bool verbose = rules.getBoolean ("verbose");
101 
102   // We do not need the adjust flag set to "flatten" the database if the last
103   // interval is open and encloses the current interval that we're adding.
104   Interval latest = getLatestInterval (database);
105   if (interval.is_open () && latest.encloses (interval))
106   {
107     if (latest.tags () == interval.tags ())
108     {
109       // If the new interval tags match those of the currently open interval,
110       // then do nothing - the tags are already being tracked.
111       return false;
112     }
113 
114     database.deleteInterval (latest);
115     latest.end = interval.start;
116     for (auto& interval : flatten (latest, getAllExclusions (rules, latest)))
117     {
118       database.addInterval (interval, verbose);
119       if (verbose)
120       {
121         std::cout << intervalSummarize (rules, interval);
122       }
123     }
124   }
125 
126   Interval overlaps_filter {interval.start, interval.end};
127   auto overlaps = getTracked (database, rules, overlaps_filter);
128 
129   if (overlaps.empty ())
130   {
131     return true;
132   }
133 
134   debug ("Input         " + interval.dump ());
135   debug ("Overlaps with");
136 
137   for (auto& overlap : overlaps)
138   {
139     debug ("              " + overlap.dump ());
140   }
141 
142   if (! adjust)
143   {
144     throw std::string ("You cannot overlap intervals. Correct the start/end time, or specify the :adjust hint.");
145   }
146   else
147   {
148     // implement overwrite resolution, i.e. the new interval overwrites existing intervals
149     for (auto& overlap : overlaps)
150     {
151       bool start_within_overlap = interval.startsWithin (overlap);
152       bool end_within_overlap = interval.endsWithin (overlap);
153 
154       if (start_within_overlap && !end_within_overlap)
155       {
156         // start date of new interval within old interval
157         Interval modified {overlap};
158         modified.end = interval.start;
159 
160         if (modified.is_empty ())
161         {
162           database.deleteInterval (overlap);
163         }
164         else
165         {
166           database.modifyInterval (overlap, modified, verbose);
167         }
168       }
169       else if (!start_within_overlap && end_within_overlap)
170       {
171         // end date of new interval within old interval
172         Interval modified {overlap};
173         modified.start = interval.end;
174 
175         if (modified.is_empty ())
176         {
177           database.deleteInterval (overlap);
178         }
179         else
180         {
181           database.modifyInterval (overlap, modified, verbose);
182         }
183       }
184       else if (!start_within_overlap && !end_within_overlap)
185       {
186         // new interval encloses old interval
187         database.deleteInterval (overlap);
188       }
189       else
190       {
191         // new interval enclosed by old interval
192         Interval split2 {overlap};
193         Interval split1 {overlap};
194 
195         split1.end = interval.start;
196         split2.start = interval.end;
197 
198         if (split1.is_empty ())
199         {
200           database.deleteInterval (overlap);
201         }
202         else
203         {
204           database.modifyInterval (overlap, split1, verbose);
205         }
206 
207         if (! split2.is_empty ())
208         {
209           database.addInterval (split2, verbose);
210         }
211       }
212     }
213   }
214   return true;
215 }
216 
217 ////////////////////////////////////////////////////////////////////////////////
validate(const CLI & cli,const Rules & rules,Database & database,Interval & interval)218 bool validate (
219   const CLI& cli,
220   const Rules& rules,
221   Database& database,
222   Interval& interval)
223 {
224   // All validation performed here.
225   if (findHint (cli, ":fill"))
226   {
227     autoFill (rules, database, interval);
228   }
229 
230   return autoAdjust (findHint (cli, ":adjust"), rules, database, interval);
231 }
232 
233 ////////////////////////////////////////////////////////////////////////////////
234