Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / tile_build / src / progress.rs
progress.rs103 lines · 3.1 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. |
| 10 | use std::io::Write; |
| 11 | |
| 12 | /// Progress over one zoom's tile loop. |
| 13 | pub 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. |
| 25 | pub 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 | |
| 37 | impl 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 | /// Snap to 100% and drop to a fresh line, so the next stage's first line |
| 70 | /// starts clean instead of on top of the meter. |
| 71 | pub fn finish(&mut self, unit: &str) { |
| 72 | if self.on { |
| 73 | self.done = self.total; |
| 74 | self.draw(unit); |
| 75 | eprintln!(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | fn draw(&self, unit: &str) { |
| 80 | let elapsed = self.start.elapsed().as_secs_f64(); |
| 81 | let pct = if self.total == 0 { |
| 82 | 100 |
| 83 | } else { |
| 84 | (self.done * 100 / self.total).min(100) |
| 85 | }; |
| 86 | // ETA extrapolates the average rate so far; meaningless until something is |
| 87 | // done, so hold it at zero for the opening frame. |
| 88 | let eta = if self.done == 0 { |
| 89 | 0.0 |
| 90 | } else { |
| 91 | elapsed * (self.total - self.done) as f64 / self.done as f64 |
| 92 | }; |
| 93 | eprint!( |
| 94 | "\r{:<28} [{pct:>3}%] {}/{} {unit} {} elapsed, ETA {} ", |
| 95 | self.label, |
| 96 | self.done, |
| 97 | self.total, |
| 98 | hms(elapsed), |
| 99 | hms(eta), |
| 100 | ); |
| 101 | let _ = std::io::stderr().flush(); |
| 102 | } |
| 103 | } |