use std::collections::BTreeMap;
use pulse_client::{aggs, windows, PulseClient, StreamBuilder};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut aggregations = BTreeMap::new();
aggregations.insert("txn_count".to_string(), aggs::count());
aggregations.insert("total_amount".to_string(), aggs::sum("amount"));
aggregations.insert("avg_amount".to_string(), aggs::avg("amount"));
aggregations.insert("max_amount".to_string(), aggs::max("amount"));
let builder = StreamBuilder::new("merchant-rollups-1m")
.from_topic("transactions")
.filter("amount > 0")
.key_by("merchant_id")
.window_with_aggs(windows::tumbling("1m"), aggregations)
.to_topic("merchant-rollups-1m");
println!("Pipeline spec: {}", builder.build()?);
let client = PulseClient::builder().base_url(base_url()).build()?;
let deployed = client.streams().deploy(&builder).await?;
println!("Deployed: {deployed}");
Ok(())
}
fn base_url() -> String {
std::env::var("PULSE_URL").unwrap_or_else(|_| "http://localhost:9090".to_string())
}