Skip to content

Commit

Permalink
Derive dataclip from attempt in inspector (#1557)
Browse files Browse the repository at this point in the history
* Populate dataclip in job inspector via attempt and job URL parameters

* update changelog and function name

---------

Co-authored-by: Taylor Downs <[email protected]>
  • Loading branch information
midigofrank and taylordowns2000 authored Dec 12, 2023
1 parent 3b6c811 commit 7197f65
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres to

### Changed

- Derive dataclip in inspector from the attempt & step
[#1551](https://github.com/OpenFn/Lightning/issues/1551)
- Updated CLI to 0.4.10 (fixes logging)
- Changed UserBackupToken model to use UTC timestamps (6563cb77)
- Restore FK relationship between `work_orders` and `attempts` pending a
Expand Down
16 changes: 16 additions & 0 deletions lib/lightning/invocation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ defmodule Lightning.Invocation do
Repo.one(query)
end

@spec get_dataclip_for_attempt_and_job(
attempt_id :: Ecto.UUID.t(),
job_id :: Ecto.UUID.t()
) ::
Dataclip.t() | nil
def get_dataclip_for_attempt_and_job(attempt_id, job_id) do
query =
from d in Query.dataclip_with_body(),
join: r in Lightning.Invocation.Run,
on: r.input_dataclip_id == d.id and r.job_id == ^job_id,
join: a in assoc(r, :attempts),
on: a.id == ^attempt_id

Repo.one(query)
end

@doc """
Gets a single dataclip.
Expand Down
20 changes: 11 additions & 9 deletions lib/lightning_web/live/workflow_live/edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule LightningWeb.WorkflowLive.Edit do
@moduledoc false
use LightningWeb, :live_view

alias Lightning.Invocation
alias Lightning.Policies.Permissions
alias Lightning.Policies.ProjectUsers
alias Lightning.WorkOrders
Expand Down Expand Up @@ -765,9 +766,7 @@ defmodule LightningWeb.WorkflowLive.Edit do
when not is_nil(job) ->
dataclip =
assigns[:follow_attempt_id] &&
Lightning.Invocation.get_dataclip_for_attempt(
assigns[:follow_attempt_id]
)
get_selected_dataclip(assigns[:follow_attempt_id], job.id)

changeset =
WorkOrders.Manual.new(
Expand All @@ -779,27 +778,30 @@ defmodule LightningWeb.WorkflowLive.Edit do
)

selectable_dataclips =
Lightning.Invocation.list_dataclips_for_job(%Lightning.Workflows.Job{
id: job.id
})
Invocation.list_dataclips_for_job(%Job{id: job.id})

socket
|> assign_manual_run_form(changeset)
|> assign(
selectable_dataclips:
maybe_add_attempt_dataclip(selectable_dataclips, dataclip)
maybe_add_selected_dataclip(selectable_dataclips, dataclip)
)

_ ->
socket
end
end

defp maybe_add_attempt_dataclip(selectable_dataclips, nil) do
defp get_selected_dataclip(attempt_id, job_id) do
Invocation.get_dataclip_for_attempt_and_job(attempt_id, job_id) ||
Invocation.get_dataclip_for_attempt(attempt_id)
end

defp maybe_add_selected_dataclip(selectable_dataclips, nil) do
selectable_dataclips
end

defp maybe_add_attempt_dataclip(selectable_dataclips, dataclip) do
defp maybe_add_selected_dataclip(selectable_dataclips, dataclip) do
if Enum.find(selectable_dataclips, fn dc -> dc.id == dataclip.id end) do
selectable_dataclips
else
Expand Down
76 changes: 76 additions & 0 deletions test/lightning_web/live/workflow_live/editor_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,82 @@ defmodule LightningWeb.WorkflowLive.EditorTest do
)
|> has_element?()
end

test "selects the input dataclip for the attempt run if an attempt is followed",
%{
conn: conn,
project: project,
workflow: %{jobs: [job_1, job_2 | _rest]} = workflow
} do
input_dataclip = insert(:dataclip, project: project, type: :http_request)

output_dataclip =
insert(:dataclip,
project: project,
type: :run_result,
body: %{"val" => Ecto.UUID.generate()}
)

%{attempts: [attempt]} =
insert(:workorder,
workflow: workflow,
dataclip: input_dataclip,
attempts: [
build(:attempt,
dataclip: input_dataclip,
starting_job: job_1,
runs: [
build(:run,
job: job_1,
input_dataclip: input_dataclip,
output_dataclip: output_dataclip,
started_at: build(:timestamp),
finished_at: build(:timestamp),
exit_reason: "success"
),
build(:run,
job: job_2,
input_dataclip: output_dataclip,
output_dataclip:
build(:dataclip,
type: :run_result,
body: %{}
),
started_at: build(:timestamp),
finished_at: build(:timestamp),
exit_reason: "success"
)
]
)
]
)

# insert 3 new dataclips
insert_list(3, :dataclip, project: project)

{:ok, view, _html} =
live(
conn,
~p"/projects/#{project}/w/#{workflow}?#{[s: job_2.id, a: attempt.id, m: "expand"]}"
)

# the run dataclip is different from the attempt dataclip.
# this assertion means that the attempt dataclip won't be selected
refute attempt.dataclip_id == output_dataclip.id

# the form has the dataclips body
assert element(view, "#manual-job-#{job_2.id} form") |> render() =~
output_dataclip.body["val"]

# the run dataclip is selected
element =
view
|> element(
"select#manual_run_form_dataclip_id option[value='#{output_dataclip.id}']"
)

assert render(element) =~ "selected"
end
end

describe "Editor events" do
Expand Down

0 comments on commit 7197f65

Please sign in to comment.