Display overall statistics
[harbour-pedalo.git] / src / status.cpp
index 2aa9f7a..b4bb630 100644 (file)
@@ -1,10 +1,12 @@
 #include <QDateTime>
+#include <QDebug>
 
 #include "status.h"
 
-Status::Status(QObject *parent) : QObject(parent),
+Status::Status(JourneyModel &journeymodel, QObject *parent) : QObject(parent),
   cycling(false),
-  startTime(0u)
+  startTime(0u),
+  journeymodel(journeymodel)
 {
 
 }
@@ -35,3 +37,80 @@ void Status::startJourney() {
     setCycling(true);
     setStartTime(QDateTime::currentMSecsSinceEpoch());
 }
+
+quint64 Status::getJourneyCount() const {
+    return journeymodel.rowCount();
+}
+
+quint64 Status::getTimeSpentCycling() const {
+    quint64 time;
+    QList<Journey> const & journeys = journeymodel.getData();
+
+    time = 0u;
+    foreach(Journey journey, journeys) {
+        time += journey.getDuration();
+    }
+
+    return time;
+}
+
+double Status::getAverageDuration() const {
+    quint64 time = getTimeSpentCycling();
+    quint64 count = Status::getJourneyCount();
+
+    return ((double)time / (double)count);
+}
+
+double Status::getSpeedPercentile() const {
+    quint64 overtook;
+    quint64 overtakenby;
+    QList<Journey> const & journeys = journeymodel.getData();
+    double percentile;
+
+    overtook = 0u;
+    overtakenby = 0u;
+    foreach(Journey journey, journeys) {
+        overtook += journey.getOvertook();
+        overtakenby += journey.getOvertakenBy();
+    }
+
+    percentile = (double)overtook / (double)(overtook + overtakenby);
+
+    return percentile;
+}
+
+QString Status::getFormattedTime(quint64 seconds, int min, int max) {
+    static const QString plural[5] = {"s", "m", "h", "d", "y"};
+    static const QString singular[5] = {"s", "m", "h", "d", "y"};
+    static const quint64 base[5] = {60, 60, 24, 365, (quint64)-1};
+    quint64 remaining;
+    quint64 portion;
+    QString formatted;
+    QList<quint64> portions;
+
+    min = qBound(0, min, static_cast<int>(sizeof(base)));
+    max = qBound(0, max, static_cast<int>(sizeof(base)));
+
+    remaining = seconds;
+    for (int unit = 0; unit < max; unit++) {
+        portion = (unit == max - 1) ? remaining : remaining % base[unit];
+        portions << portion;
+        remaining /= base[unit];
+        qDebug() << plural[unit] << ": " << portion;
+    }
+
+    formatted = "";
+    for (int unit = max - 1; unit >= min; unit--) {
+        portion = portions[unit];
+
+        if (portion != 0) {
+            if (formatted.length() > 0) {
+                formatted += " ";
+            }
+            formatted += QString::number(portion) + " " + ((portion == 1) ? singular[unit] : plural[unit]);
+        }
+    }
+
+    return formatted;
+}
+