• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

clintval / vartovcf / 9800945742

04 Jul 2024 11:45PM UTC coverage: 90.476% (-4.6%) from 95.111%
9800945742

push

github

web-flow
chore: update dependencies and linting (#83)

* chore: update dependencies and linting

* chore: remove dead code

5 of 6 new or added lines in 2 files covered. (83.33%)

4 existing lines in 2 files now uncovered.

228 of 252 relevant lines covered (90.48%)

1.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/lib/progress_logger.rs
1
//! A logging implementation that emits status every "n" records that it is tracking.
2
use std::clone::Clone;
3
use std::cmp::PartialEq;
4
use std::default::Default;
5
use std::fmt::Debug;
6
use std::time::{Duration, SystemTime, SystemTimeError};
7

8
use case::CaseExt;
9
use chrono::{DateTime, Local};
10
use log::*;
11

12
/// By default, log every `n` records.
13
pub const DEFAULT_LOG_EVERY: usize = 100_000;
14

15
/// An interface for any struct that tracks and logs progress of records.
16
pub trait RecordLogger {
17
    /// Returns the elapsed time since this progress logger was created.
18
    fn elapsed_time(&self) -> Result<Duration, SystemTimeError>;
19
    /// Emit a log statement to log the most recent record added to the logger.
20
    fn emit(&mut self) -> Result<(), SystemTimeError>;
21
    /// Observe a record and optionally log it.
22
    fn observe(&mut self) -> Result<(), SystemTimeError>;
23
}
24

25
// TODO: It would be good to allow a user to override the module name from where the log was called.
26
/// A progress logger that emits status every "n" records that it is tracking.
27
#[derive(Clone, PartialEq, Debug)]
28
pub struct ProgressLogger<'a> {
29
    /// The verb to use when emitting log statements, for example "processed", "read", "written.
30
    pub verb: &'a str,
31
    /// The noun of the record we are tracking, for example "reads" or "variants".
32
    pub noun: &'a str,
33
    /// How many records to collect before emitting a log.
34
    pub every: usize,
35
    count: usize,
36
    last_emit: usize,
37
    start: SystemTime,
38
    since: SystemTime,
39
}
40

41
impl<'a> ProgressLogger<'a> {
42
    /// Build a new empty progress logger, set the start time to now.
43
    pub fn new(verb: &'a str, noun: &'a str, every: usize) -> ProgressLogger<'a> {
1✔
44
        let now = SystemTime::now();
1✔
45
        let dt: DateTime<Local> = now.into();
1✔
UNCOV
46
        info!(
×
UNCOV
47
            "Logging records started at: {}.",
×
48
            dt.format("%Y-%m-%d %H:%M:%S")
×
49
        );
50
        ProgressLogger {
51
            verb,
52
            noun,
53
            every,
54
            count: 0,
55
            last_emit: 0,
56
            start: now,
57
            since: now,
58
        }
59
    }
60

61
    /// Format a duration for logging.
62
    fn format_duration(duration: Duration) -> String {
1✔
63
        let seconds = duration.as_secs() % 60;
1✔
64
        let minutes = (duration.as_secs() / 60) % 60;
1✔
65
        let hours = (duration.as_secs() / 60) / 60;
1✔
66
        format!("{:0>2}:{:0>2}:{:0>2}", hours, minutes, seconds)
4✔
67
    }
68
}
69

70
impl Default for ProgressLogger<'_> {
71
    /// Build a default empty progress logger, set the start time to now.
72
    fn default() -> Self {
×
73
        let now = SystemTime::now();
×
74
        ProgressLogger {
75
            verb: "processed",
76
            noun: "records",
77
            every: DEFAULT_LOG_EVERY,
78
            count: 0,
79
            last_emit: 0,
80
            start: now,
81
            since: now,
82
        }
83
    }
84
}
85

86
impl RecordLogger for ProgressLogger<'_> {
87
    /// Returns the elapsed time since this progress logger was created.
88
    fn elapsed_time(&self) -> Result<Duration, SystemTimeError> {
1✔
89
        SystemTime::now().duration_since(self.start)
1✔
90
    }
91

92
    /// Emit a log statement to log the most recent record added to the logger.
93
    fn emit(&mut self) -> Result<(), SystemTimeError> {
1✔
94
        let now = SystemTime::now();
1✔
95
        let since = ProgressLogger::format_duration(now.duration_since(self.since)?);
1✔
96
        let elapsed = ProgressLogger::format_duration(self.elapsed_time()?);
2✔
UNCOV
97
        info!(
×
98
            "{} {} {}. Elapsed time: {}. Time of last {} {}: {}",
99
            self.verb.to_capitalized(),
100
            self.count,
101
            self.noun,
102
            elapsed,
103
            self.count - self.last_emit,
104
            self.noun,
105
            since
106
        );
107
        self.last_emit = self.count;
1✔
108
        self.since = now;
1✔
109
        Ok(())
1✔
110
    }
111

112
    /// Observe a record and optionally log it.
113
    fn observe(&mut self) -> Result<(), SystemTimeError> {
1✔
114
        self.count += 1;
1✔
115
        if self.count % self.every == 0 {
2✔
116
            return self.emit();
×
117
        };
118
        Ok(())
1✔
119
    }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc