Baloo

timeestimator.cpp
1 /*
2  This file is part of the KDE Baloo Project
3  SPDX-FileCopyrightText: 2015 Pinak Ahuja <[email protected]>
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 #include "filecontentindexerprovider.h"
12 
13 using namespace Baloo;
14 
15 TimeEstimator::TimeEstimator(QObject* parent)
16  : QObject(parent)
17  , m_bufferIndex(0)
18  , m_estimateReady(false)
19 {
20 }
21 
22 uint TimeEstimator::calculateTimeLeft(int filesLeft)
23 {
24  if (!m_estimateReady) {
25  return 0;
26  }
27 
28  float totalTime = 0;
29  float totalWeight = 0;
30 
31  int bufferIndex = m_bufferIndex;
32  for (int i = 0; i < BUFFER_SIZE; ++i) {
33  float weight = sqrt(i + 1);
34  totalWeight += weight;
35 
36  totalTime += m_batchTimeBuffer[bufferIndex] * weight;
37  bufferIndex = (bufferIndex + 1) % BUFFER_SIZE;
38  }
39 
40  float weightedAverage = totalTime / totalWeight;
41 
42  return weightedAverage * filesLeft;
43 }
44 
45 void TimeEstimator::handleNewBatchTime(uint time, uint batchSize)
46 {
47  // add the current batch time in place of the oldest batch time
48  m_batchTimeBuffer[m_bufferIndex] = (float)time / batchSize;
49 
50  m_bufferIndex = (m_bufferIndex + 1) % BUFFER_SIZE;
51 
52  if (!m_estimateReady && m_bufferIndex == 0) {
53  // Buffer has been filled once. We are ready to estimate
54  m_estimateReady = true;
55  }
56 }
57 
58 #include "moc_timeestimator.cpp"
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.