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

Modern-Apps.git / scripts / networklocation / wps_harvest / src / lib.rs

lib.rs86 lines · 2.8 KiBRaw
1//! Build the offline WiFi and cell beacon stores that `:networklocation` reads.
2//!
3//! Three stages, one binary each:
4//!
5//! | Binary | Does |
6//! | --- | --- |
7//! | `wps_seed` | Open bulk dumps (beacondb, OpenCelliD) -> record shards |
8//! | `wps_crawl` | Apple gs-loc snowball -> record shards, resumably |
9//! | `wps_build` | Shards -> a sorted, filtered, deduplicated `WPSDB2` store |
10//!
11//! The stages are separate processes because they have nothing in common operationally: a
12//! seed ingest is minutes, a planet crawl is weeks, and a build is hours of IO. Passing record
13//! shards between them means a crawl can be interrupted, resumed, extended, or merged with a
14//! newer dump without any stage knowing about the others.
15//!
16//! ## Where the accuracy comes from
17//!
18//! The store this writes keeps each beacon's coordinates as integer degrees x 1e8 — bit-exact
19//! against what the source reported — and its own horizontal accuracy, plus its altitude in
20//! metres when the source reports one (Apple's WiFi `location.5`; cell responses have no
21//! altitude field). The format it replaces quantized to a 20 m grid and carried no accuracy
22//! at all, so the device substituted one constant for every beacon and the solver's
23//! uncertainty weighting, which is fully implemented, had nothing to work with.
24
25#![deny(missing_docs)]
26
27pub mod frontier;
28pub mod gsloc;
29pub mod keys;
30pub mod probe;
31pub mod progress;
32pub mod proto;
33pub mod quality;
34pub mod record;
35pub mod seed;
36pub mod sort;
37pub mod store;
38pub mod store_extra;
39pub mod v1;
40
41use std::io::{self, BufRead, BufReader};
42use std::path::Path;
43
44/// Open a path for buffered reading, or stdin when it is `-`.
45///
46/// Bulk dumps ship gzipped and are piped in (`gunzip -c dump.csv.gz | wps_seed -`) rather than
47/// decompressed in process, which keeps a decompressor out of the dependency list.
48pub fn open_input(path: &str) -> io::Result<Box<dyn BufRead>> {
49 if path == "-" {
50 Ok(Box::new(BufReader::new(io::stdin())))
51 } else {
52 Ok(Box::new(BufReader::with_capacity(
53 1 << 20,
54 std::fs::File::open(Path::new(path))?,
55 )))
56 }
57}
58
59/// Format a byte count for a status line.
60pub fn human_bytes(n: u64) -> String {
61 const UNITS: [&str; 5] = ["B", "KiB", "MiB", "GiB", "TiB"];
62 let mut v = n as f64;
63 let mut u = 0;
64 while v >= 1024.0 && u + 1 < UNITS.len() {
65 v /= 1024.0;
66 u += 1;
67 }
68 if u == 0 {
69 format!("{n} B")
70 } else {
71 format!("{v:.2} {}", UNITS[u])
72 }
73}
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn byte_counts_are_readable() {
81 assert_eq!(human_bytes(0), "0 B");
82 assert_eq!(human_bytes(1023), "1023 B");
83 assert_eq!(human_bytes(1024), "1.00 KiB");
84 assert_eq!(human_bytes(13_400_000_000), "12.48 GiB");
85 }
86}