pub fn select_workers_at_destination(
    conn: &PgConnection,
) -> Result<Vec<NodeState>, MeshSimError> {
    use schema::workers;
    use schema::worker_positions;
    use schema::worker_velocities;
    use schema::worker_destinations;

    let source: Vec<(i32, String, f64, f64, f64, f64, Option<f64>, Option<f64>)> = workers::table
        .inner_join(worker_positions::table)
        .inner_join(worker_velocities::table)
        .inner_join(worker_destinations::table)
        .select((
            workers::id,
            workers::worker_name,
            worker_positions::x,
            worker_positions::y,
            worker_velocities::x,
            worker_velocities::y,
            worker_destinations::x.nullable(),
            worker_destinations::y.nullable(),
        ))
        .get_results(conn)
        .map_err(|e| {
            let error_msg =
                String::from("Failed to read worker positions, destinations and velocities");
            MeshSimError {
                kind: MeshSimErrorKind::SQLExecutionFailure(error_msg),
                cause: Some(Box::new(e)),
            }
        })?;

    let result = source.into_iter()
    .map(|(id, name, p_x, p_y, v_x, v_y, d_x, d_y)| { 
        let dest = if d_x.is_some() && d_y.is_some() {
            let d = Position {
                x: d_x.unwrap(),
                y: d_y.unwrap(),
            };
            Some(d)
        } else {
            None
        };

        NodeState {
            id,
            name,
            pos: Position{x: p_x, y: p_y},
            vel: Velocity{x: v_x, y: v_y},
            dest,
        }
    })
    .filter(|ns| { 
        if ns.dest.is_some() {
            let dest = ns.dest.as_ref().unwrap();
            ns.pos == *dest
        } else {
            false
        }
    })
    .collect();

    // let mut result = HashMap::new();
    // for (w_id, w_name, pos_x, pos_y, dest_x, dest_y, vel_x, vel_y) in source {
    //     let vel_magnitude = (vel_x.powi(2) + vel_y.powi(2)).sqrt();
    //     let remaining_distance = euclidean_distance(pos_x, pos_y, dest_x, dest_y);
    //     let dist_threshold = vel_magnitude / 2.0;
    //     if remaining_distance <= dist_threshold {
    //         result.insert(w_id, (w_name, Position { x: pos_x, y: pos_y }, Velocity { x: vel_x, y: vel_y }));
    //     }
    // }

    Ok(result)
}
/// Function exported exclusively for the use of the Master module.
/// Returns ids and positions of all workers that have reached their destination.
Function exported exclusively for the use of the Master module.
Returns ids and positions of all workers that have reached their destination.
Function exported exclusively for the use of the Master module . Returns ids and positions of all workers that have reached their destination .
pub fn select_workers_at_destination(
    conn: &PgConnection,
) -> Result<Vec<NodeState>, MeshSimError> {
    use schema::workers;
    use schema::worker_positions;
    use schema::worker_velocities;
    use schema::worker_destinations;
    let source: Vec<(i32, String, f64, f64, f64, f64, Option<f64>, Option<f64>)> = workers::table
        .inner_join(worker_positions::table)
        .inner_join(worker_velocities::table)
        .inner_join(worker_destinations::table)
        .select((
            workers::id,
            workers::worker_name,
            worker_positions::x,
            worker_positions::y,
            worker_velocities::x,
            worker_velocities::y,
            worker_destinations::x.nullable(),
            worker_destinations::y.nullable(),
        ))
        .get_results(conn)
        .map_err(|e| {
            let error_msg =
                String::from("Failed to read worker positions, destinations and velocities");
            MeshSimError {
                kind: MeshSimErrorKind::SQLExecutionFailure(error_msg),
                cause: Some(Box::new(e)),
            }
        })?;
    let result = source.into_iter()
    .map(|(id, name, p_x, p_y, v_x, v_y, d_x, d_y)| { 
        let dest = if d_x.is_some() && d_y.is_some() {
            let d = Position {
                x: d_x.unwrap(),
                y: d_y.unwrap(),
            };
            Some(d)
        } else {
            None
        };
        NodeState {
            id,
            name,
            pos: Position{x: p_x, y: p_y},
            vel: Velocity{x: v_x, y: v_y},
            dest,
        }
    })
    .filter(|ns| { 
        if ns.dest.is_some() {
            let dest = ns.dest.as_ref().unwrap();
            ns.pos == *dest
        } else {
            false
        }
    })
    .collect();
    // let mut result = HashMap::new();
    // for (w_id, w_name, pos_x, pos_y, dest_x, dest_y, vel_x, vel_y) in source {
    //     let vel_magnitude = (vel_x.powi(2) + vel_y.powi(2)).sqrt();
    //     let remaining_distance = euclidean_distance(pos_x, pos_y, dest_x, dest_y);
    //     let dist_threshold = vel_magnitude / 2.0;
    //     if remaining_distance <= dist_threshold {
    //         result.insert(w_id, (w_name, Position { x: pos_x, y: pos_y }, Velocity { x: vel_x, y: vel_y }));
    //     }
    // }
    Ok(result)
}
pub fn select_workers_at_destination ( conn : & PgConnection , ) -> Result < Vec < NodeState > , MeshSimError > { use schema :: workers ; use schema :: worker_positions ; use schema :: worker_velocities ; use schema :: worker_destinations ; let source : Vec < ( i32 , String , f64 , f64 , f64 , f64 , Option < f64 > , Option < f64 > ) > = workers :: table . inner_join ( worker_positions :: table ) . inner_join ( worker_velocities :: table ) . inner_join ( worker_destinations :: table ) . select ( ( workers :: id , workers :: worker_name , worker_positions :: x , worker_positions :: y , worker_velocities :: x , worker_velocities :: y , worker_destinations :: x . nullable ( ) , worker_destinations :: y . nullable ( ) , ) ) . get_results ( conn ) . map_err ( | e | { let error_msg = String :: from ( " " ) ; MeshSimError { kind : MeshSimErrorKind :: SQLExecutionFailure ( error_msg ) , cause : Some ( Box :: new ( e ) ) , } } ) ? ; let result = source . into_iter ( ) . map ( | ( id , name , p_x , p_y , v_x , v_y , d_x , d_y ) | { let dest = if d_x . is_some ( ) && d_y . is_some ( ) { let d = Position { x : d_x . unwrap ( ) , y : d_y . unwrap ( ) , } ; Some ( d ) } else { None } ; NodeState { id , name , pos : Position { x : p_x , y : p_y } , vel : Velocity { x : v_x , y : v_y } , dest , } } ) . filter ( | ns | { if ns . dest . is_some ( ) { let dest = ns . dest . as_ref ( ) . unwrap ( ) ; ns . pos == * dest } else { false } } ) . collect ( ) ; // let mut result = HashMap::new(); // for (w_id, w_name, pos_x, pos_y, dest_x, dest_y, vel_x, vel_y) in source { //     let vel_magnitude = (vel_x.powi(2) + vel_y.powi(2)).sqrt(); //     let remaining_distance = euclidean_distance(pos_x, pos_y, dest_x, dest_y); //     let dist_threshold = vel_magnitude / 2.0; //     if remaining_distance <= dist_threshold { //         result.insert(w_id, (w_name, Position { x: pos_x, y: pos_y }, Velocity { x: vel_x, y: vel_y })); //     } // } Ok ( result ) }
Function exported exclusively for the use of the Master module.
Function exported exclusively for the use of the Master module .
conn
&PgConnection