Optimize handling of implicit latest interval

- If no id is given but active time tracking (i.e. open interval) is present
  then apply the command to the open interval
This commit is contained in:
Thomas Lauf 2020-01-26 17:16:01 +01:00
parent 711848915b
commit 35a38d628c
4 changed files with 36 additions and 31 deletions

View file

@ -43,28 +43,30 @@ int CmdAnnotate (
std::string annotation = cli.getAnnotation (); std::string annotation = cli.getAnnotation ();
journal.startTransaction (); journal.startTransaction ();
flattenDatabase (database, rules); flattenDatabase (database, rules);
auto intervals = getIntervalsByIds (database, rules, ids); std::vector <Interval> intervals;
if (ids.empty ()) if (ids.empty ())
{ {
if (database.empty ()) auto latest = getLatestInterval (database);
if (latest.empty ())
{ {
throw std::string ("There is no active time tracking."); throw std::string ("There is no active time tracking.");
} }
else if (!latest.is_open ())
auto latest = getLatestInterval (database);
if (!latest.is_open ())
{ {
throw std::string ("At least one ID must be specified. See 'timew help annotate'."); throw std::string ("At least one ID must be specified. See 'timew help annotate'.");
} }
latest.id = 1;
intervals.push_back (latest); intervals.push_back (latest);
} }
else
{
intervals = getIntervalsByIds (database, rules, ids);
}
// Apply annotations to ids. // Apply annotations to intervals.
for (const auto& interval : intervals) for (const auto& interval : intervals)
{ {
Interval modified {interval}; Interval modified {interval};

View file

@ -52,27 +52,29 @@ int CmdTag (
journal.startTransaction (); journal.startTransaction ();
flattenDatabase (database, rules); flattenDatabase (database, rules);
auto intervals = getIntervalsByIds (database, rules, ids); std::vector <Interval> intervals;
if (intervals.empty ()) if (ids.empty ())
{ {
if (database.empty ()) auto latest = getLatestInterval (database);
if (latest.empty ())
{ {
throw std::string ("There is no active time tracking."); throw std::string ("There is no active time tracking.");
} }
else if (!latest.is_open ())
auto latest = getLatestInterval (database);
if (!latest.is_open ())
{ {
throw std::string ("At least one ID must be specified. See 'timew help tag'."); throw std::string ("At least one ID must be specified. See 'timew help tag'.");
} }
latest.id = 1;
intervals.push_back (latest); intervals.push_back (latest);
} }
else
{
intervals = getIntervalsByIds (database, rules, ids);
}
// Apply tags to ids. // Apply tags to intervals.
for (const auto& interval : intervals) for (const auto& interval : intervals)
{ {
Interval modified {interval}; Interval modified {interval};
@ -82,7 +84,6 @@ int CmdTag (
modified.tag (tag); modified.tag (tag);
} }
//TODO validate (cli, rules, database, i);
database.modifyInterval (interval, modified, verbose); database.modifyInterval (interval, modified, verbose);
if (verbose) if (verbose)

View file

@ -51,27 +51,29 @@ int CmdUntag (
journal.startTransaction (); journal.startTransaction ();
flattenDatabase (database, rules); flattenDatabase (database, rules);
auto intervals = getIntervalsByIds (database, rules, ids); std::vector <Interval> intervals;
if (intervals.empty ()) if (ids.empty ())
{ {
if (database.empty ()) auto latest = getLatestInterval (database);
if (latest.empty ())
{ {
throw std::string ("There is no active time tracking."); throw std::string ("There is no active time tracking.");
} }
else if (!latest.is_open ())
auto latest = getLatestInterval (database);
if (!latest.is_open ())
{ {
throw std::string ("At least one ID must be specified. See 'timew help tag'."); throw std::string ("At least one ID must be specified. See 'timew help untag'.");
} }
latest.id = 1;
intervals.push_back (latest); intervals.push_back (latest);
} }
else
{
intervals = getIntervalsByIds (database, rules, ids);
}
// Remove tags from ids. // Remove tags from intervals.
for (const auto& interval : intervals) for (const auto& interval : intervals)
{ {
Interval modified {interval}; Interval modified {interval};
@ -81,7 +83,6 @@ int CmdUntag (
modified.untag (tag); modified.untag (tag);
} }
//TODO validate (cli, rules, database, i);
database.modifyInterval (interval, modified, verbose); database.modifyInterval (interval, modified, verbose);
if (verbose) if (verbose)

View file

@ -675,7 +675,7 @@ std::vector <Interval> getTracked (
{ {
auto begin = database.rbegin (); auto begin = database.rbegin ();
auto end = database.rend (); auto end = database.rend ();
if (begin != end) if (begin != end)
{ {
filter.start = IntervalFactory::fromSerialization (*begin).start; filter.start = IntervalFactory::fromSerialization (*begin).start;
} }
@ -703,7 +703,7 @@ std::vector <Interval> getTracked (
else if (interval.start.toEpoch () >= filter.start.toEpoch ()) else if (interval.start.toEpoch () >= filter.start.toEpoch ())
{ {
++id_skip; ++id_skip;
} }
else else
{ {
break; break;
@ -788,6 +788,7 @@ Interval getLatestInterval (Database& database)
if (! latestEntry.empty ()) if (! latestEntry.empty ())
{ {
i = IntervalFactory::fromSerialization (latestEntry); i = IntervalFactory::fromSerialization (latestEntry);
i.id = 1;
} }
return i; return i;