use crate::node::{InputStreams, Node, NodeExecutionError, OutputStreams};
use crate::nodes::common::BaseNode;
use crate::nodes::comparison::common::compare_less_than;
use async_trait::async_trait;
use std::any::Any;
use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Arc;
use tokio_stream::{StreamExt, wrappers::ReceiverStream};
pub struct MinAggregateNode {
pub(crate) base: BaseNode,
}
impl MinAggregateNode {
pub fn new(name: String) -> Self {
Self {
base: BaseNode::new(
name,
vec!["configuration".to_string(), "in".to_string()],
vec!["out".to_string(), "error".to_string()],
),
}
}
}
#[async_trait]
impl Node for MinAggregateNode {
fn name(&self) -> &str {
self.base.name()
}
fn set_name(&mut self, name: &str) {
self.base.set_name(name);
}
fn input_port_names(&self) -> &[String] {
self.base.input_port_names()
}
fn output_port_names(&self) -> &[String] {
self.base.output_port_names()
}
fn has_input_port(&self, name: &str) -> bool {
self.base.has_input_port(name)
}
fn has_output_port(&self, name: &str) -> bool {
self.base.has_output_port(name)
}
fn execute(
&self,
mut inputs: InputStreams,
) -> Pin<
Box<dyn std::future::Future<Output = Result<OutputStreams, NodeExecutionError>> + Send + '_>,
> {
Box::pin(async move {
let _config_stream = inputs.remove("configuration");
let in_stream = inputs.remove("in").ok_or("Missing 'in' input")?;
let (out_tx, out_rx) = tokio::sync::mpsc::channel(10);
let (error_tx, error_rx) = tokio::sync::mpsc::channel(10);
let out_tx_clone = out_tx.clone();
let error_tx_clone = error_tx.clone();
tokio::spawn(async move {
let mut in_stream = in_stream;
let mut minimum: Option<Arc<dyn Any + Send + Sync>> = None;
while let Some(item) = in_stream.next().await {
match &minimum {
None => {
minimum = Some(item);
}
Some(current_min) => {
match compare_less_than(&item, current_min) {
Ok(less_than_arc) => {
if let Ok(less_than) = less_than_arc.clone().downcast::<bool>()
&& *less_than
{
minimum = Some(item);
}
}
Err(_) => {
}
}
}
}
}
match minimum {
Some(min_value) => {
let _ = out_tx_clone.send(min_value).await;
}
None => {
let error_msg = "Cannot find minimum of empty stream".to_string();
let error_arc = Arc::new(error_msg) as Arc<dyn Any + Send + Sync>;
let _ = error_tx_clone.send(error_arc).await;
}
}
});
let mut outputs = HashMap::new();
outputs.insert(
"out".to_string(),
Box::pin(ReceiverStream::new(out_rx))
as Pin<Box<dyn tokio_stream::Stream<Item = Arc<dyn Any + Send + Sync>> + Send>>,
);
outputs.insert(
"error".to_string(),
Box::pin(ReceiverStream::new(error_rx))
as Pin<Box<dyn tokio_stream::Stream<Item = Arc<dyn Any + Send + Sync>> + Send>>,
);
Ok(outputs)
})
}
}