Permalink
Browse files

Merge pull request #2682 from amtriathlon/Measures

Access to Measure by days in Formulas
  • Loading branch information...
amtriathlon committed Nov 6, 2017
2 parents fd090e6 + c7d4f33 commit fd2f54dea9d237d67f2ca06ebe6e382941be4ca3
Showing with 47 additions and 10 deletions.
  1. +3 −0 doc/user/formula-syntax.txt
  2. +4 −4 src/Core/Athlete.cpp
  3. +5 −5 src/Core/Athlete.h
  4. +35 −1 src/Core/DataFilter.cpp
@@ -81,6 +81,9 @@ DATA PROCESSOR FUNCTIONS
autoprocess(filter)
postprocess(processor, filter)
DAILY MEASURES ACCESS
measure(date, "group", "field")
RIDE SAMPLE DATA
SECS
View
@@ -634,29 +634,29 @@ Athlete::getHrvMeasure(QDate date, int field, bool useMetricUnits)
// Common access to Measures
QStringList
Athlete::getMeasureGroupSymbols() const
Athlete::getMeasureGroupSymbols()
{
const QStringList groups = QStringList() << "Body" << "Hrv";
return groups;
}
QStringList
Athlete::getMeasureGroupNames() const
Athlete::getMeasureGroupNames()
{
const QStringList groups = QStringList() << tr("Body") << tr("Hrv");
return groups;
}
QStringList
Athlete::getMeasureFieldSymbols(int group) const
Athlete::getMeasureFieldSymbols(int group)
{
if (group == 0) return BodyMeasure::getFieldSymbols();
else if (group == 1) return HrvMeasure::getFieldSymbols();
else return QStringList();
}
QStringList
Athlete::getMeasureFieldNames(int group) const
Athlete::getMeasureFieldNames(int group)
{
if (group == 0) return BodyMeasure::getFieldNames();
else if (group == 1) return HrvMeasure::getFieldNames();
View
@@ -159,11 +159,11 @@ class Athlete : public QObject
void getHrvMeasure(QDate date, HrvMeasure&);
// Common access to Measures
QStringList getMeasureGroupSymbols() const;
QStringList getMeasureGroupNames() const;
QStringList getMeasureFieldSymbols(int group) const;
QStringList getMeasureFieldNames(int group) const;
QString getMeasureUnits(int group, int field, bool useMetricUnits=true);
static QStringList getMeasureGroupSymbols();
static QStringList getMeasureGroupNames();
static QStringList getMeasureFieldSymbols(int group);
static QStringList getMeasureFieldNames(int group);
static QString getMeasureUnits(int group, int field, bool useMetricUnits=true);
double getMeasureValue(QDate date, int group, int field, bool useMetricUnits=true);
// ride collection
View
@@ -111,9 +111,13 @@ static struct {
{ "autoprocess", 1 }, // autoprocess(filter) to run auto data processors
{ "postprocess", 2 }, // postprocess(processor, filter) to run processor
// add new ones above this line
// XDATA access
{ "XDATA_UNITS", 2 }, // e.g. xdata("WEATHER", "HUMIDITY") returns "Relative Humidity"
// Daily Measures Access by date
{ "measure", 3 }, // measure(DATE, "Hrv", "RMSSD")
// add new ones above this line
{ "", -1 }
};
@@ -152,6 +156,11 @@ DataFilter::builtins()
returning << "XDATA(\"xdata\", \"series\", sparse|repeat|interpolate|resample)";
} else if (i == 41) {
returning << "XDATA_UNITS(\"xdata\", \"series\")";
} else if (i == 42) {
QStringList groupSymbols = Athlete::getMeasureGroupSymbols();
for (int g=0; g<groupSymbols.count(); g++)
foreach (QString fieldSymbol, Athlete::getMeasureFieldSymbols(g))
returning << QString("measure(Date, \"%1\", \"%2\")").arg(groupSymbols[g]).arg(fieldSymbol);
} else {
function = DataFilterFunctions[i].name + "(";
for(int j=0; j<DataFilterFunctions[i].parameters; j++) {
@@ -2467,6 +2476,31 @@ Result Leaf::eval(DataFilterRuntime *df, Leaf *leaf, float x, RideItem *m, RideF
} else return Result(""); // not for filtering
}
break;
case 42 :
{ // MEASURE (DATE, GROUP, FIELD) get measure
if (leaf->fparms.count() < 3) return Result(0);
Result days = eval(df, leaf->fparms[0], x, m, p, c);
if (!days.isNumber) return Result(0); // invalid date
QDate date = QDate(1900,01,01).addDays(days.number);
if (!date.isValid()) return Result(0); // invalid date
if (leaf->fparms[1]->type != String) return Result(0);
QString group_symbol = *(leaf->fparms[1]->lvalue.s);
int group = m->context->athlete->getMeasureGroupSymbols().indexOf(group_symbol);
if (group < 0) return Result(0); // unknown group
if (leaf->fparms[2]->type != String) return Result(0);
QString field_symbol = *(leaf->fparms[2]->lvalue.s);
int field = m->context->athlete->getMeasureFieldSymbols(group).indexOf(field_symbol);
if (field < 0) return Result(0); // unknown field
// retrieve measure value
double value = m->context->athlete->getMeasureValue(date, group, field);
return Result(value);
}
break;
default:
return Result(0);
}

0 comments on commit fd2f54d

Please sign in to comment.