summaryrefslogtreecommitdiff
path: root/src/mongo/util/progress_meter.cpp
blob: e9cbbacaa8fa66c6ff089489d659a6a1a514c17b (plain)
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
86
87
88
// progress_meter.cpp

/*    Copyright 2009 10gen Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

#include "mongo/pch.h" // needed for log.h

#include "mongo/util/progress_meter.h"

#include "mongo/util/log.h"

using namespace std;

namespace mongo {

    void ProgressMeter::reset( unsigned long long total , int secondsBetween , int checkInterval) {
        _total = total;
        _secondsBetween = secondsBetween;
        _checkInterval = checkInterval;
        
        _done = 0;
        _hits = 0;
        _lastTime = (int)time(0);
        
        _active = 1;
    }


    bool ProgressMeter::hit( int n ) {
        if ( ! _active ) {
            warning() << "hit an inactive ProgressMeter" << endl;
            return false;
        }
        
        _done += n;
        _hits++;
        if ( _hits % _checkInterval )
            return false;
        
        int t = (int) time(0);
        if ( t - _lastTime < _secondsBetween )
            return false;
        
        if ( _total > 0 ) {
            int per = (int)( ( (double)_done * 100.0 ) / (double)_total );
            LogstreamBuilder out = log();
            out << "\t\t" << _name << ": " << _done;

            if (_showTotal) {
                out << '/' << _total << '\t' << per << '%';
            }

            if ( ! _units.empty() ) {
                out << "\t(" << _units << ")";
            }
            out << endl;
        }
        _lastTime = t;
        return true;
    }
    
    string ProgressMeter::toString() const {
        if ( ! _active )
            return "";
        stringstream buf;
        buf << _name << ": " << _done << '/' << _total << ' ' << (_done*100)/_total << '%';
        
        if ( ! _units.empty() ) {
            buf << "\t(" << _units << ")" << endl;
        }
        
        return buf.str();
    }


}