Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / networklocation / wps_harvest / src / bin / wps_seed.rs
wps_seed.rs141 lines · 4.9 KiBRaw
| 1 | //! `wps_seed --kind wifi|cell --source NAME --out SHARD [INPUT|-]` |
| 2 | //! |
| 3 | //! Turn an open bulk dump into a record shard. Neither sorting nor filtering happens here — |
| 4 | //! `wps_build` owns both, because it is the only stage that sees every source at once and can |
| 5 | //! therefore tell a beacon two datasets disagree about from one that simply moves. |
| 6 | |
| 7 | use std::fs::File; |
| 8 | use std::io::BufWriter; |
| 9 | use std::path::PathBuf; |
| 10 | use std::process::ExitCode; |
| 11 | |
| 12 | use wps_harvest::record::{RecordWriter, Source}; |
| 13 | use wps_harvest::seed::{self, Kind}; |
| 14 | |
| 15 | const USAGE: &str = "\ |
| 16 | Usage: wps_seed --kind wifi|cell --source beacondb|opencellid --out SHARD [INPUT|-] |
| 17 | |
| 18 | --kind what the dump holds |
| 19 | --source which dataset it is, recorded per record so a build can attribute a disagreement |
| 20 | --out record shard to write |
| 21 | INPUT CSV path, or - for stdin (pipe gunzip -c for a compressed dump). Default: -"; |
| 22 | |
| 23 | struct Args { |
| 24 | kind: Kind, |
| 25 | source: Source, |
| 26 | out: PathBuf, |
| 27 | input: String, |
| 28 | } |
| 29 | |
| 30 | fn parse_args(argv: &[String]) -> Result<Args, String> { |
| 31 | let (mut kind, mut source, mut out, mut input) = (None, None, None, None); |
| 32 | let mut i = 0; |
| 33 | while i < argv.len() { |
| 34 | let a = argv[i].as_str(); |
| 35 | match a { |
| 36 | "--kind" | "--source" | "--out" => { |
| 37 | let v = argv.get(i + 1).ok_or_else(|| format!("{a} needs a value"))?; |
| 38 | match a { |
| 39 | "--kind" => { |
| 40 | kind = Some(match v.as_str() { |
| 41 | "wifi" => Kind::Wifi, |
| 42 | "cell" => Kind::Cell, |
| 43 | other => return Err(format!("unknown --kind '{other}'")), |
| 44 | }) |
| 45 | } |
| 46 | "--source" => { |
| 47 | source = Some(match v.as_str() { |
| 48 | "beacondb" => Source::Beacondb, |
| 49 | "opencellid" => Source::OpenCellId, |
| 50 | other => return Err(format!("unknown --source '{other}'")), |
| 51 | }) |
| 52 | } |
| 53 | _ => out = Some(PathBuf::from(v)), |
| 54 | } |
| 55 | i += 2; |
| 56 | } |
| 57 | "-h" | "--help" => return Err("help".to_string()), |
| 58 | other if other.starts_with("--") => return Err(format!("unknown option: {other}")), |
| 59 | other => { |
| 60 | if input.replace(other.to_string()).is_some() { |
| 61 | return Err("more than one input given".to_string()); |
| 62 | } |
| 63 | i += 1; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | Ok(Args { |
| 68 | kind: kind.ok_or("--kind is required")?, |
| 69 | source: source.ok_or("--source is required")?, |
| 70 | out: out.ok_or("--out is required")?, |
| 71 | input: input.unwrap_or_else(|| "-".to_string()), |
| 72 | }) |
| 73 | } |
| 74 | |
| 75 | fn main() -> ExitCode { |
| 76 | let argv: Vec<String> = std::env::args().skip(1).collect(); |
| 77 | let args = match parse_args(&argv) { |
| 78 | Ok(a) => a, |
| 79 | Err(e) => { |
| 80 | if e != "help" { |
| 81 | eprintln!("error: {e}"); |
| 82 | } |
| 83 | eprintln!("{USAGE}"); |
| 84 | return if e == "help" { ExitCode::SUCCESS } else { ExitCode::FAILURE }; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | // A cell dump named as a WiFi one (or the reverse) would resolve a plausible-looking |
| 89 | // header and produce a shard of nonsense keys, so say what is being read. |
| 90 | println!( |
| 91 | "Reading {} dump from {} as {:?}", |
| 92 | match args.kind { |
| 93 | Kind::Wifi => "wifi", |
| 94 | Kind::Cell => "cell", |
| 95 | }, |
| 96 | args.input, |
| 97 | args.source |
| 98 | ); |
| 99 | |
| 100 | let input = match wps_harvest::open_input(&args.input) { |
| 101 | Ok(r) => r, |
| 102 | Err(e) => { |
| 103 | eprintln!("error: cannot read {}: {e}", args.input); |
| 104 | return ExitCode::FAILURE; |
| 105 | } |
| 106 | }; |
| 107 | let file = match File::create(&args.out) { |
| 108 | Ok(f) => f, |
| 109 | Err(e) => { |
| 110 | eprintln!("error: cannot write {}: {e}", args.out.display()); |
| 111 | return ExitCode::FAILURE; |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | let mut writer = RecordWriter::new(BufWriter::with_capacity(1 << 20, file)); |
| 116 | let started = std::time::Instant::now(); |
| 117 | let result = seed::read_dump(input, args.kind, args.source, |r| writer.push(&r)); |
| 118 | |
| 119 | match result { |
| 120 | Ok((rows, emitted, skipped)) => { |
| 121 | if let Err(e) = writer.finish() { |
| 122 | eprintln!("error: writing {}: {e}", args.out.display()); |
| 123 | return ExitCode::FAILURE; |
| 124 | } |
| 125 | let size = std::fs::metadata(&args.out).map(|m| m.len()).unwrap_or(0); |
| 126 | println!( |
| 127 | "{emitted} record(s) from {rows} row(s), {skipped} skipped, in {:.1}s", |
| 128 | started.elapsed().as_secs_f64() |
| 129 | ); |
| 130 | println!("Wrote {} ({})", args.out.display(), wps_harvest::human_bytes(size)); |
| 131 | if emitted == 0 { |
| 132 | eprintln!("WARNING: 0 records. Check --kind matches the dump."); |
| 133 | } |
| 134 | ExitCode::SUCCESS |
| 135 | } |
| 136 | Err(e) => { |
| 137 | eprintln!("error: {e}"); |
| 138 | ExitCode::FAILURE |
| 139 | } |
| 140 | } |
| 141 | } |