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 <thomas.lauf@tngtech.com>
This commit is contained in:
Thomas Lauf 2021-11-04 21:39:39 +01:00
parent a7e2d8c08c
commit c546add03f
12 changed files with 90 additions and 19 deletions

View file

@ -34,6 +34,9 @@
#include <format.h> #include <format.h>
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <IntervalFilterAllInRange.h>
#include <IntervalFilterAllWithTags.h>
#include <IntervalFilterAndGroup.h>
int renderChart (const CLI&, const std::string&, Interval&, Rules&, Database&); int renderChart (const CLI&, const std::string&, Interval&, Rules&, Database&);
@ -95,7 +98,12 @@ int renderChart (
const bool verbose = rules.getBoolean ("verbose"); const bool verbose = rules.getBoolean ("verbose");
// Load the data. // 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 ()) if (tracked.empty ())
{ {

View file

@ -29,6 +29,7 @@
#include <timew.h> #include <timew.h>
#include <iostream> #include <iostream>
#include <cassert> #include <cassert>
#include <IntervalFilterAllWithTags.h>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int CmdContinue ( int CmdContinue (
@ -74,9 +75,8 @@ int CmdContinue (
} }
else if (!filter.tags ().empty ()) else if (!filter.tags ().empty ())
{ {
Interval tagFilter = {filter}; auto filtering = IntervalFilterAllWithTags (filter.tags());
tagFilter.setRange (0, 0); auto tracked = getTracked (database, rules, filtering);
auto tracked = getTracked (database, rules, tagFilter);
if (tracked.empty()) if (tracked.empty())
{ {
@ -120,11 +120,13 @@ int CmdContinue (
to_copy.end = end_time; to_copy.end = end_time;
journal.startTransaction (); journal.startTransaction ();
if (validate (cli, rules, database, to_copy)) if (validate (cli, rules, database, to_copy))
{ {
database.addInterval (to_copy, verbose); database.addInterval (to_copy, verbose);
journal.endTransaction (); journal.endTransaction ();
} }
if (verbose) if (verbose)
{ {
std::cout << intervalSummarize (rules, to_copy); std::cout << intervalSummarize (rules, to_copy);

View file

@ -27,6 +27,9 @@
#include <commands.h> #include <commands.h>
#include <timew.h> #include <timew.h>
#include <iostream> #include <iostream>
#include <IntervalFilterAllInRange.h>
#include <IntervalFilterAllWithTags.h>
#include <IntervalFilterAndGroup.h>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int CmdExport ( int CmdExport (
@ -35,7 +38,16 @@ int CmdExport (
Database& database) Database& database)
{ {
auto filter = cli.getFilter (); 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; return 0;
} }

View file

@ -29,6 +29,7 @@
#include <commands.h> #include <commands.h>
#include <timew.h> #include <timew.h>
#include <iostream> #include <iostream>
#include <IntervalFilterAllInRange.h>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int CmdFill ( int CmdFill (
@ -49,7 +50,9 @@ int CmdFill (
// Load the data. // Load the data.
// Note: There is no filter. // Note: There is no filter.
Interval filter; Interval filter;
auto tracked = getTracked (database, rules, filter);
auto filtering = IntervalFilterAllInRange ({ 0, 0 });
auto tracked = getTracked (database, rules, filtering);
journal.startTransaction (); journal.startTransaction ();

View file

@ -44,13 +44,17 @@ int CmdGet (
for (auto& reference : references) for (auto& reference : references)
{ {
std::string value; std::string value;
if (! domGet (database, filter, rules, reference, value)) if (! domGet (database, filter, rules, reference, value))
{
throw format ("DOM reference '{1}' is not valid.", reference); throw format ("DOM reference '{1}' is not valid.", reference);
}
results.push_back (value); results.push_back (value);
} }
std::cout << join (" ", results) << '\n'; std::cout << join (" ", results) << '\n';
return 0; return 0;
} }

View file

@ -32,6 +32,9 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <FS.h> #include <FS.h>
#include <IntervalFilterAllInRange.h>
#include <IntervalFilterAllWithTags.h>
#include <IntervalFilterAndGroup.h>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Given a partial match for an extension script name, find the full patch of // Given a partial match for an extension script name, find the full patch of
@ -91,7 +94,12 @@ int CmdReport (
// Compose Header info. // Compose Header info.
auto filter = cli.getFilter (); 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.start", filter.is_started () ? filter.start.toISO () : "");
rules.set ("temp.report.end", filter.is_ended () ? filter.end.toISO () : ""); rules.set ("temp.report.end", filter.is_ended () ? filter.end.toISO () : "");

View file

@ -31,6 +31,9 @@
#include <commands.h> #include <commands.h>
#include <timew.h> #include <timew.h>
#include <iostream> #include <iostream>
#include <IntervalFilterAndGroup.h>
#include <IntervalFilterAllWithTags.h>
#include <IntervalFilterAllInRange.h>
// Implemented in CmdChart.cpp. // Implemented in CmdChart.cpp.
std::map <Datetime, std::string> createHolidayMap (Rules&, Interval&); std::map <Datetime, std::string> createHolidayMap (Rules&, Interval&);
@ -48,7 +51,12 @@ int CmdSummary (
auto filter = cli.getFilter (Range { Datetime ("today"), Datetime ("tomorrow") }); auto filter = cli.getFilter (Range { Datetime ("today"), Datetime ("tomorrow") });
// Load the data. // 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 ()) if (tracked.empty ())
{ {

View file

@ -31,6 +31,9 @@
#include <Color.h> #include <Color.h>
#include <set> #include <set>
#include <iostream> #include <iostream>
#include <IntervalFilterAllInRange.h>
#include <IntervalFilterAllWithTags.h>
#include <IntervalFilterAndGroup.h>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int CmdTags ( int CmdTags (
@ -42,10 +45,14 @@ int CmdTags (
// Create a filter, with no default range. // Create a filter, with no default range.
auto filter = cli.getFilter (); auto filter = cli.getFilter ();
auto filtering = IntervalFilterAndGroup ({
new IntervalFilterAllInRange ({ filter.start, filter.end }),
new IntervalFilterAllWithTags (filter.tags ())
});
// Generate a unique, ordered list of tags. // Generate a unique, ordered list of tags.
std::set <std::string> tags; std::set <std::string> tags;
for (const auto& interval : getTracked (database, rules, filter)) for (const auto& interval : getTracked (database, rules, filtering))
for (auto& tag : interval.tags ()) for (auto& tag : interval.tags ())
tags.insert (tag); tags.insert (tag);

View file

@ -36,6 +36,7 @@
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <IntervalFactory.h> #include <IntervalFactory.h>
#include <IntervalFilter.h>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Read rules and extract all holiday definitions. Create a Range for each // 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 <Interval> getTracked ( std::vector <Interval> getTracked (
Database& database, Database& database,
const Rules& rules, const Rules& rules,
Interval& filter) IntervalFilter& filter)
{ {
int current_id = 0; int current_id = 0;
std::vector <Interval> intervals; std::vector <Interval> intervals;
@ -513,11 +514,15 @@ std::vector <Interval> getTracked (
for (auto& interval : expandLatest (latest, rules)) for (auto& interval : expandLatest (latest, rules))
{ {
++current_id; ++current_id;
if (matchesFilter (interval, filter)) if (filter.accepts (interval))
{ {
interval.id = current_id; interval.id = current_id;
intervals.push_back (interval); intervals.push_back (interval);
} }
else if (filter.is_done ())
{
break;
}
} }
} }
@ -526,11 +531,11 @@ std::vector <Interval> getTracked (
Interval interval = IntervalFactory::fromSerialization(*it); Interval interval = IntervalFactory::fromSerialization(*it);
interval.id = ++current_id; interval.id = ++current_id;
if (matchesFilter (interval, filter)) if (filter.accepts (interval))
{ {
intervals.push_back (std::move (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 // 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 // order, if the filter is after the interval, we know there will be no
@ -541,9 +546,10 @@ std::vector <Interval> getTracked (
debug (format ("Loaded {1} tracked intervals", intervals.size ())); 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. // intervals sorted by date, which are ids in reverse order.
std::reverse (intervals.begin (), intervals.end ()); std::reverse (intervals.begin (), intervals.end ());
return intervals; return intervals;
} }

View file

@ -31,6 +31,9 @@
#include <format.h> #include <format.h>
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <IntervalFilterAllInRange.h>
#include <IntervalFilterAllWithTags.h>
#include <IntervalFilterAndGroup.h>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool domGet ( bool domGet (
@ -107,7 +110,12 @@ bool domGet (
// dom.tracked.<...> // dom.tracked.<...>
else if (pig.skipLiteral ("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 <int> (tracked.size ()); int count = static_cast <int> (tracked.size ());
// dom.tracked.tags // dom.tracked.tags

View file

@ -35,6 +35,7 @@
#include <Exclusion.h> #include <Exclusion.h>
#include <Palette.h> #include <Palette.h>
#include <Color.h> #include <Color.h>
#include <IntervalFilter.h>
// data.cpp // data.cpp
std::vector <Range> getHolidays (const Rules&); std::vector <Range> getHolidays (const Rules&);
@ -52,7 +53,7 @@ Range outerRange (const std::vector <Interval>&);
bool matchesRange (const Interval&, const Range&); bool matchesRange (const Interval&, const Range&);
bool matchesFilter (const Interval&, const Interval&); bool matchesFilter (const Interval&, const Interval&);
Interval clip (const Interval&, const Range&); Interval clip (const Interval&, const Range&);
std::vector <Interval> getTracked (Database&, const Rules&, Interval&); std::vector <Interval> getTracked (Database&, const Rules&, IntervalFilter&);
std::vector <Range> getUntracked (Database&, const Rules&, Interval&); std::vector <Range> getUntracked (Database&, const Rules&, Interval&);
Interval getLatestInterval (Database&); Interval getLatestInterval (Database&);
Range getFullDay (const Datetime&); Range getFullDay (const Datetime&);

View file

@ -28,6 +28,7 @@
#include <format.h> #include <format.h>
#include <timew.h> #include <timew.h>
#include <iostream> #include <iostream>
#include <IntervalFilterAllInRange.h>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// :fill // :fill
@ -44,7 +45,7 @@ void autoFill (
Interval& interval) Interval& interval)
{ {
// An empty filter allows scanning beyond interval range. // 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. // Look backwards from interval.start to a boundary.
auto tracked = getTracked (database, rules, range_filter); auto tracked = getTracked (database, rules, range_filter);
@ -99,9 +100,10 @@ static bool autoAdjust (
{ {
const bool verbose = rules.getBoolean ("verbose"); 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 is open and encloses the current interval that we're adding.
Interval latest = getLatestInterval (database); Interval latest = getLatestInterval (database);
if (interval.is_open () && latest.encloses (interval)) if (interval.is_open () && latest.encloses (interval))
{ {
if (latest.tags () == interval.tags ()) if (latest.tags () == interval.tags ())
@ -113,9 +115,11 @@ static bool autoAdjust (
database.deleteInterval (latest); database.deleteInterval (latest);
latest.end = interval.start; latest.end = interval.start;
for (auto& interval : flatten (latest, getAllExclusions (rules, latest))) for (auto& interval : flatten (latest, getAllExclusions (rules, latest)))
{ {
database.addInterval (interval, verbose); database.addInterval (interval, verbose);
if (verbose) if (verbose)
{ {
std::cout << intervalSummarize (rules, interval); 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); auto overlaps = getTracked (database, rules, overlaps_filter);
if (overlaps.empty ()) if (overlaps.empty ())