Skip to content

Commit

Permalink
Suggest empty body to create new input on inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeshe committed Feb 28, 2025
1 parent 635ade1 commit 46975f9
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 67 deletions.
153 changes: 92 additions & 61 deletions lib/lightning_web/live/workflow_live/edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1620,14 +1620,23 @@ defmodule LightningWeb.WorkflowLive.Edit do
end

def handle_event("manual_run_change", %{"manual" => params}, socket) do
changeset =
WorkOrders.Manual.new(
params,
attrs =
%{
project: socket.assigns.project,
workflow: socket.assigns.workflow,
job: socket.assigns.selected_job,
created_by: socket.assigns.current_user
)
}
|> then(fn attrs ->
if params["dataclip_id"] == "" do
Map.put(attrs, :body, "{}")
else
attrs
end
end)

changeset =
WorkOrders.Manual.new(params, attrs)
|> Map.put(:action, :validate)

{:noreply, socket |> assign_manual_run_form(changeset)}
Expand Down Expand Up @@ -2013,73 +2022,95 @@ defmodule LightningWeb.WorkflowLive.Edit do
}
end

defp maybe_show_manual_run(socket) do
case socket.assigns do
%{selected_job: nil} ->
socket
|> assign(
manual_run_form: nil,
selectable_dataclips: []
)

%{selected_job: job, selection_mode: "expand"} = assigns
when not is_nil(job) ->
dataclip =
assigns[:follow_run] &&
get_selected_dataclip(assigns[:follow_run], job.id)
defp maybe_show_manual_run(%{assigns: %{selected_job: nil}} = socket) do
assign(socket,
manual_run_form: nil,
selectable_dataclips: []
)
end

body =
new_manual_run_form_body(
assigns.manual_run_form,
job,
dataclip
)
defp maybe_show_manual_run(
%{
assigns: %{
selected_job: selected_job,
selection_mode: "expand",
current_user: current_user,
manual_run_form: manual_run_form,
project: project,
workflow: workflow,
follow_run: follow_run
}
} = socket
) do
dataclip = follow_run && get_selected_dataclip(follow_run, selected_job.id)

body =
new_manual_run_form_body(
manual_run_form,
selected_job,
follow_run,
dataclip
)

changeset =
WorkOrders.Manual.new(
%{dataclip_id: dataclip && dataclip.id, body: body},
project: socket.assigns.project,
workflow: socket.assigns.workflow,
job: socket.assigns.selected_job,
user: socket.assigns.current_user
)
changeset =
%{
project: project,
workflow: workflow,
job: selected_job,
user: current_user
}
|> then(fn attrs ->
attrs =
if body == "{}" do
Map.put(attrs, :created_by, current_user)
else
attrs
end

selectable_dataclips =
Invocation.list_dataclips_for_job(%Job{id: job.id})
WorkOrders.Manual.new(
%{dataclip_id: dataclip && dataclip.id, body: body},
attrs
)
end)

step =
assigns[:follow_run] &&
Invocation.get_first_step_for_run_and_job(
assigns[:follow_run].id,
job.id
)
selectable_dataclips =
Invocation.list_dataclips_for_job(%Job{id: selected_job.id})

socket
|> assign_manual_run_form(changeset)
|> assign(step: step)
|> assign_dataclips(selectable_dataclips, dataclip)
step =
follow_run &&
Invocation.get_first_step_for_run_and_job(
follow_run.id,
selected_job.id
)

_ ->
socket
end
socket
|> assign_manual_run_form(changeset)
|> assign(step: step)
|> assign_dataclips(selectable_dataclips, dataclip)
end

defp maybe_show_manual_run(socket), do: socket

defp new_manual_run_form_body(
prev_manual_run_form,
selected_job,
follow_run,
selected_dataclip
) do
prev_job =
prev_manual_run_form &&
Ecto.Changeset.get_embed(
prev_manual_run_form.source,
:job,
:struct
)
if is_nil(selected_dataclip) do
prev_job =
prev_manual_run_form &&
Ecto.Changeset.get_embed(
prev_manual_run_form.source,
:job,
:struct
)

if is_nil(selected_dataclip) and is_struct(prev_job) and
prev_job.id == selected_job.id do
Ecto.Changeset.get_change(prev_manual_run_form.source, :body)
if is_struct(prev_job) and prev_job.id == selected_job.id do
Ecto.Changeset.get_change(prev_manual_run_form.source, :body)
else
if is_nil(follow_run), do: "{}"
end
end
end

Expand Down Expand Up @@ -2143,12 +2174,12 @@ defmodule LightningWeb.WorkflowLive.Edit do
] and Map.get(manual_run_form.params, "dataclip_id") do
true
else
!Enum.any?(manual_run_form.source.errors)
Enum.empty?(manual_run_form.source.errors)
end

!form_valid or
!changeset.valid? or
!(can_edit_workflow or can_run_workflow)
not form_valid or
not changeset.valid? or
not (can_edit_workflow or can_run_workflow)
end
end

Expand Down
8 changes: 4 additions & 4 deletions priv/bench/collections.exs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ stream_match_trigram =
# Process.exit(self(), :normal)

IO.puts("\n### Round record count ({microsecs, count}):")
:timer.tc(fn -> stream_all.() |> Enum.count() end) |> IO.inspect(label: "stream_all")
:timer.tc(fn -> stream_match_all.() |> Enum.count() end) |> IO.inspect(label: "stream_match_all")
:timer.tc(fn -> stream_match_prefix.() |> Enum.count() end) |> IO.inspect(label: "stream_match_prefix")
:timer.tc(fn -> stream_match_trigram.() |> Enum.count() end) |> IO.inspect(label: "stream_match_trigram")
:timer.tc(fn -> stream_all.() |> Enum.count() end) |> then(&IO.puts("stream_all: #{&1}"))
:timer.tc(fn -> stream_match_all.() |> Enum.count() end) |> then(&IO.puts("stream_match_all: #{&1}"))
:timer.tc(fn -> stream_match_prefix.() |> Enum.count() end) |> then(&IO.puts("stream_match_prefix: #{&1}"))
:timer.tc(fn -> stream_match_trigram.() |> Enum.count() end) |> then(&IO.puts("stream_match_trigram: #{&1}"))
IO.puts("\n")

Benchee.run(
Expand Down
4 changes: 2 additions & 2 deletions test/lightning_web/live/workflow_live/editor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ defmodule LightningWeb.WorkflowLive.EditorTest do
~p"/projects/#{p}/w/#{w}?#{[s: job, m: "expand", v: w.lock_version]}"
)

assert view
refute view
|> element(
"button[type='submit'][form='manual_run_form'][disabled]"
)
Expand Down Expand Up @@ -1384,7 +1384,7 @@ defmodule LightningWeb.WorkflowLive.EditorTest do
refute has_element?(view, "button", "Retry from here")

assert has_element?(view, "button:disabled", "Create New Work Order"),
"create new workorder button is disabled"
"create new workorder button should be disabled"

# Wait out all the async renders on RunViewerLive, avoiding Postgrex client
# disconnection warnings.
Expand Down

0 comments on commit 46975f9

Please sign in to comment.