|
| 1 | +use crate::{ |
| 2 | + gateway::db::{Gateway, LocationChangedAtUpdate}, |
| 3 | + settings::Settings, |
| 4 | +}; |
| 5 | +use chrono::{DateTime, Utc}; |
| 6 | +use h3o::{error::InvalidCellIndex, CellIndex}; |
| 7 | +use helium_crypto::PublicKey; |
| 8 | +use serde::Deserialize; |
| 9 | +use std::{ |
| 10 | + fs::File, |
| 11 | + path::{Path, PathBuf}, |
| 12 | + str::FromStr, |
| 13 | + time::Instant, |
| 14 | +}; |
| 15 | + |
| 16 | +#[derive(Debug, clap::Parser)] |
| 17 | +pub struct Import { |
| 18 | + #[clap(short = 'f')] |
| 19 | + file: PathBuf, |
| 20 | + |
| 21 | + #[clap(subcommand)] |
| 22 | + cmd: Cmd, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Debug, clap::Subcommand)] |
| 26 | +pub enum Cmd { |
| 27 | + HotspotsAssertions, |
| 28 | +} |
| 29 | + |
| 30 | +impl Import { |
| 31 | + pub async fn run(self, settings: &Settings) -> anyhow::Result<()> { |
| 32 | + match self.cmd { |
| 33 | + Cmd::HotspotsAssertions => { |
| 34 | + custom_tracing::init(settings.log.clone(), settings.custom_tracing.clone()).await?; |
| 35 | + |
| 36 | + tracing::info!("started"); |
| 37 | + |
| 38 | + let pool = settings.database.connect("mobile-config-store").await?; |
| 39 | + |
| 40 | + let start = Instant::now(); |
| 41 | + |
| 42 | + let updates = read_csv(self.file)? |
| 43 | + .into_iter() |
| 44 | + .filter_map(|row| row.try_into().ok()) |
| 45 | + .collect::<Vec<LocationChangedAtUpdate>>(); |
| 46 | + |
| 47 | + tracing::info!("file read, updating {} records", updates.len()); |
| 48 | + |
| 49 | + let updated = Gateway::update_bulk_location_changed_at(&pool, &updates).await?; |
| 50 | + |
| 51 | + let elapsed = start.elapsed(); |
| 52 | + tracing::info!(?elapsed, updated, "finished"); |
| 53 | + |
| 54 | + Ok(()) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Debug, Deserialize)] |
| 61 | +struct CsvRow { |
| 62 | + public_key: PublicKey, |
| 63 | + // serialnumber: String, |
| 64 | + time: DateTime<Utc>, |
| 65 | + // latitude: f64, |
| 66 | + // longitude: f64, |
| 67 | + h3: String, |
| 68 | + // assertion_type: String, |
| 69 | +} |
| 70 | + |
| 71 | +#[derive(Debug, thiserror::Error)] |
| 72 | +pub enum CsvRowError { |
| 73 | + #[error("H3 index parse error: {0}")] |
| 74 | + H3IndexParseError(#[from] InvalidCellIndex), |
| 75 | +} |
| 76 | + |
| 77 | +impl TryFrom<CsvRow> for LocationChangedAtUpdate { |
| 78 | + type Error = CsvRowError; |
| 79 | + |
| 80 | + fn try_from(row: CsvRow) -> Result<Self, Self::Error> { |
| 81 | + let cell = CellIndex::from_str(&row.h3)?; |
| 82 | + |
| 83 | + Ok(Self { |
| 84 | + address: row.public_key.into(), |
| 85 | + location_changed_at: row.time, |
| 86 | + location: cell.into(), |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +fn read_csv<P: AsRef<Path>>(path: P) -> anyhow::Result<Vec<CsvRow>> { |
| 92 | + let file = File::open(path)?; |
| 93 | + let mut rdr = csv::Reader::from_reader(file); |
| 94 | + let mut rows = Vec::new(); |
| 95 | + |
| 96 | + for result in rdr.deserialize() { |
| 97 | + let record: CsvRow = result?; |
| 98 | + rows.push(record); |
| 99 | + } |
| 100 | + |
| 101 | + Ok(rows) |
| 102 | +} |
0 commit comments