| 1 | /* |
|---|---|
| 2 | This file is part of the KDE Baloo Project |
| 3 | SPDX-FileCopyrightText: 2015 Pinak Ahuja <pinak.ahuja@gmail.com> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 6 | */ |
| 7 | |
| 8 | #include <cmath> |
| 9 | |
| 10 | #include "timeestimator.h" |
| 11 | |
| 12 | using namespace Baloo; |
| 13 | |
| 14 | TimeEstimator::TimeEstimator() = default; |
| 15 | |
| 16 | uint TimeEstimator::calculateTimeLeft() const |
| 17 | { |
| 18 | if (!m_estimateReady) { |
| 19 | return 0; |
| 20 | } |
| 21 | |
| 22 | float totalTime = 0; |
| 23 | float totalWeight = 0; |
| 24 | |
| 25 | int bufferIndex = m_bufferIndex; |
| 26 | for (int i = 0; i < BUFFER_SIZE; ++i) { |
| 27 | float weight = sqrt(x: i + 1); |
| 28 | totalWeight += weight; |
| 29 | |
| 30 | totalTime += m_batchTimeBuffer[bufferIndex] * weight; |
| 31 | bufferIndex = (bufferIndex + 1) % BUFFER_SIZE; |
| 32 | } |
| 33 | |
| 34 | float weightedAverage = totalTime / totalWeight; |
| 35 | |
| 36 | return weightedAverage * m_remainingCount; |
| 37 | } |
| 38 | |
| 39 | void TimeEstimator::handleNewBatchTime(uint time, uint batchSize) |
| 40 | { |
| 41 | // add the current batch time in place of the oldest batch time |
| 42 | m_batchTimeBuffer[m_bufferIndex] = (float)time / batchSize; |
| 43 | |
| 44 | m_bufferIndex = (m_bufferIndex + 1) % BUFFER_SIZE; |
| 45 | |
| 46 | if (!m_estimateReady && m_bufferIndex == 0) { |
| 47 | // Buffer has been filled once. We are ready to estimate |
| 48 | m_estimateReady = true; |
| 49 | } |
| 50 | } |
| 51 |
