Simplify boilerplate for learning: stub inference and Postgres write
Removed ort/ndarray/image (ONNX Runtime inference) and sqlx (Postgres audit write). inference::detect() and the Postgres half of reporter::report() are now stubs with a comment for what real code goes there. Redis Streams XREADGROUP loop and the Redis pub/sub alert stay real. Verified with a clean cargo check --all-targets (0 warnings).
This commit is contained in:
+26
-28
@@ -1,42 +1,40 @@
|
||||
mod postgres;
|
||||
mod redis_pub;
|
||||
|
||||
use crate::inference::Detection;
|
||||
use sqlx::PgPool;
|
||||
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,
|
||||
pg: PgPool,
|
||||
}
|
||||
|
||||
impl Reporter {
|
||||
pub async fn connect(redis_url: &str, database_url: &str) -> anyhow::Result<Self> {
|
||||
let redis_client = redis::Client::open(redis_url)?;
|
||||
let redis = redis_client.get_multiplexed_tokio_connection().await?;
|
||||
let pg = PgPool::connect(database_url).await?;
|
||||
|
||||
Ok(Self { redis, pg })
|
||||
pub async fn connect(redis_url: &str) -> anyhow::Result<Self> {
|
||||
let client = redis::Client::open(redis_url)?;
|
||||
let redis = client.get_multiplexed_tokio_connection().await?;
|
||||
Ok(Self { redis })
|
||||
}
|
||||
|
||||
/// Fast-paths a failure alert: publishes to Redis immediately for any
|
||||
/// live UI subscribers, then persists to Postgres for the audit trail
|
||||
/// and downstream farm-manager notifications.
|
||||
pub async fn report_failure(
|
||||
&mut self,
|
||||
printer_id: &str,
|
||||
job_id: Option<&str>,
|
||||
detections: &[Detection],
|
||||
) -> anyhow::Result<()> {
|
||||
let best = detections
|
||||
.iter()
|
||||
.max_by(|a, b| a.confidence.total_cmp(&b.confidence))
|
||||
.expect("report_failure called with no detections");
|
||||
|
||||
/// 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");
|
||||
|
||||
redis_pub::publish_alert(&mut self.redis, printer_id, best).await?;
|
||||
postgres::insert_alert(&self.pg, printer_id, job_id, detections).await?;
|
||||
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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user