ProgressTimer.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/progress/ProgressTimer.cpp
22 
23  \brief The ProgressTimer is a utility class that can be used to calculate the estimated time to finish a task.
24 */
25 
26 // TerraLib
27 #include "../StringUtils.h"
28 #include "../../core/translator/Translator.h"
29 #include "ProgressTimer.h"
30 
31 te::common::ProgressTimer::ProgressTimer(int totalSteps, std::string message)
32  : m_totalSteps(totalSteps),
33  m_count(0),
34  m_startTime(0),
35  m_remainingTime(0.0),
36  m_speedTime(0.0),
37  m_message(message)
38 {
39 }
40 
42 
44 {
45 // start time
46  time(&m_startTime);
47 
48  m_count = 0;
49 }
50 
52 {
53  ++m_count;
54 
55  time_t curtime;
56 
57  // current time
58  time(&curtime);
59 
60  // difference in seconds
61  double diffInSec = difftime(curtime, m_startTime);
62 
63  m_speedTime = (static_cast<double>(m_count) / diffInSec);
64 
65  int remainingSteps = m_totalSteps - m_count;
66 
67  double remainingTimeInSec = (static_cast<double>(remainingSteps) / m_speedTime);
68 
69  m_remainingTime = remainingTimeInSec / 60.0;
70 }
71 
73 {
74  m_totalSteps = totalSteps;
75 }
76 
77 void te::common::ProgressTimer::setMessage(std::string message)
78 {
79  m_message = message;
80 }
81 
83 {
84  return m_remainingTime;
85 }
86 
88 {
89  return m_speedTime;
90 }
91 
93 {
94  std::string strTime = m_message + "\n";
95 
96  if(m_remainingTime < 1.0)
97  {
98  strTime += TE_TR("Remaining Time: Less than one minute");
99  }
100  else
101  {
102  strTime += TE_TR("Remaining Time: ") + te::common::Convert2String(m_remainingTime, 1);
103  strTime += TE_TR(" minute(s) - Speed: ") + te::common::Convert2String(m_speedTime, 2);
104  strTime += TE_TR(" Steps/Second");
105  }
106 
107  return strTime;
108 }
time_t m_startTime
Initial time.
Definition: ProgressTimer.h:99
void setMessage(std::string message)
Set the message used by task progress.
int m_count
Internal counter.
Definition: ProgressTimer.h:98
double m_speedTime
Speed time in seconds.
void tick()
Define a new step process evolution.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
double getRemainingTimeInMin() const
Function used to get the remaining time to end the process.
double getSpeedTimeInSec() const
Function used to get the speed time.
std::string m_message
Original task message.
double m_remainingTime
Remaining time in minutes.
void start()
Start the internal timer.
ProgressTimer(int totalSteps, std::string message)
It initializes a ProgressTimer.
std::string getMessage()
Get the information about the evolution of the process.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:56
~ProgressTimer()
Destructor.
void setTotalSteps(int totalSteps)
Set the total steps.
The ProgressTimer is a utility class that can be used to calculate the estimated time to finish a tas...
int m_totalSteps
Total steps.
Definition: ProgressTimer.h:97