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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#[macro_use]
extern crate log;

use std::time::{Duration, Instant};
use sysinfo::{System, SystemExt};

/// A tool to report the progress of computations. It can be built and configured
/// using the `builder` function. If given the expected number of updates,
/// reports the expected time to completion, based on the current throughtput.
///
/// Progress is reported every 10 seconds by default. See the examples about how
/// to change it.
///
/// There are three methods to update the internal counter:
///
///  - `update`, for events that don't happen frequently
///  - `update_light`, which tries to report (by checking the configured frequency
///     of updates) only once every million updates. To be used in situations where
///     updates are frequent: it's an order of magnitude faster than `update`.
///
/// Reports are issued on the console using the `info!()` macro from the `log` crate.
/// Therefore, the reports depend on your logging configuration.
///
/// Inspired by `ProgressLogger` in the [`dsiutil`](http://dsiutils.di.unimi.it/docs/it/unimi/dsi/logging/ProgressLogger.html) Java library.
///
/// # Examples
///
/// ## Basic usage
/// ```
/// use progress_logger::ProgressLogger;
///
/// let mut pl = ProgressLogger::builder().start();
/// let mut cnt = 0;
/// for i in 0..10000 {
///     cnt += 1;
///     pl.update(1u32);
/// }
/// pl.stop();
/// ```
///
/// ## Reporting every 5 seconds
/// ```
/// use progress_logger::ProgressLogger;
/// use std::time::Duration;
///
/// let mut pl = ProgressLogger::builder()
///     .with_frequency(Duration::from_secs(5))
///     .start();
/// let mut cnt = 0;
/// for i in 0..10000 {
///     cnt += 1;
///     pl.update(1u32);
/// }
/// pl.stop();
///
/// ```
///
/// ## Changing the names of updates
/// ```
/// use progress_logger::ProgressLogger;
///
/// let mut pl = ProgressLogger::builder()
///     .with_items_name("points")
///     .start();
/// let mut cnt = 0;
/// for i in 0..10000 {
///     cnt += 1;
///     pl.update(1u32);
/// }
/// pl.stop();
/// ```
pub struct ProgressLogger {
    start: Instant,
    count: u64,
    expected_updates: Option<u64>,
    items: String,
    last_logged: Instant,
    /// the estimated time to completion, in seconds
    ettc: Option<f64>,
    throughput: Option<f64>,
    frequency: Duration,
    system: System,
}

impl ProgressLogger {
    /// Creates a builder to configure a new progress logger
    pub fn builder() -> ProgressLoggerBuilder {
        ProgressLoggerBuilder {
            expected_updates: None,
            items: None,
            frequency: None,
        }
    }

    fn log(&mut self) {
        let elapsed = Instant::now() - self.start;
        let throughput = self.count as f64 / elapsed.as_secs_f64();
        self.throughput.replace(throughput);
        self.system.refresh_memory();
        let used_kb = PrettyNumber::from(self.system.get_used_memory());
        let used_swap_kb = PrettyNumber::from(self.system.get_used_swap());
        if let Some(expected_updates) = self.expected_updates {
            let prediction = (expected_updates - self.count) as f64 / throughput;
            self.ettc.replace(prediction);
            info!(
                "[mem: {} kB, swap: {} kB] {:.2?} {} {}, {:.2} s left ({:.2} {}/s)",
                used_kb,
                used_swap_kb,
                elapsed,
                PrettyNumber::from(self.count),
                self.items,
                prediction,
                PrettyNumber::from(throughput),
                self.items
            );
        } else {
            info!(
                "[mem: {} kB, swap: {} kB] {:.2?} {} {} ({:.2} {}/s)",
                used_kb,
                used_swap_kb,
                elapsed,
                PrettyNumber::from(self.count),
                self.items,
                PrettyNumber::from(throughput),
                self.items
            );
        }
    }

    /// Get the estimated time to completion, if such prediction is available
    pub fn time_to_completion(&self) -> Option<Duration> {
        self.ettc.map(Duration::from_secs_f64)
    }

    pub fn throughput(&self) -> Option<f64> {
        self.throughput
    }

    /// Try to report progress only once every million updates
    #[inline]
    pub fn update_light<N: Into<u64>>(&mut self, cnt: N) {
        self.count += cnt.into();
        if self.count % 1_000_000 == 0 {
            let now = Instant::now();
            if (now - self.last_logged) > self.frequency {
                self.log();
                self.last_logged = now;
            }
        }
    }

