Skip to content

Commit 2d5215b

Browse files
authored
refactor: replace update_graph_connections_all_fields across multiple modules (#795)
1 parent 24d3b44 commit 2d5215b

File tree

9 files changed

+140
-176
lines changed

9 files changed

+140
-176
lines changed

core/src/ten_manager/src/designer/graphs/connections/add.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ use crate::{
2929
};
3030

3131
use crate::graph::{
32-
graphs_cache_find_by_id_mut, update_graph_connections_all_fields,
32+
graphs_cache_find_by_id_mut,
33+
update_graph_connections_in_property_all_fields,
3334
};
3435

3536
#[derive(Serialize, Deserialize)]
@@ -97,27 +98,6 @@ fn create_graph_connection(
9798
connection
9899
}
99100

100-
/// Update the property.json file with the new connection.
101-
fn update_property_file(
102-
base_dir: &str,
103-
property: &mut ten_rust::pkg_info::property::Property,
104-
graph_name: &str,
105-
connection: &GraphConnection,
106-
) -> Result<()> {
107-
// Update only with the new connection.
108-
let connections_to_add = vec![connection.clone()];
109-
110-
// Update the property_all_fields map and write to property.json.
111-
update_graph_connections_all_fields(
112-
base_dir,
113-
&mut property.all_fields,
114-
graph_name,
115-
Some(&connections_to_add),
116-
None,
117-
None,
118-
)
119-
}
120-
121101
pub async fn add_graph_connection_endpoint(
122102
request_payload: web::Json<AddGraphConnectionRequestPayload>,
123103
state: web::Data<Arc<DesignerState>>,
@@ -166,15 +146,19 @@ pub async fn add_graph_connection_endpoint(
166146
{
167147
// Update property.json file with the updated graph.
168148
if let Some(property) = &mut pkg_info.property {
169-
// Create a new connection object.
170-
let connection = create_graph_connection(&request_payload);
149+
// Create a new connection object, and update only with the new
150+
// connection.
151+
let connections_to_add =
152+
vec![create_graph_connection(&request_payload)];
171153

172-
// Update the property.json file.
173-
if let Err(e) = update_property_file(
154+
// Update the property_all_fields map and write to property.json.
155+
if let Err(e) = update_graph_connections_in_property_all_fields(
174156
&pkg_info.url,
175-
property,
157+
&mut property.all_fields,
176158
graph_info.name.as_ref().unwrap(),
177-
&connection,
159+
Some(&connections_to_add),
160+
None,
161+
None,
178162
) {
179163
eprintln!(
180164
"Warning: Failed to update property.json file: {}",

core/src/ten_manager/src/designer/graphs/connections/delete.rs

Lines changed: 76 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ use crate::{
2424
response::{ApiResponse, ErrorResponse, Status},
2525
DesignerState,
2626
},
27-
graph::{graphs_cache_find_by_id_mut, update_graph_connections_all_fields},
27+
graph::{
28+
graphs_cache_find_by_id_mut,
29+
update_graph_connections_in_property_all_fields,
30+
},
2831
pkg_info::belonging_pkg_info_find_by_graph_info_mut,
2932
};
3033

@@ -45,7 +48,7 @@ pub struct DeleteGraphConnectionResponsePayload {
4548
pub success: bool,
4649
}
4750

48-
fn delete_connection(
51+
fn graph_delete_connection(
4952
graph: &mut Graph,
5053
src_app: Option<String>,
5154
src_extension: String,
@@ -164,7 +167,7 @@ pub async fn delete_graph_connection_endpoint(
164167
};
165168

166169
// Delete the connection.
167-
match delete_connection(
170+
if let Err(err) = graph_delete_connection(
168171
&mut graph_info.graph,
169172
request_payload.src_app.clone(),
170173
request_payload.src_extension.clone(),
@@ -173,89 +176,83 @@ pub async fn delete_graph_connection_endpoint(
173176
request_payload.dest_app.clone(),
174177
request_payload.dest_extension.clone(),
175178
) {
176-
Ok(_) => {
177-
if let Ok(Some(pkg_info)) =
178-
belonging_pkg_info_find_by_graph_info_mut(
179-
&mut pkgs_cache,
180-
graph_info,
181-
)
182-
{
183-
// Update property.json file to remove the connection.
184-
if let Some(property) = &mut pkg_info.property {
185-
// Create a GraphConnection representing what we
186-
// want to remove.
187-
let mut connection = GraphConnection {
188-
app: request_payload.src_app.clone(),
189-
extension: request_payload.src_extension.clone(),
190-
cmd: None,
191-
data: None,
192-
audio_frame: None,
193-
video_frame: None,
194-
};
195-
196-
// Create destination for the message flow.
197-
let destination = GraphDestination {
198-
app: request_payload.dest_app.clone(),
199-
extension: request_payload.dest_extension.clone(),
200-
msg_conversion: None,
201-
};
179+
let error_response = ErrorResponse {
180+
status: Status::Fail,
181+
message: format!("Failed to delete connection: {}", err),
182+
error: None,
183+
};
184+
return Ok(HttpResponse::BadRequest().json(error_response));
185+
}
202186

203-
// Create the message flow with destination.
204-
let message_flow = GraphMessageFlow {
205-
name: request_payload.msg_name.clone(),
206-
dest: vec![destination],
207-
};
187+
if let Ok(Some(pkg_info)) =
188+
belonging_pkg_info_find_by_graph_info_mut(&mut pkgs_cache, graph_info)
189+
{
190+
// Update property.json file to remove the connection.
191+
if let Some(property) = &mut pkg_info.property {
192+
// Create a GraphConnection representing what we
193+
// want to remove.
194+
let mut connection = GraphConnection {
195+
app: request_payload.src_app.clone(),
196+
extension: request_payload.src_extension.clone(),
197+
cmd: None,
198+
data: None,
199+
audio_frame: None,
200+
video_frame: None,
201+
};
208202

209-
// Set the appropriate message type field.
210-
match request_payload.msg_type {
211-
MsgType::Cmd => {
212-
connection.cmd = Some(vec![message_flow]);
213-
}
214-
MsgType::Data => {
215-
connection.data = Some(vec![message_flow]);
216-
}
217-
MsgType::AudioFrame => {
218-
connection.audio_frame = Some(vec![message_flow]);
219-
}
220-
MsgType::VideoFrame => {
221-
connection.video_frame = Some(vec![message_flow]);
222-
}
223-
}
203+
// Create destination for the message flow.
204+
let destination = GraphDestination {
205+
app: request_payload.dest_app.clone(),
206+
extension: request_payload.dest_extension.clone(),
207+
msg_conversion: None,
208+
};
224209

225-
let connections_to_remove = vec![connection];
210+
// Create the message flow with destination.
211+
let message_flow = GraphMessageFlow {
212+
name: request_payload.msg_name.clone(),
213+
dest: vec![destination],
214+
};
226215

227-
// Write the updated property_all_fields map to
228-
// property.json.
229-
if let Err(e) = update_graph_connections_all_fields(
230-
&pkg_info.url,
231-
&mut property.all_fields,
232-
graph_info.name.as_ref().unwrap(),
233-
None,
234-
Some(&connections_to_remove),
235-
None,
236-
) {
237-
eprintln!(
238-
"Warning: Failed to update property.json file: {}",
239-
e
240-
);
241-
}
216+
// Set the appropriate message type field.
217+
match request_payload.msg_type {
218+
MsgType::Cmd => {
219+
connection.cmd = Some(vec![message_flow]);
220+
}
221+
MsgType::Data => {
222+
connection.data = Some(vec![message_flow]);
223+
}
224+
MsgType::AudioFrame => {
225+
connection.audio_frame = Some(vec![message_flow]);
226+
}
227+
MsgType::VideoFrame => {
228+
connection.video_frame = Some(vec![message_flow]);
242229
}
243230
}
244231

245-
let response = ApiResponse {
246-
status: Status::Ok,
247-
data: DeleteGraphConnectionResponsePayload { success: true },
248-
meta: None,
249-
};
250-
Ok(HttpResponse::Ok().json(response))
251-
}
252-
Err(err) => {
253-
let error_response = ErrorResponse {
254-
status: Status::Fail,
255-
message: format!("Failed to delete connection: {}", err),
256-
error: None,
257-
};
258-
Ok(HttpResponse::BadRequest().json(error_response))
232+
let connections_to_remove = vec![connection];
233+
234+
// Write the updated property_all_fields map to
235+
// property.json.
236+
if let Err(e) = update_graph_connections_in_property_all_fields(
237+
&pkg_info.url,
238+
&mut property.all_fields,
239+
graph_info.name.as_ref().unwrap(),
240+
None,
241+
Some(&connections_to_remove),
242+
None,
243+
) {
244+
eprintln!(
245+
"Warning: Failed to update property.json file: {}",
246+
e
247+
);
248+
}
259249
}
260250
}
251+
252+
let response = ApiResponse {
253+
status: Status::Ok,
254+
data: DeleteGraphConnectionResponsePayload { success: true },
255+
meta: None,
256+
};
257+
Ok(HttpResponse::Ok().json(response))
261258
}

core/src/ten_manager/src/designer/graphs/connections/msg_conversion/update.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ use crate::{
3030
connections::validate::{
3131
validate_connection_schema, MsgConversionValidateInfo,
3232
},
33-
graphs_cache_find_by_id_mut, update_graph_connections_all_fields,
33+
graphs_cache_find_by_id_mut,
34+
update_graph_connections_in_property_all_fields,
3435
},
3536
pkg_info::belonging_pkg_info_find_by_graph_info_mut,
3637
};
@@ -161,7 +162,7 @@ fn update_property_all_fields(
161162
let connections_to_modify = vec![connection];
162163

163164
// Update the property.json file.
164-
if let Err(e) = update_graph_connections_all_fields(
165+
if let Err(e) = update_graph_connections_in_property_all_fields(
165166
&pkg_info.url,
166167
&mut property.all_fields,
167168
graph_info.name.as_ref().unwrap(),

core/src/ten_manager/src/designer/graphs/nodes/delete.rs

Lines changed: 41 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// Licensed under the Apache License, Version 2.0, with certain conditions.
55
// Refer to the "LICENSE" file in the root directory for more information.
66
//
7-
use std::collections::HashMap;
87
use std::sync::Arc;
98

109
use actix_web::{web, HttpResponse, Responder};
@@ -13,8 +12,7 @@ use serde::{Deserialize, Serialize};
1312
use uuid::Uuid;
1413

1514
use ten_rust::{
16-
base_dir_pkg_info::PkgsInfoInApp,
17-
graph::{graph_info::GraphInfo, node::GraphNode, Graph},
15+
graph::{node::GraphNode, Graph},
1816
pkg_info::{pkg_type::PkgType, pkg_type_and_name::PkgTypeAndName},
1917
};
2018

@@ -177,15 +175,46 @@ pub async fn delete_graph_node_endpoint(
177175
return Ok(HttpResponse::BadRequest().json(error_response));
178176
}
179177

180-
// Try to update property.json file if possible
181-
update_property_json_if_needed(
182-
&mut pkgs_cache,
183-
graph_info,
184-
&request_payload.name,
185-
&request_payload.addon,
186-
request_payload.extension_group.as_deref(),
187-
request_payload.app.as_deref(),
188-
);
178+
// Try to update property.json file if possible.
179+
// First, try to find the belonging package info
180+
if let Ok(Some(pkg_info)) =
181+
belonging_pkg_info_find_by_graph_info_mut(&mut pkgs_cache, graph_info)
182+
{
183+
// Check if property exists
184+
if let Some(property) = &mut pkg_info.property {
185+
// Create the GraphNode we want to remove
186+
let node_to_remove = GraphNode {
187+
type_and_name: PkgTypeAndName {
188+
pkg_type: PkgType::Extension,
189+
name: request_payload.name.to_string(),
190+
},
191+
addon: request_payload.addon.to_string(),
192+
extension_group: request_payload
193+
.extension_group
194+
.as_deref()
195+
.map(String::from),
196+
app: request_payload.app.as_deref().map(String::from),
197+
property: None,
198+
};
199+
200+
let nodes_to_remove = vec![node_to_remove];
201+
202+
// Update property.json file
203+
if let Err(e) = update_graph_node_all_fields(
204+
&pkg_info.url,
205+
&mut property.all_fields,
206+
graph_info.name.as_ref().unwrap(),
207+
None,
208+
Some(&nodes_to_remove),
209+
None,
210+
) {
211+
eprintln!(
212+
"Warning: Failed to update property.json file: {}",
213+
e
214+
);
215+
}
216+
}
217+
}
189218

190219
// Return success response
191220
let response = ApiResponse {
@@ -195,50 +224,3 @@ pub async fn delete_graph_node_endpoint(
195224
};
196225
Ok(HttpResponse::Ok().json(response))
197226
}
198-
199-
/// Helper function to update property.json after node deletion
200-
fn update_property_json_if_needed(
201-
pkgs_cache: &mut HashMap<String, PkgsInfoInApp>,
202-
graph_info: &mut GraphInfo,
203-
node_name: &str,
204-
addon: &str,
205-
extension_group: Option<&str>,
206-
app: Option<&str>,
207-
) {
208-
// Try to find the belonging package info
209-
if let Ok(Some(pkg_info)) =
210-
belonging_pkg_info_find_by_graph_info_mut(pkgs_cache, graph_info)
211-
{
212-
// Check if property exists
213-
let property = match &mut pkg_info.property {
214-
Some(property) => property,
215-
None => return,
216-
};
217-
218-
// Create the GraphNode we want to remove
219-
let node_to_remove = GraphNode {
220-
type_and_name: PkgTypeAndName {
221-
pkg_type: PkgType::Extension,
222-
name: node_name.to_string(),
223-
},
224-
addon: addon.to_string(),
225-
extension_group: extension_group.map(String::from),
226-
app: app.map(String::from),
227-
property: None,
228-
};
229-
230-
let nodes_to_remove = vec![node_to_remove];
231-
232-
// Update property.json file
233-
if let Err(e) = update_graph_node_all_fields(
234-
&pkg_info.url,
235-
&mut property.all_fields,
236-
graph_info.name.as_ref().unwrap(),
237-
None,
238-
Some(&nodes_to_remove),
239-
None,
240-
) {
241-
eprintln!("Warning: Failed to update property.json file: {}", e);
242-
}
243-
}
244-
}

0 commit comments

Comments
 (0)