CmdAnnotate: Do not load entire database

Related to issue #245
This commit is contained in:
Shaun Ruffell 2020-01-05 20:36:29 -06:00 committed by lauft
parent eaed297964
commit 8c8f6b7c7c

View file

@ -38,78 +38,50 @@ int CmdAnnotate (
Database& database, Database& database,
Journal& journal) Journal& journal)
{ {
const bool verbose = rules.getBoolean ("verbose");
std::set <int> ids = cli.getIds (); std::set <int> ids = cli.getIds ();
std::string annotation = cli.getAnnotation (); std::string annotation = cli.getAnnotation ();
// Load the data.
// Note: There is no filter.
Interval filter;
auto tracked = getTracked (database, rules, filter);
bool dirty = true;
journal.startTransaction (); journal.startTransaction ();
for (auto& id : ids) flattenDatabase (database, rules);
auto intervals = getIntervalsByIds (database, rules, ids);
if (intervals.empty ())
{ {
if (id > static_cast <int> (tracked.size ())) if (database.empty ())
{
throw format ("ID '@{1}' does not correspond to any tracking.", id);
}
if (tracked[tracked.size() - id].synthetic && dirty)
{
auto latest = getLatestInterval(database);
auto exclusions = getAllExclusions (rules, filter);
Interval modified {latest};
// Update database.
database.deleteInterval (latest);
for (auto& interval : flatten (modified, exclusions))
database.addInterval (interval, rules.getBoolean ("verbose"));
dirty = false;
}
}
if (ids.empty ())
{
if (tracked.empty ())
{ {
throw std::string ("There is no active time tracking."); throw std::string ("There is no active time tracking.");
} }
if (!tracked.back ().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'.");
} }
ids.insert (1); latest.id = 1;
intervals.push_back (latest);
} }
// Apply tags to ids. // Apply annotations to ids.
for (auto& id : ids) for (const auto& interval : intervals)
{ {
if (id > static_cast <int> (tracked.size ())) Interval modified {interval};
{
throw format ("ID '@{1}' does not correspond to any tracking.", id);
}
Interval i = tracked[tracked.size () - id]; modified.setAnnotation (annotation);
i.setAnnotation (annotation);
database.modifyInterval (tracked[tracked.size () - id], i, rules.getBoolean ("verbose")); database.modifyInterval (interval, modified, verbose);
if (rules.getBoolean ("verbose")) if (verbose)
{ {
if (annotation.empty ()) if (annotation.empty ())
{ {
std::cout << "Removed annotation from @" << id << std::endl; std::cout << "Removed annotation from @" << modified.id << std::endl;
} }
else else
{ {
std::cout << "Annotated @" << id << " with \"" << annotation << "\"" << std::endl; std::cout << "Annotated @" << modified.id << " with \"" << annotation << "\"" << std::endl;
} }
} }
} }