File indexing completed on 2024-04-21 14:52:35

0001 /*
0002     This file is part of the KDE Baloo Project
0003     SPDX-FileCopyrightText: 2015 Pinak Ahuja <pinak.ahuja@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include <cmath>
0009 
0010 #include "timeestimator.h"
0011 
0012 using namespace Baloo;
0013 
0014 TimeEstimator::TimeEstimator() = default;
0015 
0016 uint TimeEstimator::calculateTimeLeft(int filesLeft)
0017 {
0018     if (!m_estimateReady) {
0019         return 0;
0020     }
0021 
0022     float totalTime = 0;
0023     float totalWeight = 0;
0024 
0025     int bufferIndex = m_bufferIndex;
0026     for (int i = 0; i < BUFFER_SIZE; ++i) {
0027         float weight = sqrt(i + 1);
0028         totalWeight += weight;
0029 
0030         totalTime += m_batchTimeBuffer[bufferIndex] * weight;
0031         bufferIndex = (bufferIndex + 1) % BUFFER_SIZE;
0032     }
0033 
0034     float weightedAverage = totalTime / totalWeight;
0035 
0036     return weightedAverage * filesLeft;
0037 }
0038 
0039 void TimeEstimator::handleNewBatchTime(uint time, uint batchSize)
0040 {
0041     // add the current batch time in place of the oldest batch time
0042     m_batchTimeBuffer[m_bufferIndex] = (float)time / batchSize;
0043 
0044     m_bufferIndex = (m_bufferIndex + 1) % BUFFER_SIZE;
0045 
0046     if (!m_estimateReady && m_bufferIndex == 0) {
0047         // Buffer has been filled once. We are ready to estimate
0048         m_estimateReady = true;
0049     }
0050 }