Skip to content

Commit

Permalink
BUG-652: send inferred edges along with created component
Browse files Browse the repository at this point in the history
  • Loading branch information
jobelenus committed Dec 19, 2024
1 parent 8aff0d2 commit ee8116c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 18 deletions.
10 changes: 10 additions & 0 deletions app/web/src/store/components.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,16 @@ export const useComponentsStore = (forceChangeSetId?: ChangeSetId) => {
if (data.changeSetId !== changeSetId) return;
this.rawComponentsById[data.component.id] = data.component;
this.processAndStoreRawComponent(data.component.id, {});

data.inferredEdges?.forEach((edge) => {
const e = edgeFromRawEdge({
isManagement: false,
isInferred: true,
})(edge);

this.rawEdgesById[e.id] = e;
this.processRawEdge(e.id);
});
},
},
{
Expand Down
1 change: 1 addition & 0 deletions app/web/src/store/realtime/realtime_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export type WsEventPayloadMap = {

ComponentCreated: {
component: RawComponent;
inferredEdges?: RawEdge[];
changeSetId: string;
};
ComponentDeleted: {
Expand Down
2 changes: 1 addition & 1 deletion lib/dal-test/src/expected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ impl ExpectComponent {
pub async fn upsert_parent(self, ctx: &DalContext, parent_id: impl Into<ComponentId>) {
dal::component::frame::Frame::upsert_parent(ctx, self.0, parent_id.into())
.await
.expect("could not upsert parent")
.expect("could not upsert parent");
}
}

Expand Down
18 changes: 18 additions & 0 deletions lib/dal/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3646,6 +3646,7 @@ impl Component {
#[serde(rename_all = "camelCase")]
pub struct ComponentCreatedPayload {
pub component: DiagramComponentView,
pub inferred_edges: Option<Vec<SummaryDiagramInferredEdge>>,
change_set_id: ChangeSetId,
}

Expand Down Expand Up @@ -3852,6 +3853,23 @@ impl WsEvent {
ctx,
WsPayload::ComponentCreated(ComponentCreatedPayload {
change_set_id: ctx.change_set_id(),
inferred_edges: None,
component,
}),
)
.await
}

pub async fn component_created_with_inferred_edges(
ctx: &DalContext,
component: DiagramComponentView,
inferred_edges: Option<Vec<SummaryDiagramInferredEdge>>,
) -> WsEventResult<Self> {
WsEvent::new(
ctx,
WsPayload::ComponentCreated(ComponentCreatedPayload {
change_set_id: ctx.change_set_id(),
inferred_edges: inferred_edges,
component,
}),
)
Expand Down
25 changes: 11 additions & 14 deletions lib/dal/src/component/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,31 +159,28 @@ impl Frame {
}

/// Provides the ability to attach or replace a child [`Component`]'s parent
#[instrument(level = "info", skip(ctx))]
//#[instrument(level = "info", skip(ctx))]
pub async fn upsert_parent(
ctx: &DalContext,
child_id: ComponentId,
new_parent_id: ComponentId,
) -> FrameResult<()> {
) -> FrameResult<Option<Vec<SummaryDiagramInferredEdge>>> {
// let's see if we need to even do anything
if let Some(current_parent_id) = Component::get_parent_by_id(ctx, child_id).await? {
if current_parent_id == new_parent_id {
return Ok(());
return Ok(None);
}
}

match Component::get_type_by_id(ctx, new_parent_id).await? {
ComponentType::ConfigurationFrameDown | ComponentType::ConfigurationFrameUp => {
Self::attach_child_to_parent_inner(ctx, new_parent_id, child_id).await?;
}
ComponentType::Component => {
return Err(FrameError::ParentIsNotAFrame(child_id, new_parent_id))
}
ComponentType::ConfigurationFrameDown | ComponentType::ConfigurationFrameUp => Ok(
Some(Self::attach_child_to_parent_inner(ctx, new_parent_id, child_id).await?),
),
ComponentType::Component => Err(FrameError::ParentIsNotAFrame(child_id, new_parent_id)),
ComponentType::AggregationFrame => {
return Err(FrameError::AggregateFramesUnsupported(new_parent_id))
Err(FrameError::AggregateFramesUnsupported(new_parent_id))
}
}
Ok(())
}

/// Removes the existing parent connection if it exists and adds the new one.
Expand All @@ -194,7 +191,7 @@ impl Frame {
ctx: &DalContext,
parent_id: ComponentId,
child_id: ComponentId,
) -> FrameResult<()> {
) -> FrameResult<Vec<SummaryDiagramInferredEdge>> {
// cache current map of input <-> output sockets based on what the parent knows about right now!!!!
let initial_impacted_values: HashSet<SocketAttributeValuePair> =
Self::get_all_inferred_connections_for_component_tree(ctx, parent_id, child_id).await?;
Expand Down Expand Up @@ -281,7 +278,7 @@ impl Frame {
inferred_edges_to_upsert.push(edge);
}
}
WsEvent::upsert_inferred_edges(ctx, inferred_edges_to_upsert)
WsEvent::upsert_inferred_edges(ctx, inferred_edges_to_upsert.clone())
.await?
.publish_on_commit(ctx)
.await?;
Expand Down Expand Up @@ -312,7 +309,7 @@ impl Frame {
)
.await?;

Ok(())
Ok(inferred_edges_to_upsert)
}

#[instrument(
Expand Down
2 changes: 1 addition & 1 deletion lib/dal/tests/integration_test/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async fn convert_component_to_frame_and_attach_no_nesting(ctx: &mut DalContext)

// Attempt to attach a child to a parent that is a not a frame.
match Frame::upsert_parent(ctx, fallout_component.id(), starfield_component.id()).await {
Ok(()) => panic!("attaching child to parent should fail if parent is not a frame"),
Ok(_) => panic!("attaching child to parent should fail if parent is not a frame"),
Err(FrameError::ParentIsNotAFrame(..)) => {}
Err(other_error) => panic!("unexpected error: {0}", other_error),
}
Expand Down
5 changes: 3 additions & 2 deletions lib/sdf-server/src/service/v2/view/create_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ pub async fn create_component(
));
}

let mut maybe_inferred_edges = None;
if let Some(frame_id) = request.parent_id {
Frame::upsert_parent(&ctx, component.id(), frame_id).await?;
maybe_inferred_edges = Frame::upsert_parent(&ctx, component.id(), frame_id).await?;

track(
&posthog_client,
Expand Down Expand Up @@ -224,7 +225,7 @@ pub async fn create_component(
&mut diagram_sockets,
)
.await?;
WsEvent::component_created(&ctx, payload)
WsEvent::component_created_with_inferred_edges(&ctx, payload, maybe_inferred_edges)
.await?
.publish_on_commit(&ctx)
.await?;
Expand Down

0 comments on commit ee8116c

Please sign in to comment.