Skip to content

Commit

Permalink
manipulate req times
Browse files Browse the repository at this point in the history
  • Loading branch information
macpie committed Jun 3, 2024
1 parent 50ec7e4 commit d569c3c
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 48 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test_mobile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ anyhow = { workspace = true }
async-compression = { version = "0", features = ["tokio", "gzip"] }
clap = { workspace = true }
file-store = { path = "../file_store" }
h3o = { workspace = true, features = ["geo"] }
helium-proto = { workspace = true }
hextree = { workspace = true }
tokio = { workspace = true, features = ["io-util", "fs"] }
Expand All @@ -19,6 +20,7 @@ tokio = { workspace = true, features = ["io-util", "fs"] }
backon = "0"
bs58 = { workspace = true }
custom-tracing = { path = "../custom_tracing" }
chrono = { workspace = true }
helium-crypto = { workspace = true }
prost = { workspace = true }
rand = { workspace = true }
Expand Down
64 changes: 43 additions & 21 deletions test_mobile/src/cli/assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,55 @@ use anyhow::Result;
use hextree::{Cell, HexTreeMap};
use tokio::{fs::File, io::AsyncWriteExt};

pub const CENTER_CELL: u64 = 0x8a5d107916dffff;

pub const INNER_1_CELL: u64 = 0x8a5d107916c7fff;
pub const INNER_2_CELL: u64 = 0x8a5d107916cffff;
pub const INNER_3_CELL: u64 = 0x8a5d1079ed67fff;
pub const INNER_4_CELL: u64 = 0x8a5d1079ed77fff;
pub const INNER_5_CELL: u64 = 0x8a5d1079ed2ffff;
pub const INNER_6_CELL: u64 = 0x8a5d107916d7fff;

pub const OUTER_1_CELL: u64 = 0x8a5d107916e7fff;
pub const OUTER_2_CELL: u64 = 0x8a5d107916effff;
pub const OUTER_3_CELL: u64 = 0x8a5d1079e9b7fff;
pub const OUTER_4_CELL: u64 = 0x8a5d1079e997fff;
pub const OUTER_5_CELL: u64 = 0x8a5d1079ed6ffff;
pub const OUTER_6_CELL: u64 = 0x8a5d1079ed47fff;
pub const OUTER_7_CELL: u64 = 0x8a5d1079ed57fff;
pub const OUTER_8_CELL: u64 = 0x8a5d1079ed0ffff;
pub const OUTER_9_CELL: u64 = 0x8a5d1079ed07fff;
pub const OUTER_10_CELL: u64 = 0x8a5d1079ed27fff;
pub const OUTER_11_CELL: u64 = 0x8a5d1079168ffff;
pub const OUTER_12_CELL: u64 = 0x8a5d107916f7fff;

/// Generate footfall, landtype and urbanization
#[derive(Debug, clap::Args)]
pub struct Cmd {}

