From c546add03f9e89d34ec6f902d4dd65f6e773f2a8 Mon Sep 17 00:00:00 2001 From: Thomas Lauf Date: Thu, 4 Nov 2021 21:39:39 +0100 Subject: [PATCH] Use new filtering in `getTracked` Replace getTracked in commands CmdSummary, CmdChart, CmdExport, CmdFill, CmdReport, CmdTags, and CmdContinue. Also in functions domGet and autoFill Relates to #468 Signed-off-by: Thomas Lauf --- src/commands/CmdChart.cpp | 10 +++++++++- src/commands/CmdContinue.cpp | 8 +++++--- src/commands/CmdExport.cpp | 14 +++++++++++++- src/commands/CmdFill.cpp | 5 ++++- src/commands/CmdGet.cpp | 4 ++++ src/commands/CmdReport.cpp | 10 +++++++++- src/commands/CmdSummary.cpp | 10 +++++++++- src/commands/CmdTags.cpp | 9 ++++++++- src/data.cpp | 16 +++++++++++----- src/dom.cpp | 10 +++++++++- src/timew.h | 3 ++- src/validate.cpp | 10 +++++++--- 12 files changed, 90 insertions(+), 19 deletions(-) diff --git a/src/commands/CmdChart.cpp b/src/commands/CmdChart.cpp index fc7243d2..2fcc7f6f 100644 --- a/src/commands/CmdChart.cpp +++ b/src/commands/CmdChart.cpp @@ -34,6 +34,9 @@ #include #include #include +#include +#include +#include int renderChart (const CLI&, const std::string&, Interval&, Rules&, Database&); @@ -95,7 +98,12 @@ int renderChart ( const bool verbose = rules.getBoolean ("verbose"); // Load the data. - const auto tracked = getTracked (database, rules, filter); + auto filtering = IntervalFilterAndGroup ({ + new IntervalFilterAllInRange ({ filter.start, filter.end }), + new IntervalFilterAllWithTags (filter.tags()) + }); + + auto tracked = getTracked (database, rules, filtering); if (tracked.empty ()) { diff --git a/src/commands/CmdContinue.cpp b/src/commands/CmdContinue.cpp index 331c07b4..d00ccac7 100644 --- a/src/commands/CmdContinue.cpp +++ b/src/commands/CmdContinue.cpp @@ -29,6 +29,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////////////////////////// int CmdContinue ( @@ -74,9 +75,8 @@ int CmdContinue ( } else if (!filter.tags ().empty ()) { - Interval tagFilter = {filter}; - tagFilter.setRange (0, 0); - auto tracked = getTracked (database, rules, tagFilter); + auto filtering = IntervalFilterAllWithTags (filter.tags()); + auto tracked = getTracked (database, rules, filtering); if (tracked.empty()) { @@ -120,11 +120,13 @@ int CmdContinue ( to_copy.end = end_time; journal.startTransaction (); + if (validate (cli, rules, database, to_copy)) { database.addInterval (to_copy, verbose); journal.endTransaction (); } + if (verbose) { std::cout << intervalSummarize (rules, to_copy); diff --git a/src/commands/CmdExport.cpp b/src/commands/CmdExport.cpp index fc6dd4cc..14b08c52 100644 --- a/src/commands/CmdExport.cpp +++ b/src/commands/CmdExport.cpp @@ -27,6 +27,9 @@ #include #include #include +#include +#include +#include //////////////////////////////////////////////////////////////////////////////// int CmdExport ( @@ -35,7 +38,16 @@ int CmdExport ( Database& database) { auto filter = cli.getFilter (); - std::cout << jsonFromIntervals (getTracked (database, rules, filter)); + + auto filtering = IntervalFilterAndGroup ({ + new IntervalFilterAllInRange ({ filter.start, filter.end }), + new IntervalFilterAllWithTags (filter.tags()) + }); + + auto intervals = getTracked (database, rules, filtering); + + std::cout << jsonFromIntervals (intervals); + return 0; } diff --git a/src/commands/CmdFill.cpp b/src/commands/CmdFill.cpp index f068183d..359c786d 100644 --- a/src/commands/CmdFill.cpp +++ b/src/commands/CmdFill.cpp @@ -29,6 +29,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////////////////////////// int CmdFill ( @@ -49,7 +50,9 @@ int CmdFill ( // Load the data. // Note: There is no filter. Interval filter; - auto tracked = getTracked (database, rules, filter); + + auto filtering = IntervalFilterAllInRange ({ 0, 0 }); + auto tracked = getTracked (database, rules, filtering); journal.startTransaction (); diff --git a/src/commands/CmdGet.cpp b/src/commands/CmdGet.cpp index b7c66227..4b0d35b1 100644 --- a/src/commands/CmdGet.cpp +++ b/src/commands/CmdGet.cpp @@ -44,13 +44,17 @@ int CmdGet ( for (auto& reference : references) { std::string value; + if (! domGet (database, filter, rules, reference, value)) + { throw format ("DOM reference '{1}' is not valid.", reference); + } results.push_back (value); } std::cout << join (" ", results) << '\n'; + return 0; } diff --git a/src/commands/CmdReport.cpp b/src/commands/CmdReport.cpp index 4c533d17..7f811024 100644 --- a/src/commands/CmdReport.cpp +++ b/src/commands/CmdReport.cpp @@ -32,6 +32,9 @@ #include #include #include +#include +#include +#include //////////////////////////////////////////////////////////////////////////////// // Given a partial match for an extension script name, find the full patch of @@ -91,7 +94,12 @@ int CmdReport ( // Compose Header info. auto filter = cli.getFilter (); - auto tracked = getTracked (database, rules, filter); + auto filtering = IntervalFilterAndGroup ({ + new IntervalFilterAllInRange ({ filter.start, filter.end }), + new IntervalFilterAllWithTags (filter.tags ()) + }); + + auto tracked = getTracked (database, rules, filtering); rules.set ("temp.report.start", filter.is_started () ? filter.start.toISO () : ""); rules.set ("temp.report.end", filter.is_ended () ? filter.end.toISO () : ""); diff --git a/src/commands/CmdSummary.cpp b/src/commands/CmdSummary.cpp index ef5a5c5e..9183f06c 100644 --- a/src/commands/CmdSummary.cpp +++ b/src/commands/CmdSummary.cpp @@ -31,6 +31,9 @@ #include #include #include +#include +#include +#include // Implemented in CmdChart.cpp. std::map createHolidayMap (Rules&, Interval&); @@ -48,7 +51,12 @@ int CmdSummary ( auto filter = cli.getFilter (Range { Datetime ("today"), Datetime ("tomorrow") }); // Load the data. - auto tracked = getTracked (database, rules, filter); + auto filtering = IntervalFilterAndGroup ({ + new IntervalFilterAllInRange ({ filter.start, filter.end }), + new IntervalFilterAllWithTags (filter.tags()) + }); + + auto tracked = getTracked (database, rules, filtering); if (tracked.empty ()) { diff --git a/src/commands/CmdTags.cpp b/src/commands/CmdTags.cpp index c3f0ca06..47d45b99 100644 --- a/src/commands/CmdTags.cpp +++ b/src/commands/CmdTags.cpp @@ -31,6 +31,9 @@ #include #include #include +#include +#include +#include //////////////////////////////////////////////////////////////////////////////// int CmdTags ( @@ -42,10 +45,14 @@ int CmdTags ( // Create a filter, with no default range. auto filter = cli.getFilter (); + auto filtering = IntervalFilterAndGroup ({ + new IntervalFilterAllInRange ({ filter.start, filter.end }), + new IntervalFilterAllWithTags (filter.tags ()) + }); // Generate a unique, ordered list of tags. std::set tags; - for (const auto& interval : getTracked (database, rules, filter)) + for (const auto& interval : getTracked (database, rules, filtering)) for (auto& tag : interval.tags ()) tags.insert (tag); diff --git a/src/data.cpp b/src/data.cpp index a74fbff6..49b0f4ed 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -36,6 +36,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////////////////////////// // Read rules and extract all holiday definitions. Create a Range for each @@ -495,7 +496,7 @@ Interval clip (const Interval& interval, const Range& range) std::vector getTracked ( Database& database, const Rules& rules, - Interval& filter) + IntervalFilter& filter) { int current_id = 0; std::vector intervals; @@ -513,11 +514,15 @@ std::vector getTracked ( for (auto& interval : expandLatest (latest, rules)) { ++current_id; - if (matchesFilter (interval, filter)) + if (filter.accepts (interval)) { interval.id = current_id; intervals.push_back (interval); } + else if (filter.is_done ()) + { + break; + } } } @@ -526,11 +531,11 @@ std::vector getTracked ( Interval interval = IntervalFactory::fromSerialization(*it); interval.id = ++current_id; - if (matchesFilter (interval, filter)) + if (filter.accepts (interval)) { intervals.push_back (std::move (interval)); } - else if ((interval.start < filter.start) && ! interval.intersects (filter)) + else if (filter.is_done ()) { // Since we are moving backwards in time, and the intervals are in sorted // order, if the filter is after the interval, we know there will be no @@ -541,9 +546,10 @@ std::vector getTracked ( debug (format ("Loaded {1} tracked intervals", intervals.size ())); - // By default intervals are sorted by id, but getTracked needs to return the + // By default, intervals are sorted by id, but getTracked needs to return the // intervals sorted by date, which are ids in reverse order. std::reverse (intervals.begin (), intervals.end ()); + return intervals; } diff --git a/src/dom.cpp b/src/dom.cpp index 008b52a7..689971a3 100644 --- a/src/dom.cpp +++ b/src/dom.cpp @@ -31,6 +31,9 @@ #include #include #include +#include +#include +#include //////////////////////////////////////////////////////////////////////////////// bool domGet ( @@ -107,7 +110,12 @@ bool domGet ( // dom.tracked.<...> else if (pig.skipLiteral ("tracked.")) { - auto tracked = getTracked (database, rules, filter); + auto filtering = IntervalFilterAndGroup ({ + new IntervalFilterAllInRange ({ filter.start, filter.end }), + new IntervalFilterAllWithTags (filter.tags()) + }); + + auto tracked = getTracked (database, rules, filtering); int count = static_cast (tracked.size ()); // dom.tracked.tags diff --git a/src/timew.h b/src/timew.h index 722f1e1c..99384a15 100644 --- a/src/timew.h +++ b/src/timew.h @@ -35,6 +35,7 @@ #include #include #include +#include // data.cpp std::vector getHolidays (const Rules&); @@ -52,7 +53,7 @@ Range outerRange (const std::vector &); bool matchesRange (const Interval&, const Range&); bool matchesFilter (const Interval&, const Interval&); Interval clip (const Interval&, const Range&); -std::vector getTracked (Database&, const Rules&, Interval&); +std::vector getTracked (Database&, const Rules&, IntervalFilter&); std::vector getUntracked (Database&, const Rules&, Interval&); Interval getLatestInterval (Database&); Range getFullDay (const Datetime&); diff --git a/src/validate.cpp b/src/validate.cpp index 32c95b29..0251a656 100644 --- a/src/validate.cpp +++ b/src/validate.cpp @@ -28,6 +28,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////////////////////////// // :fill @@ -44,7 +45,7 @@ void autoFill ( Interval& interval) { // An empty filter allows scanning beyond interval range. - Interval range_filter; + auto range_filter = IntervalFilterAllInRange ({0, 0}); // Look backwards from interval.start to a boundary. auto tracked = getTracked (database, rules, range_filter); @@ -99,9 +100,10 @@ static bool autoAdjust ( { const bool verbose = rules.getBoolean ("verbose"); - // We do not need the adjust flag set to "flatten" the database if the last + // 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)) { if (latest.tags () == interval.tags ()) @@ -113,9 +115,11 @@ static bool autoAdjust ( database.deleteInterval (latest); latest.end = interval.start; + for (auto& interval : flatten (latest, getAllExclusions (rules, latest))) { database.addInterval (interval, verbose); + if (verbose) { std::cout << intervalSummarize (rules, interval); @@ -123,7 +127,7 @@ static bool autoAdjust ( } } - Interval overlaps_filter {interval.start, interval.end}; + auto overlaps_filter = IntervalFilterAllInRange ({interval.start, interval.end}); auto overlaps = getTracked (database, rules, overlaps_filter); if (overlaps.empty ())