Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / scripts / maps / tile_build / src / progress.rs

progress.rs114 lines · 3.6 KiBRaw
1//! A one-line progress bar for the long tiling loops.
2//!
3//! The single standard meter shape (`label [ 42%] done/total elapsed, ETA`,
4//! carriage return, no newline until the end), shared with `osm_ingest`'s
5//! passes so a build log reads consistently across crates. A planet layer
6//! spends minutes to hours per zoom and every tiler was previously silent
7//! for all of it.
8//!
9//! Single-threaded, so no atomics: the tiling loops are one thread each.
10use std::io::Write;
11
12/// Progress over one zoom's tile loop.
13pub struct Progress {
14 label: String,
15 total: usize,
16 done: usize,
17 last_pct: usize,
18 start: std::time::Instant,
19 on: bool,
20}
21
22/// Whole seconds as `HhMMmSSs`, dropping units above the largest non-zero one.
23/// Same shape as `osm_ingest::progress::hms`, duplicated because the two
24/// crates share no code.
25pub fn hms(s: f64) -> String {
26 let s = s.max(0.0) as u64;
27 if s < 60 {
28 return format!("{s}s");
29 }
30 let m = s / 60;
31 if m < 60 {
32 return format!("{m}m{:02}s", s % 60);
33 }
34 format!("{}h{:02}m{:02}s", m / 60, m % 60, s % 60)
35}
36
37impl Progress {
38 /// `unit` names what is being counted, because the number is not always the tile
39 /// count the report ends up printing — see [`Progress::new`]'s callers.
40 pub fn new(label: String, total: usize, unit: &str, on: bool) -> Progress {
41 let p = Progress {
42 label,
43 total,
44 done: 0,
45 last_pct: usize::MAX,
46 start: std::time::Instant::now(),
47 on,
48 };
49 if p.on {
50 p.draw(unit);
51 }
52 p
53 }
54
55 /// One unit done. Throttled to whole percent: a planet zoom has millions of tiles
56 /// and a write per tile would cost more than the tiling.
57 pub fn tick(&mut self, unit: &str) {
58 self.done += 1;
59 if !self.on || self.total == 0 {
60 return;
61 }
62 let pct = self.done * 100 / self.total;
63 if pct != self.last_pct {
64 self.last_pct = pct;
65 self.draw(unit);
66 }
67 }
68
69 /// `n` units done at once, for loops that finish work in batches rather than one unit at a
70 /// time. Draws at most once per call, and only when the whole percent moved -- the same
71 /// throttle as [`tick`](Self::tick), so the meter reads the same.
72 pub fn tick_n(&mut self, n: usize, unit: &str) {
73 self.done += n;
74 if !self.on || self.total == 0 {
75 return;
76 }
77 let pct = self.done * 100 / self.total;
78 if pct != self.last_pct {
79 self.last_pct = pct;
80 self.draw(unit);
81 }
82 }
83
84 /// Snap to 100% and drop to a fresh line, so the next stage's first line
85 /// starts clean instead of on top of the meter.
86 pub fn finish(&mut self, unit: &str) {
87 if self.on {
88 self.done = self.total;
89 self.draw(unit);
90 eprintln!();
91 }
92 }
93
94 fn draw(&self, unit: &str) {
95 let elapsed = self.start.elapsed().as_secs_f64();
96 let pct = (self.done * 100).checked_div(self.total).map_or(100, |p| p.min(100));
97 // ETA extrapolates the average rate so far; meaningless until something is
98 // done, so hold it at zero for the opening frame.
99 let eta = if self.done == 0 {
100 0.0
101 } else {
102 elapsed * (self.total - self.done) as f64 / self.done as f64
103 };
104 eprint!(
105 "\r{:<28} [{pct:>3}%] {}/{} {unit} {} elapsed, ETA {} ",
106 self.label,
107 self.done,
108 self.total,
109 hms(elapsed),
110 hms(eta),
111 );
112 let _ = std::io::stderr().flush();
113 }
114}