Skip to content

Commit

Permalink
DataFilter - Add events function to get current season events
Browse files Browse the repository at this point in the history
events(date|name|priority|description) returns a vector with the
required field for each event in the current date range span.
For example, to create vertical lines in a Trends User Chart:
sapply(events(date), { annotate(vline, events(name)[i], solid, x); });
  • Loading branch information
amtriathlon committed Jul 27, 2022
1 parent d282d1f commit e738b81
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/Core/DataFilter.cpp
Expand Up @@ -38,6 +38,7 @@
#include "LTMTrend.h" // for LR when copying CP chart filtering mechanism
#include "WPrime.h" // for LR when copying CP chart filtering mechanism
#include "FastKmeans.h" // for kmeans(...)
#include "Season.h" // for events(...)

#ifdef GC_HAVE_SAMPLERATE
// we have libsamplerate
Expand Down Expand Up @@ -366,6 +367,9 @@ static struct {
// - same as intervals above but instead of returning a vector of numbers, the values
// are converted to strings as appropriate for the metric (e.g. Pace_Rowing mm:ss/500m).

{ "events", 0 }, // events(name|date|priority|description)
// - returns a vector of values for the field specified for each even in current date range

{ "powerindex", 2 }, // powerindex(power, secs) - returns an array or value representing the power and duration
// represented as a power index

Expand Down Expand Up @@ -2236,6 +2240,25 @@ void Leaf::validateFilter(Context *context, DataFilterRuntime *df, Leaf *leaf)
DataFiltererrors << QString(tr("too many parameters: %1(symbol, start, stop)").arg(leaf->function));
}

} else if (leaf->function == "events") {

// is the param a symbol
if (leaf->fparms.count() != 1 || leaf->fparms[0]->type != Leaf::Symbol) {
leaf->inerror = true;
DataFiltererrors << QString(tr("%1(name|date|priority|description)").arg(leaf->function));

} else if (leaf->fparms.count() == 1) {

QRegExp symbols("^(name|date|priority|description)$");
QString symbol=*(leaf->fparms[0]->lvalue.n);
if (!symbols.exactMatch(symbol) && df->lookupMap.value(symbol,"") == "") {
leaf->inerror = true;
DataFiltererrors << QString(tr("invalid symbol '%1', should be 'name|date|priority|description''").arg(symbol));

}

}

} else if (leaf->function == "bests") {

int po=0;
Expand Down Expand Up @@ -5103,6 +5126,38 @@ Result Leaf::eval(DataFilterRuntime *df, Leaf *leaf, const Result &x, long it, R
return returning;
}

if (leaf->function == "events") {

// symbol determines what to return
QString symbol = *(leaf->fparms[0]->lvalue.n);

// returning numbers or strings
Result returning(0);
if (symbol != "date") returning.isNumber = false;

QList<Season> tmpSeasons = m->context->athlete->seasons->seasons;
std::sort(tmpSeasons.begin(),tmpSeasons.end(),Season::LessThanForStarts);
foreach (Season s, tmpSeasons) {
foreach (SeasonEvent event, s.events) {
if (event.date >= d.from && event.date <= d.to) {
if (symbol == "date") {
int value = QDate(1900,01,01).daysTo(event.date);
returning.number() += value;
returning.asNumeric().append(value);
} if (symbol == "name") {
returning.asString().append(event.name);
} if (symbol == "priority") {
returning.asString().append(SeasonEvent::priorityList().at(event.priority));
} if (symbol == "description") {
returning.asString().append(event.description);
}
}
}
}

return returning;
}

// measures
if (leaf->function == "measures") {

Expand Down

0 comments on commit e738b81

Please sign in to comment.