Permalink
Browse files

Access to Measure by days in Formulas

Eg. measure(Date-1, "Hrv", "RMSSD") to get the RMSSD
measure from Hrv group the day before the activity date.
Part 2 of #2588
  • Loading branch information...
amtriathlon committed Nov 3, 2017
1 parent fd090e6 commit e26a672755a72f7a116c2224b22662e25acb9e38
Showing with 33 additions and 1 deletion.
  1. +3 −0 doc/user/formula-syntax.txt
  2. +30 −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
@@ -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,8 @@ DataFilter::builtins()
returning << "XDATA(\"xdata\", \"series\", sparse|repeat|interpolate|resample)";
} else if (i == 41) {
returning << "XDATA_UNITS(\"xdata\", \"series\")";
} else if (i == 42) {
returning << "measure(date, \"group\", \"field\")";
} else {
function = DataFilterFunctions[i].name + "(";
for(int j=0; j<DataFilterFunctions[i].parameters; j++) {
@@ -2467,6 +2473,29 @@ 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
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
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 e26a672

Please sign in to comment.