-
Notifications
You must be signed in to change notification settings - Fork 447
/
UserMetric.cpp
373 lines (319 loc) · 9.95 KB
/
UserMetric.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright (c) 2015 Mark Liversedge (liversedge@gmail.com)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "RideMetric.h"
#include "UserMetricSettings.h"
#include "DataFilter.h"
UserMetric::UserMetric(Context *context, UserMetricSettings settings)
: RideMetric(), settings(settings)
{
// compile the program - built in a context that can close.
program = new DataFilter(NULL, context, settings.program);
program->refcount = 1;
root = program->root();
rt = &program->rt;
// lookup functions we need
finit = rt->functions.contains("init") ? rt->functions.value("init") : NULL;
frelevant = rt->functions.contains("relevant") ? rt->functions.value("relevant") : NULL;
fsample = rt->functions.contains("sample") ? rt->functions.value("sample") : NULL;
fbefore = rt->functions.contains("before") ? rt->functions.value("before") : NULL;
fafter = rt->functions.contains("after") ? rt->functions.value("after") : NULL;
fvalue = rt->functions.contains("value") ? rt->functions.value("value") : NULL;
fcount = rt->functions.contains("count") ? rt->functions.value("count") : NULL;
// we're not a clone, we're the original
clone_ = false;
}
UserMetric::UserMetric(const UserMetric *from) : RideMetric()
{
RideMetricFactory::instance().mutex.lock();
this->settings = from->settings;
this->program = from->program;
this->program->refcount++;
this->root = from->program->root();
this->finit = from->finit;
this->frelevant = from->frelevant;
this->fsample = from->fsample;
this->fbefore = from->fbefore;
this->fafter = from->fafter;
this->fvalue = from->fvalue;
this->fcount = from->fcount;
this->index_ = from->index_;
rt = new DataFilterRuntime;
// and copy it in an atomic operation
*rt = *from->rt;
// we are being cloned
clone_ = true;
RideMetricFactory::instance().mutex.unlock();
}
UserMetric::~UserMetric()
{
// program is shared, only delete when last is destroyed
RideMetricFactory::instance().mutex.lock();
if (program) {
program->refcount--;
if (!program->refcount) delete program;
}
if (clone_) delete rt;
RideMetricFactory::instance().mutex.unlock();
}
RideMetric *
UserMetric::clone() const
{
return new UserMetric(this);
}
void
UserMetric::initialize()
{
return; // nothing doing
}
QString
UserMetric::symbol() const
{
return settings.symbol;
}
// A short string suitable for showing to the user in the ride
// summaries, configuration dialogs, etc. It should be translated
// using tr().
QString
UserMetric::name() const
{
return settings.name;
}
QString
UserMetric::internalName() const
{
return settings.name;
}
QString
UserMetric::description() const
{
return settings.description;
}
RideMetric::MetricType
UserMetric::type() const
{
return static_cast<RideMetric::MetricType>(settings.type);
}
// units
QString
UserMetric::units(bool metric) const
{
return metric ? settings.unitsMetric : settings.unitsImperial;
}
// Factor to multiple value to convert from metric to imperial
double
UserMetric::conversion() const
{
return settings.conversion;
}
// And sum for example Fahrenheit from CentigradE
double
UserMetric::conversionSum() const
{
return settings.conversionSum;
}
// How many digits after the decimal we should show when displaying the
// value of a UserRideMetric.
int
UserMetric::precision() const
{
return settings.precision;
}
// Get the value and apply conversion if needed
double
UserMetric::value(bool metric) const
{
if (metric) return value();
else return (value() * conversion()) + conversionSum();
}
// Apply conversion if needed to the supplied value
double
UserMetric::value(double v, bool metric) const
{
if (metric) return v;
else return (v * conversion()) + conversionSum();
}
// for averages the count of items included in the average
double
UserMetric::count() const
{
return count_;
}
// when aggregating averages, should we include zeroes ? no by default
bool
UserMetric::aggregateZero() const
{
return settings.aggzero;
}
// is this metric relevant
bool
UserMetric::isRelevantForRide(const RideItem *item) const
{
if (item->context && root) {
if (frelevant) {
Result res = root->eval(rt, frelevant, 0, 0, const_cast<RideItem*>(item), NULL, NULL);
return res.number();
} else
return true;
}
return false;
}
// Compute the ride metric from a file.
void
UserMetric::compute(RideItem *item, Specification spec, const QHash<QString,RideMetric*> &pc)
{
QTime timer;
timer.start();
//qDebug()<<"CODE";
if (!root) {
setValue(RideFile::NIL);
setCount(0);
return;
}
// clear rt indexes
rt->indexes.clear();
// if there are no precomputed metrics then just use the values for the rideitem
// this is a specific use case when testing a user metric in preferences
const QHash<QString, RideMetric*> *c = NULL;
if (pc.count()) c=&pc;
//qDebug()<<"INIT";
// always init first
if (finit) root->eval(rt, finit, 0, 0, const_cast<RideItem*>(item), NULL, c, spec);
//qDebug()<<"CHECK";
// can it provide a value and is it relevant ?
if (!fvalue || !isRelevantForRide(item)) {
setValue(RideFile::NIL);
setCount(0);
return;
}
//qDebug()<<"BEFORE";
if (!spec.isEmpty(item->ride()) && fbefore) {
RideFileIterator it(item->ride(), spec, RideFileIterator::Before);
while(it.hasNext()) {
struct RideFilePoint *point = it.next();
root->eval(rt, fbefore, 0, 0, const_cast<RideItem*>(item), point, c, spec);
}
}
//qDebug()<<"SAMPLE";
// process samples, if there are any and a function exists
if (!spec.isEmpty(item->ride()) && fsample) {
RideFileIterator it(item->ride(), spec);
while(it.hasNext()) {
struct RideFilePoint *point = it.next();
root->eval(rt, fsample, 0, 0, const_cast<RideItem*>(item), point, c, spec);
}
}
//qDebug()<<"AFTER";
if (!spec.isEmpty(item->ride()) && fafter) {
RideFileIterator it(item->ride(), spec, RideFileIterator::After);
while(it.hasNext()) {
struct RideFilePoint *point = it.next();
root->eval(rt, fafter, 0, 0, const_cast<RideItem*>(item), point, c, spec);
}
}
//qDebug()<<"VALUE";
// value ?
if (fvalue) {
Result v = root->eval(rt, fvalue, 0, 0, const_cast<RideItem*>(item), NULL, c, spec);
setValue(v.number());
}
//qDebug()<<"COUNT";
// count?
if (fcount) {
Result n = root->eval(rt, fcount, 0, 0, const_cast<RideItem*>(item), NULL, c, spec);
setCount(n.number());
}
//qDebug()<<symbol()<<index_<<value_<<"ELAPSED="<<timer.elapsed()<<"ms";
}
bool
UserMetric::isTime() const
{
return settings.istime;
}
void
UserMetric::addCompatibility(QList<UserMetricSettings>&metrics)
{
// update the list of compatibility metrics
RideMetricFactory &f = RideMetricFactory::instance();
// reset, get added below ONLY if user didn't define them
f.compatibilitymetrics.clear();
// add metrics for backwards compatibility if they are not
// already defined by the user
bool hasc1=false, hasc2=false, hasc3=false;
foreach(UserMetricSettings x, metrics) {
if (x.name == "TSS") hasc1=true;
if (x.name == "IF") hasc2=true;
if (x.name == "NP") hasc3=true;
}
if (!hasc1) {
UserMetricSettings c1;
// add backwards compatibility metrics
c1.symbol = "compatibility_1";
c1.name = "TSS";
c1.description = "BikeStress";
c1.type = 0;
c1.unitsMetric = "";
c1.unitsImperial = "";
c1.conversion = 1;
c1.conversionSum = 0;
c1.program = "{ value { BikeStress; } count { Duration; } }";
c1.precision = 0;
c1.istime = false;
c1.aggzero = true;
c1.fingerprint = c1.symbol + DataFilter::fingerprint(c1.program);
metrics.insert(0, c1);
f.compatibilitymetrics << "TSS";
}
if (!hasc2) {
UserMetricSettings c2;
// add backwards compatibility metrics
c2.symbol = "compatibility_2";
c2.name = "IF";
c2.type = 1;
c2.description = "BikeIntensity";
c2.unitsMetric = "";
c2.unitsImperial = "";
c2.conversion = 1;
c2.conversionSum = 0;
c2.program = "{ value { BikeIntensity; } count { Duration; } }";
c2.precision = 3;
c2.istime = false;
c2.aggzero = true;
c2.fingerprint = c2.symbol + DataFilter::fingerprint(c2.program);
metrics.insert(0, c2);
f.compatibilitymetrics << "IF";
}
if (!hasc3) {
UserMetricSettings c3;
// add backwards compatibility metrics
c3.symbol = "compatibility_3";
c3.name = "NP";
c3.type = 1;
c3.description = "IsoPower";
c3.unitsMetric = "";
c3.unitsImperial = "";
c3.conversion = 1;
c3.conversionSum = 0;
c3.program = "{ value { IsoPower; } count { Duration; } }";
c3.precision = 0;
c3.istime = false;
c3.aggzero = true;
c3.fingerprint = c3.symbol + DataFilter::fingerprint(c3.program);
metrics.insert(0, c3);
f.compatibilitymetrics << "NP";
}
}