CmdUntag: Do not load entire database when untagging intervals

Related to issue #245
This commit is contained in:
Shaun Ruffell 2020-01-05 20:25:56 -06:00 committed by lauft
parent aa44df24c0
commit a5785dd41a

View file

@ -39,76 +39,52 @@ int CmdUntag (
Journal& journal)
{
// Gather IDs and TAGs.
const bool verbose = rules.getBoolean ("verbose");
std::set <int> ids = cli.getIds ();
std::vector<std::string> tags = cli.getTags ();
if (tags.empty ())
{
throw std::string ("At least one tag must be specified. See 'timew help untag'.");
throw std::string ("At least one tag must be specified. See 'timew help tag'.");
}
journal.startTransaction ();
// Load the data.
// Note: There is no filter.
Interval filter;
auto tracked = getTracked (database, rules, filter);
flattenDatabase (database, rules);
auto intervals = getIntervalsByIds (database, rules, ids);
bool dirty = true;
for (auto& id : ids)
if (intervals.empty ())
{
if (id > static_cast <int> (tracked.size ()))
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 ())
if (database.empty ())
{
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 tag'.");
}
ids.insert (1);
latest.id = 1;
intervals.push_back (latest);
}
// Remove tags from ids.
for (auto& id : ids)
// Apply tags to ids.
for (const auto& interval : intervals)
{
if (id > static_cast <int> (tracked.size ()))
throw format ("ID '@{1}' does not correspond to any tracking.", id);
Interval i = tracked[tracked.size () - id];
Interval modified {interval};
for (auto& tag : tags)
i.untag (tag);
modified.untag (tag);
// TODO validate (cli, rules, database, i);
database.modifyInterval (tracked[tracked.size () - id], i, rules.getBoolean ("verbose"));
//TODO validate (cli, rules, database, i);
database.modifyInterval (interval, modified, verbose);
if (rules.getBoolean ("verbose"))
if (verbose)
{
std::cout << "Removed " << joinQuotedIfNeeded (" ", tags) << " from @" << id << '\n';
std::cout << "Removed " << joinQuotedIfNeeded (" ", tags) << " from @" << interval.id << '\n';
}
}