add computeMetrics and freeMemory functions

computeMetrics computes the RideMetrics without building up the htmlSummary.
freeMemory frees the rideFile object associated with this ride.
This commit is contained in:
Sean Rhea
2009-10-25 17:40:03 -04:00
parent b144b5ec48
commit 0d45c46a2a
2 changed files with 72 additions and 33 deletions

View File

@@ -31,7 +31,7 @@ RideItem::RideItem(int type,
QString path, QString fileName, const QDateTime &dateTime,
Zones **zones, QString notesFileName) :
QTreeWidgetItem(type), path(path), fileName(fileName),
dateTime(dateTime), zones(zones), notesFileName(notesFileName)
dateTime(dateTime), ride(NULL), zones(zones), notesFileName(notesFileName)
{
setText(0, dateTime.toString("ddd"));
setText(1, dateTime.toString("MMM d, yyyy"));
@@ -176,6 +176,73 @@ static const char *metricsXml =
" </metric_group>\n"
"</metrics>\n";
void
RideItem::freeMemory()
{
if (ride) {
delete ride;
ride = NULL;
}
}
void
RideItem::computeMetrics()
{
const QDateTime nilTime;
if ((computeMetricsTime != nilTime) &&
(!zones || !*zones || (computeMetricsTime >= (*zones)->modificationTime))) {
return;
}
if (!ride) {
QFile file(path + "/" + fileName);
QStringList errors;
ride = RideFileFactory::instance().openRideFile(file, errors);
if (!ride)
return;
}
computeMetricsTime = QDateTime::currentDateTime();
int zone_range = -1;
int num_zones = 0;
if (zones && *zones &&
((zone_range = (*zones)->whichRange(dateTime.date())) >= 0)) {
num_zones = (*zones)->numZones(zone_range);
}
const RideMetricFactory &factory = RideMetricFactory::instance();
QSet<QString> todo;
// hack djconnel: do the metrics TWICE, to catch dependencies
// on displayed variables. Presently if a variable depends on zones,
// for example, and zones change, the value may be considered still
// value even though it will change. This is presently happening
// where bikescore depends on relative intensity.
// note metrics are only calculated if zones are defined
for (int metriciteration = 0; metriciteration < 2; metriciteration ++) {
for (int i = 0; i < factory.metricCount(); ++i) {
todo.insert(factory.metricName(i));
while (!todo.empty()) {
QMutableSetIterator<QString> i(todo);
later:
while (i.hasNext()) {
const QString &name = i.next();
const QVector<QString> &deps = factory.dependencies(name);
for (int j = 0; j < deps.size(); ++j)
if (!metrics.contains(deps[j]))
goto later;
RideMetric *metric = factory.newMetric(name);
metric->compute(ride, *zones, zone_range, metrics);
metrics.insert(name, metric);
i.remove();
}
}
}
}
}
QString
RideItem::htmlSummary()
{
@@ -201,6 +268,7 @@ RideItem::htmlSummary()
+ dateTime.toString("dddd MMMM d, yyyy, h:mm AP")
+ "</h2><h3>Device Type: " + ride->deviceType() + "</h3>");
computeMetrics();
boost::shared_ptr<QSettings> settings = GetApplicationSettings();
QVariant unit = settings->value(GC_UNIT);
@@ -214,37 +282,6 @@ RideItem::htmlSummary()
time_in_zone.clear();
time_in_zone.resize(num_zones);
}
const RideMetricFactory &factory = RideMetricFactory::instance();
QSet<QString> todo;
// hack djconnel: do the metrics TWICE, to catch dependencies
// on displayed variables. Presently if a variable depends on zones,
// for example, and zones change, the value may be considered still
// value even though it will change. This is presently happening
// where bikescore depends on relative intensity.
// note metrics are only calculated if zones are defined
for (int metriciteration = 0; metriciteration < 2; metriciteration ++) {
for (int i = 0; i < factory.metricCount(); ++i) {
todo.insert(factory.metricName(i));
while (!todo.empty()) {
QMutableSetIterator<QString> i(todo);
later:
while (i.hasNext()) {
const QString &name = i.next();
const QVector<QString> &deps = factory.dependencies(name);
for (int j = 0; j < deps.size(); ++j)
if (!metrics.contains(deps[j]))
goto later;
RideMetric *metric = factory.newMetric(name);
metric->compute(ride, *zones, zone_range, metrics);
metrics.insert(name, metric);
i.remove();
}
}
}
}
double secs_watts = 0.0;

View File

@@ -37,7 +37,7 @@ class RideItem : public QTreeWidgetItem {
QString fileName;
QDateTime dateTime;
QString summary;
QDateTime summaryGenerationTime;
QDateTime summaryGenerationTime, computeMetricsTime;
RideFile *ride;
Zones **zones;
QString notesFileName;
@@ -53,7 +53,9 @@ class RideItem : public QTreeWidgetItem {
~RideItem();
void computeMetrics();
QString htmlSummary();
void freeMemory();
int zoneRange();
int numZones();