diff --git a/src/reporting_thread.cpp b/src/reporting_thread.cpp index 107c7ca..2132575 100644 --- a/src/reporting_thread.cpp +++ b/src/reporting_thread.cpp @@ -8,6 +8,7 @@ #include #include +#include ReportingThread::ReportingThread(hwapi *hw) : m_hw(hw) @@ -99,14 +100,27 @@ void ReportingThread::run() { #endif - uint16_t const totalBlocks = m_hw->dcDownloadGetTotalBlockNumber(); + uint16_t totalBlocks = m_hw->dcDownloadGetTotalBlockNumber(); + cnt = 10; + while(--cnt > 0 && totalBlocks == 0) { + totalBlocks = m_hw->dcDownloadGetTotalBlockNumber(); + qCritical() << QDateTime::currentDateTime() << __PRETTY_FUNCTION__ + << QString("line=%1 TOTAL BLOCKS=%2 (%3)") + .arg(__LINE__).arg(totalBlocks).arg(cnt); + QThread::sleep(1); + } - qCritical() << QDateTime::currentDateTime() << "TOTAL BLOCKS" << totalBlocks; + qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate) << __PRETTY_FUNCTION__ + << QString("line=%1 TOTAL BLOCKS=%2").arg(__LINE__).arg(totalBlocks); if (totalBlocks) { QDateTime const start = QDateTime::currentDateTime(); uint16_t currentBlockNumber = 0; uint16_t prevBlockNumber = ~0; + uint64_t estimatedMinutesLeftMax = ~0ULL; + uint64_t estimatedSecondsLeftMax = ~0ULL; + uint64_t estimatedMinutesLeftPrev = 0; + uint64_t estimatedSecondsLeftPrev = 0; while (m_hw->dcDownloadGetRunning()) { currentBlockNumber = m_hw->dcDownloadGetCurrentBlockNumber(); @@ -114,24 +128,48 @@ void ReportingThread::run() { double durationSecs = start.secsTo(QDateTime::currentDateTime()); double const timeAveragePerBlock = (currentBlockNumber > 0) ? (durationSecs / currentBlockNumber) : durationSecs; - long estimatedSecondsLeft = lround((timeAveragePerBlock * (totalBlocks - currentBlockNumber))); - long estimatedMinutesLeft = + uint64_t estimatedSecondsLeft = lround((timeAveragePerBlock * (totalBlocks - currentBlockNumber))); + uint64_t estimatedMinutesLeft = ((estimatedSecondsLeft % 60) == 0) ? (estimatedSecondsLeft / 60) : ((estimatedSecondsLeft + 60) / 60); + estimatedSecondsLeft = (estimatedSecondsLeft % 60); - double percent = ((double)currentBlockNumber / (double)totalBlocks) * 100.0; - report = QString(": total blocks %1, current block %2 [%3] (est. time left: %4min %5s)") - .arg(totalBlocks) - .arg(currentBlockNumber) - .arg(percent, 0, 'f', 2) - .arg(estimatedMinutesLeft) - .arg(estimatedSecondsLeft, 2); + if ((estimatedMinutesLeft <= estimatedMinutesLeftMax) + || (estimatedSecondsLeft <= estimatedSecondsLeftMax)) { + estimatedMinutesLeftMax = estimatedMinutesLeft; + estimatedSecondsLeftMax = estimatedSecondsLeft; + estimatedMinutesLeftPrev = estimatedMinutesLeft; + estimatedSecondsLeftPrev = estimatedSecondsLeft; - qCritical() << m_hw << "RT report" << report; + double percent = ((double)currentBlockNumber / (double)totalBlocks) * 100.0; + report = QString(": total blocks %1, current block %2 [%3] (est. time left: %4min %5s)") + .arg(totalBlocks) + .arg(currentBlockNumber) + .arg(percent, 0, 'f', 2) + .arg(estimatedMinutesLeft) + .arg(estimatedSecondsLeft, 2); - emit m_hw->hwapi_reportDCDownloadStatus(report); + qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate) + << QString("line=%1: RT report").arg(__LINE__) << report; + + emit m_hw->hwapi_reportDCDownloadStatus(report); + + } else { + double percent = ((double)currentBlockNumber / (double)totalBlocks) * 100.0; + report = QString(": total blocks %1, current block %2 [%3] (est. time left: %4min %5s)") + .arg(totalBlocks) + .arg(currentBlockNumber) + .arg(percent, 0, 'f', 2) + .arg(estimatedMinutesLeftPrev) + .arg(estimatedSecondsLeftPrev, 2); + + qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate) + << QString("line=%1: RT report").arg(__LINE__) << report; + + emit m_hw->hwapi_reportDCDownloadStatus(report); + } prevBlockNumber = currentBlockNumber; } @@ -150,7 +188,7 @@ void ReportingThread::run() { } } - qCritical() << QDateTime::currentDateTime() << __PRETTY_FUNCTION__ + qCritical() << QDateTime::currentDateTime().time().toString(Qt::ISODate) << __PRETTY_FUNCTION__ << QString("line=%1 REPORT THREAD ABOUT TO FINISH").arg(__LINE__); cnt = 10;