impl Cmd {
pub async fn run(self) -> Result<()> {
let center_cell = Cell::from_raw(0x8a5d107916dffff)?;

let inner_1_cell = Cell::from_raw(0x8a5d107916c7fff)?;
let inner_2_cell = Cell::from_raw(0x8a5d107916cffff)?;
let inner_3_cell = Cell::from_raw(0x8a5d1079ed67fff)?;
let inner_4_cell = Cell::from_raw(0x8a5d1079ed77fff)?;
let inner_5_cell = Cell::from_raw(0x8a5d1079ed2ffff)?;
let inner_6_cell = Cell::from_raw(0x8a5d107916d7fff)?;

let outer_1_cell = Cell::from_raw(0x8a5d107916e7fff)?;
let outer_2_cell = Cell::from_raw(0x8a5d107916effff)?;
let outer_3_cell = Cell::from_raw(0x8a5d1079e9b7fff)?;
let outer_4_cell = Cell::from_raw(0x8a5d1079e997fff)?;
let outer_5_cell = Cell::from_raw(0x8a5d1079ed6ffff)?;
let outer_6_cell = Cell::from_raw(0x8a5d1079ed47fff)?;
let outer_7_cell = Cell::from_raw(0x8a5d1079ed57fff)?;
let outer_8_cell = Cell::from_raw(0x8a5d1079ed0ffff)?;
let outer_9_cell = Cell::from_raw(0x8a5d1079ed07fff)?;
let outer_10_cell = Cell::from_raw(0x8a5d1079ed27fff)?;
let outer_11_cell = Cell::from_raw(0x8a5d1079168ffff)?;
let outer_12_cell = Cell::from_raw(0x8a5d107916f7fff)?;
let center_cell = Cell::from_raw(CENTER_CELL)?;

let inner_1_cell = Cell::from_raw(INNER_1_CELL)?;
let inner_2_cell = Cell::from_raw(INNER_2_CELL)?;
let inner_3_cell = Cell::from_raw(INNER_3_CELL)?;
let inner_4_cell = Cell::from_raw(INNER_4_CELL)?;
let inner_5_cell = Cell::from_raw(INNER_5_CELL)?;
let inner_6_cell = Cell::from_raw(INNER_6_CELL)?;

let outer_1_cell = Cell::from_raw(OUTER_1_CELL)?;
let outer_2_cell = Cell::from_raw(OUTER_2_CELL)?;
let outer_3_cell = Cell::from_raw(OUTER_3_CELL)?;
let outer_4_cell = Cell::from_raw(OUTER_4_CELL)?;
let outer_5_cell = Cell::from_raw(OUTER_5_CELL)?;
let outer_6_cell = Cell::from_raw(OUTER_6_CELL)?;
let outer_7_cell = Cell::from_raw(OUTER_7_CELL)?;
let outer_8_cell = Cell::from_raw(OUTER_8_CELL)?;
let outer_9_cell = Cell::from_raw(OUTER_9_CELL)?;
let outer_10_cell = Cell::from_raw(OUTER_10_CELL)?;
let outer_11_cell = Cell::from_raw(OUTER_11_CELL)?;
let outer_12_cell = Cell::from_raw(OUTER_12_CELL)?;

// Footfall Data
// POI - footfalls > 1 for a POI across hexes
Expand Down
102 changes: 77 additions & 25 deletions test_mobile/tests/common/hotspot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ use anyhow::Result;
use backon::{ExponentialBuilder, Retryable};
use helium_crypto::{KeyTag, Keypair, Sign};
use helium_proto::services::poc_mobile::{
Client as PocMobileClient, CoverageObjectReqV1, SpeedtestReqV1,
Client as PocMobileClient, CoverageObjectReqV1, RadioHexSignalLevel, SpeedtestReqV1,
WifiHeartbeatReqV1,
};
use prost::Message;
use rand::rngs::OsRng;
use std::{
str::FromStr,
time::{SystemTime, UNIX_EPOCH},
};
use std::str::FromStr;
use test_mobile::cli::assignment::CENTER_CELL;
use tonic::{metadata::MetadataValue, transport::Channel, Request};
use uuid::Uuid;

use crate::common::{hours_ago, keypair_to_bs58, now, TimestampToDateTime};

pub struct Hotspot {
client: PocMobileClient<Channel>,
api_token: String,
Expand Down Expand Up @@ -49,10 +50,12 @@ impl Hotspot {
download_speed: u64,
latency: u32,
) -> Result<()> {
let timestamp = now();

let mut speedtest_req = SpeedtestReqV1 {
pub_key: self.keypair.public_key().into(),
serial: self.serial.clone(),
timestamp: now() as u64,
timestamp,
upload_speed,
download_speed,
latency,
Expand All @@ -65,20 +68,36 @@ impl Hotspot {
.expect("sign");

let request = self.set_metadata(speedtest_req.clone());
tracing::debug!("submitting speedtest {:?}", speedtest_req);
tracing::debug!(
"submitting speedtest @ {} = {:?}",
timestamp.to_datetime(),
speedtest_req
);

let res = self.client.submit_speedtest(request).await?;
tracing::debug!("submitted speedtest {:?}", res);
tracing::debug!(
"submitted speedtest @ {} = {:?}",
timestamp.to_datetime(),
res
);

Ok(())
}

pub async fn submit_coverage_object(&mut self) -> Result<()> {
pub async fn submit_coverage_object(&mut self, uuid: Uuid) -> Result<()> {
let location = h3o::CellIndex::try_from(CENTER_CELL).unwrap();

let coverage_claim_time = now() - hours_ago(24);

let mut coverage_object_req = CoverageObjectReqV1 {
pub_key: self.keypair.public_key().into(),
uuid: Uuid::new_v4().as_bytes().to_vec(),
coverage_claim_time: now() as u64,
coverage: vec![],
uuid: uuid.as_bytes().to_vec(),
coverage_claim_time,
coverage: vec![RadioHexSignalLevel {
location: location.to_string(),
signal_level: 3,
signal_power: 1000,
}],
indoor: false,
trust_score: 1,
signature: vec![],
Expand All @@ -91,10 +110,54 @@ impl Hotspot {
.expect("sign");

let request = self.set_metadata(coverage_object_req.clone());
tracing::debug!("submitting coverage_object {:?}", coverage_object_req);
tracing::debug!(
"submitting coverage_object @ {} = {:?}",
coverage_claim_time.to_datetime(),
coverage_object_req
);

let res = self.client.submit_coverage_object(request).await?;
tracing::debug!("submitted coverage_object {:?}", res);
tracing::debug!(
"submitted coverage_object @ {} = {:?}",
coverage_claim_time.to_datetime(),
res
);

Ok(())
}

pub async fn submit_wifi_heartbeat(&mut self, when: u64, coverage_object: Uuid) -> Result<()> {
let timestamp = now() - when;

let mut wifi_heartbeat_req = WifiHeartbeatReqV1 {
pub_key: self.keypair.public_key().into(),
timestamp,
lat: 19.642310,
lon: -155.990626,
location_validation_timestamp: timestamp,
operation_mode: true,
coverage_object: coverage_object.as_bytes().to_vec(),
signature: vec![],
};

wifi_heartbeat_req.signature = self
.keypair
.sign(&wifi_heartbeat_req.encode_to_vec())
.expect("sign");

let request = self.set_metadata(wifi_heartbeat_req.clone());
tracing::debug!(
"submitting wifi_heartbeat @ {} = {:?}",
timestamp.to_datetime(),
wifi_heartbeat_req
);

let res = self.client.submit_wifi_heartbeat(request).await?;
tracing::debug!(
"submitted wifi_heartbeat @ {} = {:?}",
timestamp.to_datetime(),
res
);

Ok(())
}
Expand All @@ -111,14 +174,3 @@ impl Hotspot {
request
}
}

fn now() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as u64
}

fn keypair_to_bs58(keypair: &Keypair) -> String {
bs58::encode(keypair.public_key().to_vec()).into_string()
}
33 changes: 33 additions & 0 deletions test_mobile/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
use std::time::{SystemTime, UNIX_EPOCH};

use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use helium_crypto::Keypair;

pub mod docker;
pub mod hotspot;

trait TimestampToDateTime {
fn to_datetime(&self) -> DateTime<Utc>;
}
impl TimestampToDateTime for u64 {
fn to_datetime(&self) -> DateTime<Utc> {
// Convert the u64 timestamp in milliseconds to NaiveDateTime
let naive = NaiveDateTime::from_timestamp_millis(*self as i64).expect("Invalid timestamp");

// Convert NaiveDateTime to DateTime<Utc> using Utc timestamp
Utc.from_utc_datetime(&naive)
}
}

pub fn now() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as u64
}

pub fn keypair_to_bs58(keypair: &Keypair) -> String {
bs58::encode(keypair.public_key().to_vec()).into_string()
}

pub fn hours_ago(hours: i64) -> u64 {
chrono::Duration::hours(hours).num_milliseconds() as u64
}
10 changes: 8 additions & 2 deletions test_mobile/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use common::{docker::Docker, hotspot::Hotspot};
use common::{docker::Docker, hotspot::Hotspot, hours_ago};
use std::time::Duration;
use uuid::Uuid;

mod common;

Expand All @@ -24,11 +25,16 @@ async fn main() -> Result<()> {
}

let mut hotspot1 = Hotspot::new("api-token".to_string()).await;
let co_uuid = Uuid::new_v4();

hotspot1.submit_coverage_object(co_uuid).await?;

hotspot1.submit_speedtest(1001, 1001, 25).await?;
hotspot1.submit_speedtest(1002, 1002, 25).await?;

hotspot1.submit_coverage_object().await?;
hotspot1
.submit_wifi_heartbeat(hours_ago(12), co_uuid)
.await?;

let _ = tokio::time::sleep(Duration::from_secs(10)).await;

Expand Down

0 comments on commit d569c3c

Please sign in to comment.