use redis::AsyncCommands; use tracing::info; use crate::inference::Detection; const ALERT_CHANNEL: &str = "channel:print_alerts"; /// Fast-paths a failure alert out to whatever's listening. pub struct Reporter { redis: redis::aio::MultiplexedConnection, } impl Reporter { pub async fn connect(redis_url: &str) -> anyhow::Result { let client = redis::Client::open(redis_url)?; let redis = client.get_multiplexed_tokio_connection().await?; Ok(Self { redis }) } /// Publishes to Redis (real — this is what `continuum-backend`'s /// WebSocket fan-out subscribes to, for live operator-console alerts). /// /// A real version would also `INSERT` into Postgres here via `sqlx` for /// the permanent audit trail — that's left as a `println!` for now; /// come back to it once async SQL feels comfortable. pub async fn report(&mut self, printer_id: &str, detections: &[Detection]) -> anyhow::Result<()> { let best = &detections[0]; info!(printer_id, label = %best.label, confidence = best.confidence, "reporting print failure"); let payload = serde_json::json!({ "printerId": printer_id, "label": best.label, "confidence": best.confidence, }); self.redis.publish::<_, _, ()>(ALERT_CHANNEL, payload.to_string()).await?; println!("(would also INSERT this alert into Postgres for the audit trail)"); Ok(()) } }