forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgress.cpp
More file actions
85 lines (68 loc) · 2.37 KB
/
Progress.cpp
File metadata and controls
85 lines (68 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2011-2015, Armory Technologies, Inc. //
// Distributed under the GNU Affero General Public License (AGPL v3) //
// See LICENSE-ATI or http://www.gnu.org/licenses/agpl.html //
// //
////////////////////////////////////////////////////////////////////////////////
#include "Progress.h"
ProgressCalculator::ProgressCalculator(uint64_t total)
: total_(total)
{
init(0);
}
void ProgressCalculator::init(uint64_t to)
{
auto now = std::chrono::high_resolution_clock::now();
auto duration = now.time_since_epoch();
then_ = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
lastSample_ = to;
}
void ProgressCalculator::advance(uint64_t to)
{
static const double smoothingFactor=.10;
if (to == lastSample_) return;
auto sysclock = std::chrono::high_resolution_clock::now();
auto duration = sysclock.time_since_epoch();
auto now = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
if (now == then_) return;
auto diff = now - then_;
double doubleDiff = double(diff.count()) / 1000.0;
double speed = (to-lastSample_)/doubleDiff;
if (lastSample_ == 0)
avgSpeed_ = speed;
lastSample_ = to;
avgSpeed_ = smoothingFactor*speed + (1-smoothingFactor)*avgSpeed_;
then_ = now;
}
ProgressReporterFilter::ProgressReporterFilter(ProgressReporter *to)
: to_(to)
{ }
void ProgressReporterFilter::progress(
double progress, unsigned secondsRemaining
)
{
secondsRemaining_ = secondsRemaining;
progress_ = progress;
to_->progress(progress, secondsRemaining);
}
ProgressFilter::ProgressFilter(ProgressReporter *to, int64_t offset, uint64_t total)
: ProgressReporterFilter(to), calc_(total), offset_(offset)
{
advance(0);
}
ProgressFilter::ProgressFilter(ProgressReporter *to, uint64_t total)
: ProgressReporterFilter(to), calc_(total)
{
advance(0);
}
ProgressFilter::~ProgressFilter()
{
advance(calc_.total());
}
void ProgressFilter::advance(uint64_t to)
{
calc_.advance(to+offset_);
progress(calc_.fractionCompleted(), calc_.remainingSeconds());
}
// kate: indent-width 3; replace-tabs on;