improve telemetry ergonomics

This commit is contained in:
2025-12-31 11:15:39 -05:00
parent 778c1a0dfd
commit b8475a12ad
10 changed files with 210 additions and 234 deletions

View File

@@ -1,10 +1,9 @@
use api::client::telemetry::Telemetry;
use api::client::telemetry::TelemetryRegistry;
use api::client::Client;
use api::data_type::DataType;
use api::data_value::DataValue;
use chrono::{TimeDelta, Utc};
use futures_util::future::join_all;
use num_traits::FloatConst;
use std::f64;
use std::sync::Arc;
use std::time::Duration;
use tokio::time::{sleep_until, Instant};
@@ -14,111 +13,31 @@ use tokio_util::sync::CancellationToken;
async fn main() -> anyhow::Result<()> {
env_logger::init();
let client = Arc::new(Client::connect("ws://[::1]:8080/backend")?);
let client = Arc::new(Client::connect("ws://localhost:8080/backend")?);
let tlm = TelemetryRegistry::new(client);
let time_offset = Telemetry::register(
client.clone(),
"simple_producer/time_offset".into(),
DataType::Float64,
)
let time_offset = tlm.register::<f64>("simple_producer/time_offset").await;
let publish_offset = tlm.register::<f64>("simple_producer/publish_offset").await;
let await_offset = tlm.register::<f64>("simple_producer/await_offset").await;
let bool_tlm_handle = tlm.register::<bool>("simple_producer/bool").await;
let sin_handles = join_all((1..=6).map(|i| {
tlm.register::<f32>(format!(
"simple_producer/sin{}",
if i == 1 { "".into() } else { i.to_string() }
))
}))
.await;
let publish_offset = Telemetry::register(
client.clone(),
"simple_producer/publish_offset".into(),
DataType::Float64,
)
.await;
let await_offset = Telemetry::register(
client.clone(),
"simple_producer/await_offset".into(),
DataType::Float64,
)
.await;
let sin_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/sin".into(),
DataType::Float32,
)
.await;
let cos_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/cos".into(),
DataType::Float64,
)
.await;
let bool_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/bool".into(),
DataType::Boolean,
)
.await;
let sin2_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/sin2".into(),
DataType::Float32,
)
.await;
let cos2_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/cos2".into(),
DataType::Float64,
)
.await;
let sin3_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/sin3".into(),
DataType::Float32,
)
.await;
let cos3_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/cos3".into(),
DataType::Float64,
)
.await;
let sin4_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/sin4".into(),
DataType::Float32,
)
.await;
let cos4_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/cos4".into(),
DataType::Float64,
)
.await;
let sin5_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/sin5".into(),
DataType::Float32,
)
.await;
let cos5_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/cos5".into(),
DataType::Float64,
)
.await;
let sin6_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/sin6".into(),
DataType::Float32,
)
.await;
let cos6_tlm_handle = Telemetry::register(
client.clone(),
"simple_producer/cos6".into(),
DataType::Float64,
)
let cos_handles = join_all((1..=6).map(|i| {
tlm.register::<f64>(format!(
"simple_producer/cos{}",
if i == 1 { "".into() } else { i.to_string() }
))
}))
.await;
let cancellation_token = CancellationToken::new();
@@ -134,81 +53,44 @@ async fn main() -> anyhow::Result<()> {
let start_instant = Instant::now();
let mut next_time = start_instant;
let mut index = 0;
let mut tasks = vec![];
while !cancellation_token.is_cancelled() {
next_time += Duration::from_millis(10);
index += 1;
sleep_until(next_time).await;
let publish_time = start_time + TimeDelta::from_std(next_time - start_instant).unwrap();
let actual_time = Instant::now();
tasks.push(time_offset.publish(
DataValue::Float64((actual_time - next_time).as_secs_f64()),
Utc::now(),
));
tasks.push(sin_tlm_handle.publish(
DataValue::Float32((f32::TAU() * (index as f32) / (1000.0_f32)).sin()),
publish_time,
));
tasks.push(cos_tlm_handle.publish(
DataValue::Float64((f64::TAU() * (index as f64) / (1000.0_f64)).cos()),
publish_time,
));
tasks.push(bool_tlm_handle.publish(DataValue::Boolean(index % 1000 > 500), publish_time));
tasks.push(sin2_tlm_handle.publish(
DataValue::Float32((f32::TAU() * (index as f32) / (500.0_f32)).sin()),
publish_time,
));
tasks.push(cos2_tlm_handle.publish(
DataValue::Float64((f64::TAU() * (index as f64) / (500.0_f64)).cos()),
publish_time,
));
tasks.push(sin3_tlm_handle.publish(
DataValue::Float32((f32::TAU() * (index as f32) / (333.0_f32)).sin()),
publish_time,
));
tasks.push(cos3_tlm_handle.publish(
DataValue::Float64((f64::TAU() * (index as f64) / (333.0_f64)).cos()),
publish_time,
));
tasks.push(sin4_tlm_handle.publish(
DataValue::Float32((f32::TAU() * (index as f32) / (250.0_f32)).sin()),
publish_time,
));
tasks.push(cos4_tlm_handle.publish(
DataValue::Float64((f64::TAU() * (index as f64) / (250.0_f64)).cos()),
publish_time,
));
tasks.push(sin5_tlm_handle.publish(
DataValue::Float32((f32::TAU() * (index as f32) / (200.0_f32)).sin()),
publish_time,
));
tasks.push(cos5_tlm_handle.publish(
DataValue::Float64((f64::TAU() * (index as f64) / (200.0_f64)).cos()),
publish_time,
));
tasks.push(sin6_tlm_handle.publish(
DataValue::Float32((f32::TAU() * (index as f32) / (166.0_f32)).sin()),
publish_time,
));
tasks.push(cos6_tlm_handle.publish(
DataValue::Float64((f64::TAU() * (index as f64) / (166.0_f64)).cos()),
publish_time,
));
tasks.push(publish_offset.publish(
DataValue::Float64((Instant::now() - actual_time).as_secs_f64()),
Utc::now(),
));
// Due to how telemetry handles are implemented, unless the send buffer is full awaiting
// these will return immediately
time_offset
.publish_now((actual_time - next_time).as_secs_f64())
.await?;
bool_tlm_handle
.publish(index % 1000 > 500, publish_time)
.await?;
// Join the tasks so they all run in parallel
for task in join_all(tasks.drain(..)).await {
task?;
for (i, sin) in sin_handles.iter().enumerate() {
sin.publish(
(f32::TAU() * (index as f32) / (1000.0_f32 / (i + 1) as f32)).sin(),
publish_time,
)
.await?;
}
for (i, cos) in cos_handles.iter().enumerate() {
cos.publish(
(f64::TAU() * (index as f64) / (1000.0_f64 / (i + 1) as f64)).cos(),
publish_time,
)
.await?;
}
tasks.push(await_offset.publish(
DataValue::Float64((Instant::now() - actual_time).as_secs_f64()),
Utc::now(),
));
publish_offset
.publish((Instant::now() - actual_time).as_secs_f64(), Utc::now())
.await?;
await_offset
.publish((Instant::now() - actual_time).as_secs_f64(), Utc::now())
.await?;
}
Ok(())