#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)
{
}
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();
+ double average = 0.0;
+
+ if (count > 0) {
+ average = ((double)time / (double)count);
+ }
+ else {
+ average = 0.0;
+ }
+
+ return average;
+}
+
+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();
+ }
+
+ if (overtook + overtakenby > 0.0) {
+ percentile = (double)overtook / (double)(overtook + overtakenby);
+ }
+ else {
+ percentile = 0.0;
+ }
+
+ 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];
+ }
+
+ 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]);
+ }
+ }
+
+ if (formatted == "") {
+ formatted = "None";
+ }
+
+ return formatted;
+}
+
+QList<float> Status::getGraphData() {
+ static QList<float> data({0.1, 0.2, 0.3, 0.4, 1.0, 0.8, 0.7, 0.9, 0.5, 1.0});
+
+ return data;
+}