CmdStart: Honor the :adjust flag when overwriting a currently open interval

Move the adjustment of a new open interval that is enclosed by the
current open interval into the validation processing, where the other
overlap resolution takes place.

This will allow the start command to honor the :adjust flag when
starting a new interval that predates the current open interval.

Closes #326

Signed-off-by: Shaun Ruffell <sruffell@sruffell.net>
Signed-off-by: Thomas Lauf <thomas.lauf@tngtech.com>
This commit is contained in:
Shaun Ruffell 2020-06-08 06:13:00 +02:00 committed by Thomas Lauf
parent 1c1066ae6c
commit cc82f468e1
3 changed files with 43 additions and 57 deletions

View file

@ -38,83 +38,47 @@ int CmdStart (
auto verbose = rules.getBoolean ("verbose");
const Datetime now {};
auto filter = cli.getFilter ({now, 0});
auto interval = cli.getFilter ({now, 0});
if (filter.start > now)
if (interval.start > now)
{
throw std::string ("Time tracking cannot be set in the future.");
}
else if (!filter.is_started ())
else if (!interval.is_started ())
{
// The :all hint provides a filter that is neither started nor ended, which
// the start command cannot handle and we do not want to auto start it now.
throw std::string ("Interval start must be specified");
}
else if (interval.is_ended ())
{
return CmdTrack (cli, rules, database, journal);
}
auto latest = getLatestInterval (database);
journal.startTransaction ();
// If the latest interval is open, close it.
if (latest.is_open ())
{
// If the new interval tags match those of the currently open interval, then
// do nothing - the tags are already being tracked.
if (latest.encloses (filter) && latest.tags () == filter.tags ())
if (latest.encloses (interval) && latest.tags () == interval.tags ())
{
if (verbose)
std::cout << intervalSummarize (database, rules, latest);
return 0;
}
// Stop it, at the given start time, if applicable.
Interval modified {latest};
if (filter.start.toEpoch () != 0)
{
if (modified.start >= filter.start)
{
throw std::string ("The end of a date range must be after the start.");
std::cout << intervalSummarize (database, rules, latest);
}
modified.end = filter.start;
}
else
{
modified.end = Datetime ();
}
// Update database.
database.deleteInterval (latest);
validate (cli, rules, database, modified);
for (auto& interval : flatten (modified, getAllExclusions (rules, modified)))
{
database.addInterval (interval, verbose);
if (verbose)
std::cout << intervalSummarize (database, rules, interval);
return 0;
}
}
// Now add the new open interval.
Interval started;
if (filter.start.toEpoch () != 0)
started.start = filter.start;
else
started.start = Datetime ();
for (auto& tag : filter.tags ())
started.tag (tag);
// Update database. An open interval does not need to be flattened.
validate (cli, rules, database, started);
database.addInterval (started, verbose);
journal.startTransaction ();
validate (cli, rules, database, interval);
database.addInterval (interval, verbose);
journal.endTransaction ();
if (verbose)
std::cout << intervalSummarize (database, rules, started);
journal.endTransaction ();
{
std::cout << intervalSummarize (database, rules, interval);
}
return 0;
}

View file

@ -99,6 +99,23 @@ static void autoAdjust (
{
const bool verbose = rules.getBoolean ("verbose");
// We do not need the adjust flag set to "flatten" the database if the last
// interval is open and encloses the current interval that we're adding.
Interval latest = getLatestInterval (database);
if (interval.is_open () && latest.encloses (interval))
{
database.deleteInterval (latest);
latest.end = interval.start;
for (auto& interval : flatten (latest, getAllExclusions (rules, latest)))
{
database.addInterval (interval, verbose);
if (verbose)
{
std::cout << intervalSummarize (database, rules, interval);
}
}
}
Interval overlaps_filter {interval.start, interval.end};
auto overlaps = getTracked (database, rules, overlaps_filter);