Skip to content

Commit

Permalink
refactor: reuse PropertyPredefinedGraph in tman pkg (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoadonia authored Sep 24, 2024
1 parent da72d77 commit 4f3b32e
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 323 deletions.
84 changes: 40 additions & 44 deletions core/src/ten_manager/src/dev_server/graphs/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ use std::sync::{Arc, RwLock};

use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};
use ten_rust::pkg_info::graph::{
GraphConnection, GraphDestination, GraphMessageFlow,
};

use crate::dev_server::get_all_pkgs::get_all_pkgs;
use crate::dev_server::response::{ApiResponse, ErrorResponse, Status};
use crate::dev_server::DevServerState;
use ten_rust::pkg_info::pkg_type::PkgType;
use ten_rust::pkg_info::predefined_graphs::connection::PkgDestination;
use ten_rust::pkg_info::predefined_graphs::connection::{
PkgConnection, PkgMessageFlow,
};
use ten_rust::pkg_info::predefined_graphs::pkg_predefined_graphs_find;

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
Expand All @@ -38,36 +37,24 @@ pub struct DevServerConnection {
pub video_frame: Option<Vec<DevServerMessageFlow>>,
}

impl From<PkgConnection> for DevServerConnection {
fn from(conn: PkgConnection) -> Self {
impl From<GraphConnection> for DevServerConnection {
fn from(conn: GraphConnection) -> Self {
DevServerConnection {
app: conn.app,
extension_group: conn.extension_group,
extension: conn.extension,

cmd: if conn.cmd.is_empty() {
None
} else {
Some(get_dev_server_msg_flow_from_pkg(conn.cmd.clone()))
},

data: if conn.data.is_empty() {
None
} else {
Some(get_dev_server_msg_flow_from_pkg(conn.data.clone()))
},

audio_frame: if conn.audio_frame.is_empty() {
None
} else {
Some(get_dev_server_msg_flow_from_pkg(conn.audio_frame.clone()))
},

video_frame: if conn.video_frame.is_empty() {
None
} else {
Some(get_dev_server_msg_flow_from_pkg(conn.video_frame.clone()))
},
cmd: conn.cmd.map(get_dev_server_msg_flow_from_property),

data: conn.data.map(get_dev_server_msg_flow_from_property),

audio_frame: conn
.audio_frame
.map(get_dev_server_msg_flow_from_property),

video_frame: conn
.video_frame
.map(get_dev_server_msg_flow_from_property),
}
}
}
Expand All @@ -78,18 +65,22 @@ pub struct DevServerMessageFlow {
pub dest: Vec<DevServerDestination>,
}

impl From<PkgMessageFlow> for DevServerMessageFlow {
fn from(msg_flow: PkgMessageFlow) -> Self {
impl From<GraphMessageFlow> for DevServerMessageFlow {
fn from(msg_flow: GraphMessageFlow) -> Self {
DevServerMessageFlow {
name: msg_flow.name,
dest: get_dev_server_destination_from_pkg(msg_flow.dest),
dest: get_dev_server_destination_from_property(msg_flow.dest),
}
}
}

fn get_dev_server_msg_flow_from_pkg(
msg_flow: Vec<PkgMessageFlow>,
fn get_dev_server_msg_flow_from_property(
msg_flow: Vec<GraphMessageFlow>,
) -> Vec<DevServerMessageFlow> {
if msg_flow.is_empty() {
return vec![];
}

msg_flow.into_iter().map(|v| v.into()).collect()
}

Expand All @@ -100,8 +91,8 @@ pub struct DevServerDestination {
pub extension: String,
}

impl From<PkgDestination> for DevServerDestination {
fn from(destination: PkgDestination) -> Self {
impl From<GraphDestination> for DevServerDestination {
fn from(destination: GraphDestination) -> Self {
DevServerDestination {
app: destination.app,
extension_group: destination.extension_group,
Expand All @@ -110,8 +101,8 @@ impl From<PkgDestination> for DevServerDestination {
}
}

fn get_dev_server_destination_from_pkg(
destinations: Vec<PkgDestination>,
fn get_dev_server_destination_from_property(
destinations: Vec<GraphDestination>,
) -> Vec<DevServerDestination> {
destinations.into_iter().map(|v| v.into()).collect()
}
Expand Down Expand Up @@ -142,15 +133,20 @@ pub async fn get_graph_connections(
// specified graph_name.
if let Some(graph) =
pkg_predefined_graphs_find(&app_pkg.predefined_graphs, |g| {
g.name == graph_name
g.prop_predefined_graph.name == graph_name
})
{
// Convert the connections field to RespConnection.
let resp_connections: Vec<DevServerConnection> = graph
.connections
.iter()
.map(|conn| conn.clone().into())
.collect();
let connections =
graph.prop_predefined_graph.graph.connections.as_ref();
let resp_connections: Vec<DevServerConnection> =
match connections {
Some(connections) => connections
.iter()
.map(|conn| conn.clone().into())
.collect(),
None => vec![],
};

let response = ApiResponse {
status: Status::Ok,
Expand Down
7 changes: 5 additions & 2 deletions core/src/ten_manager/src/dev_server/graphs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ pub async fn get_graphs(
.predefined_graphs
.iter()
.map(|graph| RespGraph {
name: graph.name.clone(),
auto_start: graph.auto_start,
name: graph.prop_predefined_graph.name.clone(),
auto_start: graph
.prop_predefined_graph
.auto_start
.unwrap_or(false),
})
.collect();

Expand Down
33 changes: 21 additions & 12 deletions core/src/ten_manager/src/dev_server/graphs/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ fn update_graph_to_property(app_pkg: &mut PkgInfo) {
app_pkg
.predefined_graphs
.iter()
.cloned()
.map(|g| g.into())
.map(|g| g.prop_predefined_graph.clone())
.collect(),
);
} else {
Expand All @@ -49,8 +48,7 @@ fn update_graph_to_property(app_pkg: &mut PkgInfo) {
app_pkg
.predefined_graphs
.iter()
.cloned()
.map(|g| g.into())
.map(|g| g.prop_predefined_graph.clone())
.collect(),
),
uri: None,
Expand All @@ -65,8 +63,7 @@ fn update_graph_to_property(app_pkg: &mut PkgInfo) {
app_pkg
.predefined_graphs
.iter()
.cloned()
.map(|g| g.into())
.map(|g| g.prop_predefined_graph.clone())
.collect(),
),
uri: None,
Expand Down Expand Up @@ -129,11 +126,11 @@ pub async fn update_graph(
if let Some(old_graph) = app_pkg
.predefined_graphs
.iter_mut()
.find(|g| g.name == graph_name)
.find(|g| g.prop_predefined_graph.name == graph_name)
{
old_graph.auto_start = new_graph.auto_start;
old_graph.nodes = new_graph.nodes;
old_graph.connections = new_graph.connections;
old_graph.prop_predefined_graph =
new_graph.prop_predefined_graph;
}

update_graph_to_property(app_pkg);
Expand Down Expand Up @@ -256,12 +253,24 @@ mod tests {
let predefined_graph = app_pkg
.predefined_graphs
.iter()
.find(|g| g.name == "0")
.find(|g| g.prop_predefined_graph.name == "0")
.unwrap();

assert!(!predefined_graph.auto_start);
assert!(!predefined_graph
.prop_predefined_graph
.auto_start
.unwrap());
assert_eq!(predefined_graph.nodes.len(), 2);
assert_eq!(predefined_graph.connections.len(), 1);
assert_eq!(
predefined_graph
.prop_predefined_graph
.graph
.connections
.as_ref()
.unwrap()
.len(),
1
);

let property = app_pkg.property.as_ref().unwrap();
let property_predefined_graphs = property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,42 @@
use crate::dev_server::graphs::connections::{
DevServerConnection, DevServerDestination, DevServerMessageFlow,
};
use ten_rust::pkg_info::predefined_graphs::connection::{
PkgConnection, PkgDestination, PkgMessageFlow,
use ten_rust::pkg_info::graph::{
GraphConnection, GraphDestination, GraphMessageFlow,
};

impl From<DevServerConnection> for PkgConnection {
impl From<DevServerConnection> for GraphConnection {
fn from(dev_server_connection: DevServerConnection) -> Self {
PkgConnection {
GraphConnection {
app: dev_server_connection.app,
extension_group: dev_server_connection.extension_group,
extension: dev_server_connection.extension,

cmd: match dev_server_connection.cmd {
Some(cmd) => get_pkg_msg_flow_from_dev_server(cmd),
None => Vec::new(),
},
data: match dev_server_connection.data {
Some(data) => get_pkg_msg_flow_from_dev_server(data),
None => Vec::new(),
},
audio_frame: match dev_server_connection.audio_frame {
Some(audio_frame) => {
get_pkg_msg_flow_from_dev_server(audio_frame)
}
None => Vec::new(),
},
video_frame: match dev_server_connection.video_frame {
Some(video_frame) => {
get_pkg_msg_flow_from_dev_server(video_frame)
}
None => Vec::new(),
},
cmd: dev_server_connection
.cmd
.map(get_property_msg_flow_from_dev_server),
data: dev_server_connection
.data
.map(get_property_msg_flow_from_dev_server),
audio_frame: dev_server_connection
.audio_frame
.map(get_property_msg_flow_from_dev_server),
video_frame: dev_server_connection
.video_frame
.map(get_property_msg_flow_from_dev_server),
}
}
}

fn get_pkg_msg_flow_from_dev_server(
fn get_property_msg_flow_from_dev_server(
msg_flow: Vec<DevServerMessageFlow>,
) -> Vec<PkgMessageFlow> {
) -> Vec<GraphMessageFlow> {
msg_flow.into_iter().map(|v| v.into()).collect()
}

impl From<DevServerMessageFlow> for PkgMessageFlow {
impl From<DevServerMessageFlow> for GraphMessageFlow {
fn from(dev_server_msg_flow: DevServerMessageFlow) -> Self {
PkgMessageFlow {
GraphMessageFlow {
name: dev_server_msg_flow.name,
dest: dev_server_msg_flow
.dest
Expand All @@ -61,9 +53,9 @@ impl From<DevServerMessageFlow> for PkgMessageFlow {
}
}

impl From<DevServerDestination> for PkgDestination {
impl From<DevServerDestination> for GraphDestination {
fn from(dev_server_destination: DevServerDestination) -> Self {
PkgDestination {
GraphDestination {
app: dev_server_destination.app,
extension_group: dev_server_destination.extension_group,
extension: dev_server_destination.extension,
Expand Down
64 changes: 1 addition & 63 deletions core/src/ten_rust/src/pkg_info/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ use std::{collections::HashMap, str::FromStr};
use anyhow::Result;
use serde::{Deserialize, Serialize};

use super::{
pkg_type::PkgType,
predefined_graphs::{
connection::{PkgConnection, PkgDestination, PkgMessageFlow},
node::PkgNode,
},
PkgInfo,
};
use super::{pkg_type::PkgType, predefined_graphs::node::PkgNode, PkgInfo};
use crate::pkg_info::default_app_loc;

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down Expand Up @@ -157,61 +150,6 @@ pub struct GraphDestination {
pub extension: String,
}

fn get_property_msg_flow_from_pkg(
msg_flow: Vec<PkgMessageFlow>,
) -> Vec<GraphMessageFlow> {
msg_flow.into_iter().map(|v| v.into()).collect()
}

impl From<PkgConnection> for GraphConnection {
fn from(pkg_msg_flow: PkgConnection) -> Self {
GraphConnection {
app: pkg_msg_flow.app.clone(),
extension_group: pkg_msg_flow.extension_group.clone(),
extension: pkg_msg_flow.extension.clone(),
cmd: if pkg_msg_flow.cmd.is_empty() {
None
} else {
Some(get_property_msg_flow_from_pkg(pkg_msg_flow.cmd))
},
data: if pkg_msg_flow.data.is_empty() {
None
} else {
Some(get_property_msg_flow_from_pkg(pkg_msg_flow.data))
},
audio_frame: if pkg_msg_flow.audio_frame.is_empty() {
None
} else {
Some(get_property_msg_flow_from_pkg(pkg_msg_flow.audio_frame))
},
video_frame: if pkg_msg_flow.video_frame.is_empty() {
None
} else {
Some(get_property_msg_flow_from_pkg(pkg_msg_flow.video_frame))
},
}
}
}

impl From<PkgMessageFlow> for GraphMessageFlow {
fn from(pkg_msg_flow: PkgMessageFlow) -> Self {
GraphMessageFlow {
name: pkg_msg_flow.name.clone(),
dest: pkg_msg_flow.dest.iter().map(|d| d.clone().into()).collect(),
}
}
}

impl From<PkgDestination> for GraphDestination {
fn from(pkg_destination: PkgDestination) -> Self {
GraphDestination {
app: pkg_destination.app.clone(),
extension_group: pkg_destination.extension_group.clone(),
extension: pkg_destination.extension.clone(),
}
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;
Expand Down
Loading

0 comments on commit 4f3b32e

Please sign in to comment.