Filter: Merged Filter and Interval, removing Filter

This commit is contained in:
Paul Beckingham 2016-04-22 22:10:26 -04:00
parent 35f54b64e1
commit e7dd8edf26
8 changed files with 20 additions and 172 deletions

View file

@ -12,7 +12,6 @@ set (timew_SRCS CLI.cpp CLI.h
Daterange.cpp Daterange.h Daterange.cpp Daterange.h
Exclusion.cpp Exclusion.h Exclusion.cpp Exclusion.h
Extensions.cpp Extensions.h Extensions.cpp Extensions.h
Filter.cpp Filter.h
Interval.cpp Interval.h Interval.cpp Interval.h
Lexer.cpp Lexer.h Lexer.cpp Lexer.h
Rules.cpp Rules.h Rules.cpp Rules.h

View file

@ -1,80 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <Filter.h>
#include <sstream>
////////////////////////////////////////////////////////////////////////////////
bool Filter::empty () const
{
return _range.start ().toEpoch () == 0 &&
_range.end ().toEpoch () == 0 &&
_tags.size () == 0;
}
////////////////////////////////////////////////////////////////////////////////
Daterange Filter::range () const
{
return _range;
}
////////////////////////////////////////////////////////////////////////////////
void Filter::range (const Daterange& range)
{
_range = range;
}
////////////////////////////////////////////////////////////////////////////////
std::set <std::string> Filter::tags () const
{
return _tags;
}
////////////////////////////////////////////////////////////////////////////////
void Filter::tag (const std::string& tag)
{
if (_tags.find (tag) == _tags.end ())
_tags.insert (tag);
}
////////////////////////////////////////////////////////////////////////////////
std::string Filter::dump () const
{
std::stringstream out;
out << "Filter _range "
<< _range.dump ()
<< " _tags";
for (auto& tag : _tags)
out << " '" << tag << "'";
out << "\n";
return out.str ();
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -1,53 +0,0 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2015 - 2016, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_FILTER
#define INCLUDED_FILTER
#include <Daterange.h>
#include <string>
#include <set>
class Filter
{
public:
Filter () = default;
bool empty () const;
Daterange range () const;
void range (const Daterange&);
std::set <std::string> tags () const;
void tag (const std::string&);
std::string dump () const;
private:
Daterange _range {};
std::set <std::string> _tags {};
};
#endif

View file

@ -35,7 +35,7 @@ int CmdExport (
Rules& rules, Rules& rules,
Database& database) Database& database)
{ {
auto filter = createFilterFromCLI (cli); auto filter = createFilterIntervalFromCLI (cli);
auto timeline = createTimelineFromData (rules, database, filter); auto timeline = createTimelineFromData (rules, database, filter);
std::cout << jsonFromIntervals (timeline.tracked (rules)); std::cout << jsonFromIntervals (timeline.tracked (rules));
return 0; return 0;

View file

@ -79,7 +79,7 @@ int CmdReport (
throw std::string ("Specify which report to run."); throw std::string ("Specify which report to run.");
// Filter the data. // Filter the data.
auto filter = createFilterFromCLI (cli); auto filter = createFilterIntervalFromCLI (cli);
auto timeline = createTimelineFromData (rules, database, filter); auto timeline = createTimelineFromData (rules, database, filter);
auto intervals = timeline.tracked (rules); auto intervals = timeline.tracked (rules);

View file

@ -35,13 +35,12 @@ int CmdTrack (
Rules& rules, Rules& rules,
Database& database) Database& database)
{ {
auto filter = createFilterFromCLI (cli); auto filter = createFilterIntervalFromCLI (cli);
auto tracked = createIntervalFromFilter (filter); database.addInterval (filter);
database.addInterval (tracked);
// User feedback. // User feedback.
if (rules.getBoolean ("verbose")) if (rules.getBoolean ("verbose"))
std::cout << intervalSummarize (rules, tracked); std::cout << intervalSummarize (rules, filter);
return 0; return 0;
} }

View file

@ -115,17 +115,16 @@ bool expandIntervalHint (
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// A filter is a placeholder for a start datetime, end datetime and a set of // A filter is just another interval, containing start, end and tags.
// tags, which makes it essentially an interval.
// //
// Supported interval forms: // Supported interval forms:
// ["from"] <date> ["to"|"-" <date>] // ["from"] <date> ["to"|"-" <date>]
// ["from"] <date> "for" <duration> // ["from"] <date> "for" <duration>
// <duration> ["before"|"after" <date>] // <duration> ["before"|"after" <date>]
// //
Filter createFilterFromCLI (const CLI& cli) Interval createFilterIntervalFromCLI (const CLI& cli)
{ {
Filter filter; Interval filter;
std::string start; std::string start;
std::string end; std::string end;
std::string duration; std::string duration;
@ -275,20 +274,6 @@ Filter createFilterFromCLI (const CLI& cli)
return filter; return filter;
} }
////////////////////////////////////////////////////////////////////////////////
// An interval and a filter are almost identical.
// TODO Why aren't they identical? Fix this.
Interval createIntervalFromFilter (const Filter& filter)
{
Interval interval;
interval.range (filter.range ());
for (auto& tag : filter.tags ())
interval.tag (tag);
return interval;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// The five different overlap possibilities: // The five different overlap possibilities:
// //
@ -307,7 +292,7 @@ Interval createIntervalFromFilter (const Filter& filter)
Timeline createTimelineFromData ( Timeline createTimelineFromData (
const Rules& rules, const Rules& rules,
Database& database, Database& database,
const Filter& filter) const Interval& filter)
{ {
Timeline t; Timeline t;
t.range (filter.range ()); t.range (filter.range ());
@ -320,7 +305,7 @@ Timeline createTimelineFromData (
Interval i; Interval i;
i.initialize (line); i.initialize (line);
if (intervalMatchesFilter (i, filter)) if (intervalMatchesFilterInterval (i, filter))
t.include (i); t.include (i);
} }
else if (line[0] == 'e') else if (line[0] == 'e')
@ -350,17 +335,17 @@ Interval getLatestInterval (Database& database)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// An interval matches a filter if the start/end overlaps, and all filter tags // An interval matches a filter interval if the start/end overlaps, and all
// are found in the interval. // filter interval tags are found in the interval.
bool intervalMatchesFilter (const Interval& interval, const Filter& filter) bool intervalMatchesFilterInterval (const Interval& interval, const Interval& filter)
{ {
if ((filter.range ().start ().toEpoch () == 0 && if ((filter.start ().toEpoch () == 0 &&
filter.range ().end ().toEpoch () == 0) filter.end ().toEpoch () == 0)
|| ||
(interval.end () > filter.range ().start () && (interval.end () > filter.start () &&
interval.start () < filter.range ().end ())) interval.start () < filter.end ()))
{ {
for (auto& tag : filter.tags ()) for (auto& tag : filter.tags ())
if (! interval.hasTag (tag)) if (! interval.hasTag (tag))

View file

@ -32,7 +32,6 @@
#include <Rules.h> #include <Rules.h>
#include <Extensions.h> #include <Extensions.h>
#include <Interval.h> #include <Interval.h>
#include <Filter.h>
#include <Timeline.h> #include <Timeline.h>
#include <Color.h> #include <Color.h>
@ -48,11 +47,10 @@ int dispatchCommand (const CLI&, Database&, Rules&, const Extensions&);
Color tagColor (const Rules&, const std::string&); Color tagColor (const Rules&, const std::string&);
std::string intervalSummarize (const Rules&, const Interval&); std::string intervalSummarize (const Rules&, const Interval&);
bool expandIntervalHint (const std::string&, std::string&, std::string&); bool expandIntervalHint (const std::string&, std::string&, std::string&);
Filter createFilterFromCLI (const CLI&); Interval createFilterIntervalFromCLI (const CLI&);
Interval createIntervalFromFilter (const Filter&); Timeline createTimelineFromData (const Rules&, Database&, const Interval&);
Timeline createTimelineFromData (const Rules&, Database&, const Filter&);
Interval getLatestInterval (Database&); Interval getLatestInterval (Database&);
bool intervalMatchesFilter (const Interval&, const Filter&); bool intervalMatchesFilterInterval (const Interval&, const Interval&);
std::string jsonFromIntervals (const std::vector <Interval>&); std::string jsonFromIntervals (const std::vector <Interval>&);
std::vector <Daterange> rangesFromHolidays (const Rules&); std::vector <Daterange> rangesFromHolidays (const Rules&);
std::vector <Daterange> addRanges (const Daterange&, const std::vector <Daterange>&, const std::vector <Daterange>&); std::vector <Daterange> addRanges (const Daterange&, const std::vector <Daterange>&, const std::vector <Daterange>&);