    /// Update the internal counter and report progress if the time
    /// since the last report is greater than the configured duration
    #[inline]
    pub fn update<N: Into<u64>>(&mut self, cnt: N) {
        let cnt: u64 = cnt.into();
        self.count += cnt;
        let now = Instant::now();
        if (now - self.last_logged) > self.frequency {
            self.log();
            self.last_logged = now;
        }
    }

    /// Stops and drops the progress logger, logging the completion statement
    pub fn stop(self) {
        let elapsed = Instant::now() - self.start;
        let throughput = self.count as f64 / elapsed.as_secs_f64();
        info!(
            "Done in {:.2?}. {} {} ({:.2} {}/s)",
            elapsed,
            PrettyNumber::from(self.count),
            self.items,
            PrettyNumber::from(throughput),
            self.items
        );
    }
}

/// Builds a new progress logger. All the configurations are optional,
/// To obtain a builder, use `ProgressLogger::builder()`.
pub struct ProgressLoggerBuilder {
    expected_updates: Option<u64>,
    items: Option<String>,
    frequency: Option<Duration>,
}

impl ProgressLoggerBuilder {
    /// Configure the expected number of updates.
    pub fn with_expected_updates<N: Into<u64>>(mut self, updates: N) -> Self {
        self.expected_updates = Some(updates.into());
        self
    }
    /// Set the name of the items being counted.
    pub fn with_items_name<S: Into<String>>(mut self, name: S) -> Self {
        self.items = Some(name.into());
        self
    }
    /// Set the frequency of reports on the console.
    pub fn with_frequency(mut self, freq: Duration) -> Self {
        self.frequency = Some(freq);
        self
    }
    /// Builds the `ProgressLogger`, starting the internal timer.
    pub fn start(self) -> ProgressLogger {
        let now = Instant::now();
        ProgressLogger {
            start: now,
            count: 0,
            expected_updates: self.expected_updates,
            items: self.items.unwrap_or_else(|| "updates".to_owned()),
            last_logged: now,
            ettc: None,
            throughput: None,
            frequency: self.frequency.unwrap_or_else(|| Duration::from_secs(10)),
            system: System::default(),
        }
    }
}

struct PrettyNumber {
    rendered: String,
}

impl std::fmt::Display for PrettyNumber {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.rendered)
    }
}

impl std::fmt::Debug for PrettyNumber {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.rendered)
    }
}

impl From<u64> for PrettyNumber {
    fn from(n: u64) -> PrettyNumber {
        let s = format!("{}", n);
        let tmp: Vec<char> = s.chars().rev().collect();
        let mut chunks: Vec<&[char]> = tmp.chunks(3).collect();

        let mut rendered = String::new();
        let mut ul = chunks.len() % 2 == 1;
        while let Some(chunk) = chunks.pop() {
            let mut chunk = Vec::from(chunk);
            if ul {
                rendered.push_str("\x1B[0m");
            } else {
                rendered.push_str("\x1B[4m");
            }
            ul = !ul;
            while let Some(c) = chunk.pop() {
                rendered.push(c);
            }
        }
        if ul {
            rendered.push_str("\x1B[0m");
        }

        PrettyNumber { rendered }
    }
}

impl From<f64> for PrettyNumber {
    fn from(x: f64) -> PrettyNumber {
        assert!(x >= 0.0, "only positive number are supported for now");
        let s = format!("{:.2}", x);
        let mut parts = s.split(".");
        let s = parts.next().expect("missing integer part");
        let decimal = parts.next();
        let tmp: Vec<char> = s.chars().rev().collect();
        let mut chunks: Vec<&[char]> = tmp.chunks(3).collect();

        let mut rendered = String::new();
        let mut ul = chunks.len() % 2 == 1;
        while let Some(chunk) = chunks.pop() {
            let mut chunk = Vec::from(chunk);
            if ul {
                rendered.push_str("\x1B[0m");
            } else {
                rendered.push_str("\x1B[4m");
            }
            ul = !ul;
            while let Some(c) = chunk.pop() {
                rendered.push(c);
            }
        }
        if ul {
            rendered.push_str("\x1B[0m");
        }
        if let Some(decimal) = decimal {
            rendered.push('.');
            rendered.push_str(decimal);
        }

        PrettyNumber { rendered }
    }
}