Skip to content

Commit

Permalink
refactor: using PropertyPredefinedGraph instead of PkgPredefinedGraph (
Browse files Browse the repository at this point in the history
…#43)

Co-authored-by: Hu Yueh-Wei <[email protected]>
  • Loading branch information
leoadonia and halajohn authored Sep 25, 2024
1 parent cc3716d commit bba06a4
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 490 deletions.
13 changes: 6 additions & 7 deletions core/src/ten_manager/src/dev_server/graphs/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,13 @@ pub async fn get_graph_connections(
{
// If the app package has predefined graphs, find the one with the
// specified graph_name.
if let Some(graph) =
pkg_predefined_graphs_find(&app_pkg.predefined_graphs, |g| {
g.prop_predefined_graph.name == graph_name
})
{
if let Some(predefined_graph) = pkg_predefined_graphs_find(
app_pkg.get_predefined_graphs(),
|g| g.name == graph_name,
) {
// Convert the connections field to RespConnection.
let connections =
graph.prop_predefined_graph.graph.connections.as_ref();
let connections: Option<_> =
predefined_graph.graph.connections.as_ref();
let resp_connections: Vec<DevServerConnection> =
match connections {
Some(connections) => connections
Expand Down
10 changes: 4 additions & 6 deletions core/src/ten_manager/src/dev_server/graphs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ pub async fn get_graphs(
.find(|pkg| pkg.pkg_identity.pkg_type == PkgType::App)
{
let graphs: Vec<RespGraph> = app_pkg
.predefined_graphs
.get_predefined_graphs()
.unwrap_or(&vec![])
.iter()
.map(|graph| RespGraph {
name: graph.prop_predefined_graph.name.clone(),
auto_start: graph
.prop_predefined_graph
.auto_start
.unwrap_or(false),
name: graph.name.clone(),
auto_start: graph.auto_start.unwrap_or(false),
})
.collect();

Expand Down
189 changes: 92 additions & 97 deletions core/src/ten_manager/src/dev_server/graphs/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ use crate::dev_server::common::{
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::api::PkgCmdResult;
use ten_rust::pkg_info::api::{
PkgApiCmdLike, PkgApiDataLike, PkgPropertyAttributes, PkgPropertyItem,
};
use ten_rust::pkg_info::predefined_graphs::extension::{
get_extension_nodes_in_graph, get_extension_nodes_pkg_info,
use ten_rust::pkg_info::predefined_graphs::extension::get_extension_nodes_in_graph;
use ten_rust::pkg_info::{
api::PkgCmdResult, predefined_graphs::extension::get_pkg_info_for_extension,
};

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -223,7 +223,7 @@ pub async fn get_graph_nodes(
}

if let Some(all_pkgs) = &state.all_pkgs {
let mut extensions =
let extensions =
match get_extension_nodes_in_graph(&graph_name, all_pkgs) {
Ok(exts) => exts,
Err(err) => {
Expand All @@ -239,104 +239,99 @@ pub async fn get_graph_nodes(
}
};

if let Err(err) =
get_extension_nodes_pkg_info(&mut extensions, all_pkgs)
{
let error_response = ErrorResponse {
status: Status::Fail,
message: format!(
"Error fetching runtime extensions for graph '{}': {}",
graph_name, err
),
error: None,
};
return HttpResponse::NotFound().json(error_response);
}
let mut resp_extensions: Vec<DevServerExtension> = Vec::new();
for extension in &extensions {
let pkg_info = get_pkg_info_for_extension(extension, all_pkgs);
if let Err(err) = pkg_info {
let error_response = ErrorResponse {
status: Status::Fail,
message: format!(
"Error fetching runtime extensions for graph '{}': {}",
graph_name, err
),
error: None,
};
return HttpResponse::NotFound().json(error_response);
}

let resp_extensions: Vec<DevServerExtension> = extensions
.into_iter()
.map(|extension| DevServerExtension {
let pkg_info = pkg_info.unwrap();
resp_extensions.push(DevServerExtension {
addon: extension.addon.clone(),
name: extension.name.clone(),
extension_group: extension.extension_group.clone().unwrap(),
app: extension.app.clone(),
api: extension.pkg_info
// The pkg_info should be checked in 'get_extension_nodes_pkg_info', should not happen.
.unwrap_or_else(|| panic!("Addon '{}' used to instantiate extension '{}' is not found.", extension.addon, extension.name))
.api.as_ref().map(|api| {
DevServerApi {
property: if api.property.is_empty() {
None
} else {
Some(get_dev_server_property_hashmap_from_pkg(
api.property.clone(),
))
},

cmd_in: if api.cmd_in.is_empty() {
None
} else {
Some(get_dev_server_api_cmd_likes_from_pkg(
api.cmd_in.clone(),
))
},
cmd_out: if api.cmd_out.is_empty() {
None
} else {
Some(get_dev_server_api_cmd_likes_from_pkg(
api.cmd_out.clone(),
))
},

data_in: if api.data_in.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.data_in.clone(),
))
},
data_out: if api.data_out.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.data_out.clone(),
))
},

audio_frame_in: if api.audio_frame_in.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.audio_frame_in.clone(),
))
},
audio_frame_out: if api.audio_frame_out.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.audio_frame_out.clone(),
))
},

video_frame_in: if api.video_frame_in.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.video_frame_in.clone(),
))
},
video_frame_out: if api.video_frame_out.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.video_frame_out.clone(),
))
},
}
}),
property: extension.property,
})
.collect();
api: pkg_info.api.as_ref().map(|api| DevServerApi {
property: if api.property.is_empty() {
None
} else {
Some(get_dev_server_property_hashmap_from_pkg(
api.property.clone(),
))
},

cmd_in: if api.cmd_in.is_empty() {
None
} else {
Some(get_dev_server_api_cmd_likes_from_pkg(
api.cmd_in.clone(),
))
},
cmd_out: if api.cmd_out.is_empty() {
None
} else {
Some(get_dev_server_api_cmd_likes_from_pkg(
api.cmd_out.clone(),
))
},

data_in: if api.data_in.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.data_in.clone(),
))
},
data_out: if api.data_out.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.data_out.clone(),
))
},

