X-Git-Url: https://www.flypig.org.uk/git/?p=harbour-pedalo.git;a=blobdiff_plain;f=src%2Fstatshourcongestion.cpp;fp=src%2Fstatshourcongestion.cpp;h=a98230e4f479c4622155f4167762498c7935d682;hp=0000000000000000000000000000000000000000;hb=e6c4099f5bb1565a770e55d1dd671d846dcbd68b;hpb=dc14479d561196e19417c4ecf78e847ec4b43b7c 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"; +}