From e6c4099f5bb1565a770e55d1dd671d846dcbd68b Mon Sep 17 00:00:00 2001 From: David Llewellyn-Jones Date: Mon, 23 Jul 2018 00:14:35 +0100 Subject: [PATCH] Added new graphs Journey proportions per hour. Congestion by hour. --- harbour-pedalo.pro | 8 +++- src/harbour-pedalo.cpp | 8 ++++ src/statshourcongestion.cpp | 67 ++++++++++++++++++++++++++++++++++ src/statshourcongestion.h | 18 +++++++++ src/statshourjourneys.cpp | 60 ++++++++++++++++++++++++++++++ src/statshourjourneys.h | 18 +++++++++ src/statsweekdayave.cpp | 2 +- src/statsweekdayave.h | 1 - src/statsweekdaycongestion.cpp | 9 +++-- src/statsweekdaycongestion.h | 1 - 10 files changed, 183 insertions(+), 9 deletions(-) create mode 100644 src/statshourcongestion.cpp create mode 100644 src/statshourcongestion.h create mode 100644 src/statshourjourneys.cpp create mode 100644 src/statshourjourneys.h diff --git a/harbour-pedalo.pro b/harbour-pedalo.pro index 7c01192..bc118dd 100644 --- a/harbour-pedalo.pro +++ b/harbour-pedalo.pro @@ -37,7 +37,9 @@ SOURCES += src/harbour-pedalo.cpp \ src/statsmodel.cpp \ src/stats.cpp \ src/statsweekdayave.cpp \ - src/statsweekdaycongestion.cpp + src/statsweekdaycongestion.cpp \ + src/statshourcongestion.cpp \ + src/statshourjourneys.cpp DISTFILES += qml/harbour-pedalo.qml \ qml/cover/CoverPage.qml \ @@ -83,4 +85,6 @@ HEADERS += \ src/statsmodel.h \ src/stats.h \ src/statsweekdayave.h \ - src/statsweekdaycongestion.h + src/statsweekdaycongestion.h \ + src/statshourcongestion.h \ + src/statshourjourneys.h diff --git a/src/harbour-pedalo.cpp b/src/harbour-pedalo.cpp index 35fc77f..f19a8fc 100644 --- a/src/harbour-pedalo.cpp +++ b/src/harbour-pedalo.cpp @@ -13,7 +13,9 @@ #include "imageprovider.h" #include "graph.h" #include "statsweekdayave.h" +#include "statshourjourneys.h" #include "statsweekdaycongestion.h" +#include "statshourcongestion.h" #include "harbour-pedalo.h" @@ -51,9 +53,15 @@ int main(int argc, char *argv[]) StatsWeekdayAve statsweekdayave(&journeys); statsmodel.addStats(statsweekdayave); + StatsHourJourneys statshourjourneys(&journeys); + statsmodel.addStats(statshourjourneys); + StatsWeekdayCongestion statsweekdaycongestion(&journeys); statsmodel.addStats(statsweekdaycongestion); + StatsHourCongestion statshourcongestion(&journeys); + statsmodel.addStats(statshourcongestion); + QFile file; file.setFileName(Settings::getConfigDir() + "/journeys.csv"); journeys.importFromFile(file); diff --git a/src/statshourcongestion.cpp b/src/statshourcongestion.cpp new file mode 100644 index 0000000..a98230e --- /dev/null +++ b/src/statshourcongestion.cpp @@ -0,0 +1,67 @@ +#include + +#include "statshourcongestion.h" + +#define LOWESTHOUR (7) +#define HIGHESTHOUR (21) + +StatsHourCongestion::StatsHourCongestion(JourneyModel * journeys) : + journeys(journeys) +{ + title = "Congestion by hour (cycles per hour)"; + units = ""; + + labels.clear(); + for (int hour = LOWESTHOUR; hour <= HIGHESTHOUR; hour++) { + labels << QString::number(hour); + } +} + +void StatsHourCongestion::update() { + double passed[24]; + unsigned int count[24]; + int pos; + + qDebug() << "Calculating values"; + values.clear(); + + for (pos = 0; pos < 24; pos++) { + passed[pos] = 0.0; + count[pos] = 0; + } + + foreach (Journey const &journey, journeys->getData()) { + QTime time = journey.getStartTime(); + int hour = time.hour(); + int startmin = time.minute(); + int duration = (journey.getDuration() / 60); + int remaining = duration; + + while (remaining > 0) { + // toadd is always greater than 0, so the loop is guaranteed to exit + unsigned int toadd = (startmin + remaining) < 60 ? remaining : 60 - startmin; + passed[hour] += (double)toadd * ((double)(journey.getOvertook() + journey.getOvertakenBy()) / duration); + count[hour] += toadd; + remaining -= toadd; + startmin = 0; + + hour = (hour + 1) % 24; + } + } + + maxval = 0.0; + for (pos = LOWESTHOUR; pos <= HIGHESTHOUR; pos++) { + float result = 0.0f; + if (count[pos] > 0) { + result = (float)(60.0 * passed[pos] / (double)count[pos]); + } + if (result > maxval) { + maxval = result; + } + values << result; + } + + step = (maxval > 5.0) ? qRound(maxval / 5.0) : (maxval / 5.0); + + qDebug() << "Calculated values"; +} diff --git a/src/statshourcongestion.h b/src/statshourcongestion.h new file mode 100644 index 0000000..ce64257 --- /dev/null +++ b/src/statshourcongestion.h @@ -0,0 +1,18 @@ +#ifndef STATSHOURCONGESTION_H +#define STATSHOURCONGESTION_H + +#include "journeymodel.h" +#include "stats.h" + +class StatsHourCongestion : public Stats +{ +public: + StatsHourCongestion(JourneyModel * journeys); + + void update(); + +private: + JourneyModel * journeys; +}; + +#endif // STATSHOURCONGESTION_H diff --git a/src/statshourjourneys.cpp b/src/statshourjourneys.cpp new file mode 100644 index 0000000..f8616cb --- /dev/null +++ b/src/statshourjourneys.cpp @@ -0,0 +1,60 @@ +#include + +#include "statshourjourneys.h" + +#define LOWESTHOUR (7) +#define HIGHESTHOUR (21) + +StatsHourJourneys::StatsHourJourneys(JourneyModel * journeys) : + journeys(journeys) +{ + title = "Journey proportions per hour (%)"; + units = "%"; + + labels.clear(); + for (int hour = LOWESTHOUR; hour <= HIGHESTHOUR; hour++) { + labels << QString::number(hour); + } +} + +void StatsHourJourneys::update() { + unsigned int minsperhour[24]; + quint64 totalmins; + int pos; + + qDebug() << "Calculating values"; + values.clear(); + + for (pos = 0; pos < 24; pos++) { + minsperhour[pos] = 0u; + } + + totalmins = 0u; + foreach (Journey const &journey, journeys->getData()) { + QTime time = journey.getStartTime(); + int hour = time.hour(); + int startmin = time.minute(); + int remaining = (journey.getDuration() / 60); + totalmins += remaining; + + while (remaining > 0) { + // toadd is always greater than 0, so the loop is guaranteed to exit + unsigned int toadd = (startmin + remaining) < 60 ? remaining : 60 - startmin; + minsperhour[hour] += toadd; + remaining -= toadd; + startmin = 0; + + hour = (hour + 1) % 24; + } + } + + for (pos = LOWESTHOUR; pos <= HIGHESTHOUR; pos++) { + float result = 0.0f; + if (totalmins > 0) { + result = ((double)minsperhour[pos] / (double)totalmins); + } + values << result; + } + + qDebug() << "Calculated values"; +} diff --git a/src/statshourjourneys.h b/src/statshourjourneys.h new file mode 100644 index 0000000..ad42832 --- /dev/null +++ b/src/statshourjourneys.h @@ -0,0 +1,18 @@ +#ifndef STATSHOURJOURNEYS_H +#define STATSHOURJOURNEYS_H + +#include "journeymodel.h" +#include "stats.h" + +class StatsHourJourneys : public Stats +{ +public: + StatsHourJourneys(JourneyModel * journeys); + + void update(); + +private: + JourneyModel * journeys; +}; + +#endif // STATSHOURJOURNEYS_H diff --git a/src/statsweekdayave.cpp b/src/statsweekdayave.cpp index 63e595e..527cb1d 100644 --- a/src/statsweekdayave.cpp +++ b/src/statsweekdayave.cpp @@ -44,7 +44,7 @@ void StatsWeekdayAve::update() { values << result; } - step = qRound(maxval / 5.0); + step = maxval > 5.0 ? qRound(maxval / 5.0) : (maxval / 5.0); qDebug() << "Calculated values"; } diff --git a/src/statsweekdayave.h b/src/statsweekdayave.h index 1a36d32..5e5a63a 100644 --- a/src/statsweekdayave.h +++ b/src/statsweekdayave.h @@ -2,7 +2,6 @@ #define STATSWEEKDAYAVE_H #include "journeymodel.h" - #include "stats.h" class StatsWeekdayAve : public Stats diff --git a/src/statsweekdaycongestion.cpp b/src/statsweekdaycongestion.cpp index 685e175..c4842b3 100644 --- a/src/statsweekdaycongestion.cpp +++ b/src/statsweekdaycongestion.cpp @@ -5,13 +5,13 @@ StatsWeekdayCongestion::StatsWeekdayCongestion(JourneyModel * journeys) : journeys(journeys) { - title = "Congested days (cycles passed)"; + title = "Congestion by day (cycles per hour)"; units = ""; labels = QStringList{"M", "T", "W", "Th", "F", "S", "Su"}; } void StatsWeekdayCongestion::update() { - quint32 passed[7]; + double passed[7]; unsigned int count[7]; int pos; @@ -26,8 +26,9 @@ void StatsWeekdayCongestion::update() { foreach (Journey const &journey, journeys->getData()) { QDate date = journey.getStartDate(); int dayofweek = date.dayOfWeek() - 1; + double duration = journey.getDuration() / (60.0 * 60.0); if (dayofweek >= 0) { - passed[dayofweek] += journey.getOvertook() + journey.getOvertakenBy(); + passed[dayofweek] += (journey.getOvertook() + journey.getOvertakenBy()) / duration; count[dayofweek]++; } } @@ -44,7 +45,7 @@ void StatsWeekdayCongestion::update() { values << result; } - step = qRound(maxval / 5.0); + step = maxval > 5.0 ? qRound(maxval / 5.0) : (maxval / 5.0); qDebug() << "Calculated values"; } diff --git a/src/statsweekdaycongestion.h b/src/statsweekdaycongestion.h index 2b0dda2..1e53cff 100644 --- a/src/statsweekdaycongestion.h +++ b/src/statsweekdaycongestion.h @@ -2,7 +2,6 @@ #define STATSWEEKDAYCONGESTION_H #include "journeymodel.h" - #include "stats.h" class StatsWeekdayCongestion : public Stats -- 2.25.1