audio_frame_in: if api.audio_frame_in.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.audio_frame_in.clone(),
))
},
audio_frame_out: if api.audio_frame_out.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.audio_frame_out.clone(),
))
},

video_frame_in: if api.video_frame_in.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.video_frame_in.clone(),
))
},
video_frame_out: if api.video_frame_out.is_empty() {
None
} else {
Some(get_dev_server_api_data_likes_from_pkg(
api.video_frame_out.clone(),
))
},
}),
property: extension.property.clone(),
});
}

let response = ApiResponse {
status: Status::Ok,
Expand Down
85 changes: 8 additions & 77 deletions core/src/ten_manager/src/dev_server/graphs/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
// Licensed under the Apache License, Version 2.0, with certain conditions.
// Refer to the "LICENSE" file in the root directory for more information.
//
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

use actix_web::{web, HttpResponse, Responder};
use serde::{Deserialize, Serialize};

use ten_rust::pkg_info::property::{Property, TenInProperty};
use ten_rust::pkg_info::PkgInfo;

use super::{connections::DevServerConnection, nodes::DevServerExtension};
use crate::dev_server::response::{ApiResponse, ErrorResponse, Status};
use crate::dev_server::{get_all_pkgs::get_all_pkgs, DevServerState};
Expand All @@ -31,51 +27,6 @@ pub struct GraphUpdateResponse {
pub success: bool,
}

fn update_graph_to_property(app_pkg: &mut PkgInfo) {
if let Some(ref mut property) = app_pkg.property {
// If _ten exists, update its predefined_graphs; otherwise, create _ten.
if let Some(ref mut ten) = property._ten {
ten.predefined_graphs = Some(
app_pkg
.predefined_graphs
.iter()
.map(|g| g.prop_predefined_graph.clone())
.collect(),
);
} else {
property._ten = Some(TenInProperty {
predefined_graphs: Some(
app_pkg
.predefined_graphs
.iter()
.map(|g| g.prop_predefined_graph.clone())
.collect(),
),
uri: None,
additional_fields: HashMap::new(),
});
}
} else {
// If property is None, create a new Property with _ten field.
let new_property = Property {
_ten: Some(TenInProperty {
predefined_graphs: Some(
app_pkg
.predefined_graphs
.iter()
.map(|g| g.prop_predefined_graph.clone())
.collect(),
),
uri: None,
additional_fields: HashMap::new(),
}),
additional_fields: HashMap::new(),
};

app_pkg.property = Some(new_property);
}
}

pub async fn update_graph(
req: web::Path<String>,
body: web::Json<GraphUpdateRequest>,
Expand Down Expand Up @@ -123,17 +74,7 @@ pub async fn update_graph(
};

// TODO(Liu): Add graph check.
if let Some(old_graph) = app_pkg
.predefined_graphs
.iter_mut()
.find(|g| g.prop_predefined_graph.name == graph_name)
{
old_graph.nodes = new_graph.nodes;
old_graph.prop_predefined_graph =
new_graph.prop_predefined_graph;
}

update_graph_to_property(app_pkg);
app_pkg.update_predefined_graph(&new_graph);

let response = ApiResponse {
status: Status::Ok,
Expand Down Expand Up @@ -250,25 +191,15 @@ mod tests {
.find(|pkg| pkg.pkg_identity.pkg_type == PkgType::App)
.unwrap();

let predefined_graph = app_pkg
.predefined_graphs
.iter()
.find(|g| g.prop_predefined_graph.name == "0")
.unwrap();
let predefined_graphs =
app_pkg.get_predefined_graphs().unwrap();
let predefined_graph =
predefined_graphs.iter().find(|g| g.name == "0").unwrap();

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

Expand Down
Loading

0 comments on commit bba06a4

Please sign in to comment.