From 098a18d980874e69e7cd07f93386c0e2dabfe861 Mon Sep 17 00:00:00 2001 From: Damien Date: Fri, 16 Nov 2012 23:49:20 +0100 Subject: [PATCH] Refresh metrics for rides after a new weight form Withings download --- src/MetricAggregator.cpp | 10 +++++++- src/MetricAggregator.h | 3 ++- src/WithingsDownload.cpp | 51 ++++++++++++++++++++++++++++++---------- src/WithingsDownload.h | 3 +++ src/ZeoDownload.cpp | 1 + 5 files changed, 54 insertions(+), 14 deletions(-) diff --git a/src/MetricAggregator.cpp b/src/MetricAggregator.cpp index 84a2a4f86..924226fda 100644 --- a/src/MetricAggregator.cpp +++ b/src/MetricAggregator.cpp @@ -59,7 +59,14 @@ MetricAggregator::~MetricAggregator() // used to store timestamp and fingerprint used in database struct status { unsigned long timestamp, fingerprint; }; +// Refresh not up to date metrics void MetricAggregator::refreshMetrics() +{ + refreshMetrics(QDateTime()); +} + +// Refresh not up to date metrics and metrics after date +void MetricAggregator::refreshMetrics(QDateTime forceAfterThisDate) { // only if we have established a connection to the database if (dbaccess == NULL || main->isclean==true) return; @@ -143,7 +150,8 @@ void MetricAggregator::refreshMetrics() QApplication::processEvents(); if (dbTimeStamp < QFileInfo(file).lastModified().toTime_t() || - zoneFingerPrint != fingerprint) { + zoneFingerPrint != fingerprint || + (!forceAfterThisDate.isNull() && name >= forceAfterThisDate.toString("yyyy_MM_dd_hh_mm_ss"))) { QStringList errors; // log diff --git a/src/MetricAggregator.h b/src/MetricAggregator.h index f0d6112a1..840602014 100644 --- a/src/MetricAggregator.h +++ b/src/MetricAggregator.h @@ -43,7 +43,8 @@ class MetricAggregator : public QObject ~MetricAggregator(); - void refreshMetrics(); + void refreshMetrics(); + void refreshMetrics(QDateTime forceAfterThisDate); void getFirstLast(QDate &, QDate &); DBAccess *db() { return dbaccess; } SummaryMetrics getAllMetricsFor(QString filename); // for a single ride diff --git a/src/WithingsDownload.cpp b/src/WithingsDownload.cpp index 9724fe498..77f3811dd 100644 --- a/src/WithingsDownload.cpp +++ b/src/WithingsDownload.cpp @@ -49,24 +49,51 @@ WithingsDownload::downloadFinished(QNetworkReply *reply) QString text = reply->readAll(); QStringList errors; parser->parse(text, errors); - QString status = QString(tr("%1 measurements received.")).arg(parser->readings().count()); - QMessageBox::information(main, tr("Withings Data Download"), status); //main->metricDB->db()->connection().transaction(); + newMeasures = 0; + allMeasures = parser->readings().count(); + QDateTime olderDate; + - // debug output to examine data downloaded foreach (WithingsReading x, parser->readings()) { - SummaryMetrics add; - add.setDateTime(x.when); - add.setText("Weight", QString("%1").arg(x.weightkg)); - add.setText("Height", QString("%1").arg(x.sizemeter)); - add.setText("Lean Mass", QString("%1").arg(x.leankg)); - add.setText("Fat Mass", QString("%1").arg(x.fatkg)); - add.setText("Fat Ratio", QString("%1").arg(x.fatpercent)); + QList list = main->metricDB->getAllMeasuresFor(x.when,x.when); + bool presentOrEmpty = false; + for (int i=0;i0)) && + ((x.sizemeter == 0) || (sm.getText("Height", "").length()>0)) && + ((x.leankg>0) || (sm.getText("Lean Mass", "").length()>0)) && + ((x.fatkg>0) || (sm.getText("Fat Mass", "").length()>0)) && + ((x.fatpercent>0) || (sm.getText("Fat Ratio", "").length()>0))) { + presentOrEmpty = true; + } + } - main->metricDB->importMeasure(&add); + if (!presentOrEmpty) { + newMeasures ++; + SummaryMetrics add; + add.setDateTime(x.when); + add.setText("Weight", QString("%1").arg(x.weightkg)); + add.setText("Height", QString("%1").arg(x.sizemeter)); + add.setText("Lean Mass", QString("%1").arg(x.leankg)); + add.setText("Fat Mass", QString("%1").arg(x.fatkg)); + add.setText("Fat Ratio", QString("%1").arg(x.fatpercent)); + + main->metricDB->importMeasure(&add); + + if (olderDate.isNull() || x.whenisclean = false; + main->metricDB->refreshMetrics(olderDate); } - //main->metricDB->db()->connection().commit(); return; } diff --git a/src/WithingsDownload.h b/src/WithingsDownload.h index d53a45dcd..bdadbf54d 100644 --- a/src/WithingsDownload.h +++ b/src/WithingsDownload.h @@ -45,5 +45,8 @@ private: MainWindow *main; QNetworkAccessManager *nam; WithingsParser *parser; + + int allMeasures; + int newMeasures; }; #endif diff --git a/src/ZeoDownload.cpp b/src/ZeoDownload.cpp index 5d96a1498..54702c433 100644 --- a/src/ZeoDownload.cpp +++ b/src/ZeoDownload.cpp @@ -17,6 +17,7 @@ */ #include "ZeoDownload.h" +#include "MetricAggregator.h" #include ZeoDownload::ZeoDownload(MainWindow *main) : main(main)