|
| 1 | +use sift_rs::metadata; |
| 2 | +use sift_stream::{ |
| 3 | + ChannelConfig, ChannelDataType, ChannelValue, Credentials, DiskBackupPolicy, Flow, FlowBuilder, |
| 4 | + FlowConfig, IngestionConfigForm, RecoveryStrategy, RetryPolicy, RunForm, SiftStreamBuilder, |
| 5 | + TimeValue, |
| 6 | +}; |
| 7 | +use std::{ |
| 8 | + env, |
| 9 | + error::Error, |
| 10 | + path::PathBuf, |
| 11 | + process::ExitCode, |
| 12 | + time::{Duration, SystemTime, UNIX_EPOCH}, |
| 13 | +}; |
| 14 | +use tracing_subscriber::filter::EnvFilter; |
| 15 | + |
| 16 | +#[tokio::main] |
| 17 | +async fn main() -> ExitCode { |
| 18 | + tracing_subscriber::fmt() |
| 19 | + .with_target(false) |
| 20 | + .with_env_filter(EnvFilter::from_default_env()) |
| 21 | + .init(); |
| 22 | + |
| 23 | + match run().await { |
| 24 | + Ok(()) => ExitCode::SUCCESS, |
| 25 | + Err(err) => { |
| 26 | + eprintln!("{err}"); |
| 27 | + ExitCode::FAILURE |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +async fn run() -> Result<(), Box<dyn Error>> { |
| 33 | + let credentials = Credentials::Config { |
| 34 | + apikey: env::var("SIFT_API_KEY").unwrap(), |
| 35 | + uri: env::var("SIFT_URI").unwrap(), |
| 36 | + }; |
| 37 | + |
| 38 | + // Define the schema of your telemetry |
| 39 | + let ingestion_config = IngestionConfigForm { |
| 40 | + asset_name: "MarsRover0".into(), |
| 41 | + client_key: "mars-rover0-ingestion-config-v1".into(), |
| 42 | + flows: vec![FlowConfig { |
| 43 | + name: "robotic-arm".into(), |
| 44 | + channels: vec![ChannelConfig { |
| 45 | + name: "joint-angle-encoder".into(), |
| 46 | + description: "measures the angular position of the arm’s joints".into(), |
| 47 | + data_type: ChannelDataType::Double.into(), |
| 48 | + unit: "degrees".into(), |
| 49 | + ..Default::default() |
| 50 | + }], |
| 51 | + }], |
| 52 | + }; |
| 53 | + |
| 54 | + let ts = SystemTime::now() |
| 55 | + .duration_since(UNIX_EPOCH) |
| 56 | + .map(|d| d.as_millis()) |
| 57 | + .unwrap(); |
| 58 | + |
| 59 | + // Create metadata using the metadata macro |
| 60 | + let metadata = metadata![ |
| 61 | + ("test_number", 5.0), |
| 62 | + ("is_simulation", true), |
| 63 | + ("location", "SiftHQ"), |
| 64 | + ]; |
| 65 | + |
| 66 | + // Define an optional run to group together data for this period of telemetry ingestion. |
| 67 | + let run = RunForm { |
| 68 | + name: format!("[MarsRover0].{ts}"), |
| 69 | + client_key: format!("mars-rover-sim-{ts}"), |
| 70 | + description: Some("simulation run".into()), |
| 71 | + tags: Some(vec!["simulation".into()]), |
| 72 | + metadata: Some(metadata), |
| 73 | + }; |
| 74 | + |
| 75 | + let recovery_strategy = RecoveryStrategy::RetryWithBackups { |
| 76 | + retry_policy: RetryPolicy::default(), |
| 77 | + disk_backup_policy: DiskBackupPolicy { |
| 78 | + backups_dir: Some(PathBuf::from("/tmp/sift_backup")), |
| 79 | + ..Default::default() |
| 80 | + }, |
| 81 | + }; |
| 82 | + |
| 83 | + // Initialize your Sift Stream |
| 84 | + let mut sift_stream = SiftStreamBuilder::new(credentials) |
| 85 | + .ingestion_config(ingestion_config) |
| 86 | + .recovery_strategy(recovery_strategy) |
| 87 | + .attach_run(run) |
| 88 | + .build_file_backup() |
| 89 | + .await?; |
| 90 | + |
| 91 | + // Stream telemetry to backup files using the [`SiftStream::send`] method. |
| 92 | + for i in 0..360 { |
| 93 | + let flow = Flow::new( |
| 94 | + "robotic-arm", |
| 95 | + TimeValue::now(), |
| 96 | + &[ChannelValue::new("joint-angle-encoder", f64::from(i).sin())], |
| 97 | + ); |
| 98 | + |
| 99 | + sift_stream.send(flow).await.unwrap(); |
| 100 | + |
| 101 | + // For demonstrative purposes, adding a contrived wait to get 10Hz data. |
| 102 | + tokio::time::sleep(Duration::from_millis(100)).await; |
| 103 | + } |
| 104 | + |
| 105 | + // Next, stream telemetry to backup files using the [`SiftStream::send_requests_nonblocking`] method |
| 106 | + // and the [`FlowBuilder`] to build the flow. |
| 107 | + // |
| 108 | + // This approach is more performant, and also provides methods to set the channel value via |
| 109 | + // the channel index instead of the key, which further improves performance by avoiding |
| 110 | + // hashing operations on the channel key. |
| 111 | + // |
| 112 | + // However, this approach does require setting the run ID on the flow builder instead of |
| 113 | + // letting the [`SiftStream`] handle it. Though this can be useful if using a single [`SiftStream`] |
| 114 | + // to send data for multiple runs/assets at one time. |
| 115 | + let descriptor = sift_stream.get_flow_descriptor("robotic-arm").unwrap(); |
| 116 | + let run_id = sift_stream.run().unwrap().run_id.clone(); |
| 117 | + for i in 0..360 { |
| 118 | + // Build the flow using the [`FlowBuilder`] and send it to |
| 119 | + // Sift using the [`SiftStream::send_requests_nonblocking`] method. |
| 120 | + let mut flow_builder = FlowBuilder::new(&descriptor); |
| 121 | + flow_builder.attach_run_id(&run_id); |
| 122 | + flow_builder |
| 123 | + .set_with_key("joint-angle-encoder", f64::from(i).sin()) |
| 124 | + .unwrap(); |
| 125 | + |
| 126 | + sift_stream |
| 127 | + .send_requests_nonblocking(vec![flow_builder.request(TimeValue::now())]) |
| 128 | + .unwrap(); |
| 129 | + |
| 130 | + // For demonstrative purposes, adding a contrived wait to get 10Hz data. |
| 131 | + tokio::time::sleep(Duration::from_millis(100)).await; |
| 132 | + } |
| 133 | + |
| 134 | + // Gracefully terminate your stream |
| 135 | + sift_stream |
| 136 | + .finish() |
| 137 | + .await |
| 138 | + .expect("failed to gracefully terminate Sift stream"); |
| 139 | + |
| 140 | + Ok(()) |
| 141 | +} |
0 commit comments