use super::pipeline::Pipeline;
use super::serve;
use async_trait::async_trait;
use fv_streams_engine::{Binding, BuildSignal, ControlPlane, StageDef, Topology};
use serde_json::{json, Value as J};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
pub struct FileControlPlane {
pub(crate) def: J,
pub(crate) stop: Arc<AtomicBool>,
pub(crate) live: tokio::sync::watch::Sender<J>,
}
impl FileControlPlane {
pub fn new(pipeline: &Pipeline, stop: Arc<AtomicBool>) -> Self {
Self {
def: pipeline.def.clone(),
stop,
live: tokio::sync::watch::channel(json!({ "status": "STARTING", "metrics": {} })).0,
}
}
pub fn live(&self) -> serve::Live {
self.live.subscribe()
}
}
#[async_trait]
impl ControlPlane for FileControlPlane {
async fn topology(&self, _pipeline: &str) -> Result<Topology, String> {
if let Some(sql) = self.def["sql"].as_str() {
return fv_streams_sql::compile(sql).map_err(|e| format!("pipeline SQL: {e}"));
}
let transforms = self.def["transforms"]
.as_array()
.ok_or("pipeline.toml: needs at least one [[transforms]] table (or a `sql = \"…\"` field)")?;
let stages = transforms
.iter()
.map(|t| StageDef {
steps: t["steps"].as_array().cloned().unwrap_or_default(),
inputs: t["inputs"]
.as_array()
.map(|a| a.iter().filter_map(|v| v.as_str().map(String::from)).collect())
.unwrap_or_default(),
output: t["output"].as_str().unwrap_or("").to_string(),
})
.collect();
Ok(Topology { stages })
}
async fn resolve_dataset(&self, dataset: &str) -> Result<Binding, String> {
let ds = &self.def["datasets"][dataset];
if ds.is_null() {
return Err(format!(
"pipeline.toml: dataset '{dataset}' is not defined under [datasets] — every transform input/output must be"
));
}
let default_brokers = self.def["brokers"]["default"]
.as_str()
.unwrap_or("localhost:9092")
.to_string();
let cx = fv_streams_connectors::Ctx {
dataset,
ds,
default_brokers: &default_brokers,
};
for warning in fv_streams_connectors::check_dataset(&cx)? {
eprintln!("fv-streams: {warning}");
}
Ok(Binding {
api_name: dataset.to_string(),
source: fv_streams_connectors::resolve_source(&cx)?,
sink: fv_streams_connectors::resolve_sink(&cx)?,
})
}
async fn publishes(&self, dataset: &str) -> bool {
let ds = &self.def["datasets"][dataset];
ds.get("sink").is_some() || ds.get("topic").is_some()
}
async fn heartbeat(&self, _build_id: &str) -> BuildSignal {
if self.stop.load(Ordering::Relaxed) {
BuildSignal::Stop
} else {
BuildSignal::Continue
}
}
async fn put_record(&self, _build_id: &str, record: &J) {
let _ = self.live.send(record.clone());
let m = &record["metrics"];
let status = record["status"].as_str().unwrap_or("RUNNING");
println!(
"▸ {status} consumed={} emitted={} dropped={}",
m["consumed"].as_u64().unwrap_or(0),
m["emitted"].as_u64().unwrap_or(0),
m["dropped"].as_u64().unwrap_or(0)
);
}
}