Skip to content

Commit

Permalink
Expand workorder row when workorder_id is specified in the filter
Browse files Browse the repository at this point in the history
  • Loading branch information
midigofrank authored and stuartc committed Dec 14, 2023
1 parent 6a6d36b commit 96ae4bf
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to
[#1064](https://github.com/OpenFn/Lightning/issues/1064)
- Custom metric to track Attempt queue delay
[#1556](https://github.com/OpenFn/Lightning/issues/1556)
- Expand workorder row when a `workorder_id` is specified in the filter
[#1515](https://github.com/OpenFn/Lightning/issues/1515)

### Changed

Expand Down
22 changes: 20 additions & 2 deletions lib/lightning_web/live/run_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule LightningWeb.RunLive.Index do
body: :boolean,
log: :boolean,
workflow_id: :string,
workorder_id: :string,
date_after: :utc_datetime,
date_before: :utc_datetime,
wo_date_after: :utc_datetime,
Expand Down Expand Up @@ -117,11 +118,13 @@ defmodule LightningWeb.RunLive.Index do

@impl true
def handle_params(params, _url, socket) do
%{filters: filters, project: project} = socket.assigns
%{project: project} = socket.assigns
filters = Map.get(params, "filters", init_filters())

{:noreply,
socket
|> assign(
filters: filters,
page_title: "History",
run: %Run{},
filters_changeset: filters_changeset(filters),
Expand Down Expand Up @@ -169,7 +172,8 @@ defmodule LightningWeb.RunLive.Index do
|> assign(
page: searched_page,
async_page: AsyncResult.ok(async_page, searched_page)
)}
)
|> maybe_show_selected_workorder_details()}
end

def handle_async(:load_workorders, {:exit, reason}, socket) do
Expand Down Expand Up @@ -490,4 +494,18 @@ defmodule LightningWeb.RunLive.Index do
Keyword.merge(route_params, filters: filters)
)
end

defp maybe_show_selected_workorder_details(socket) do
%{filters_changeset: changeset} = socket.assigns

if workorder_id = Ecto.Changeset.get_change(changeset, :workorder_id) do
send_update(LightningWeb.RunLive.WorkOrderComponent,
id: workorder_id,
show_details: true,
show_prev_attempts: true
)
end

socket
end
end
4 changes: 4 additions & 0 deletions lib/lightning_web/live/run_live/workorder_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ defmodule LightningWeb.RunLive.WorkOrderComponent do
{:ok, socket |> assign(assigns) |> set_work_order_details(work_order)}
end

def update(assigns, socket) do
{:ok, assign(socket, assigns)}
end

defp set_work_order_details(socket, work_order) do
last_run = List.last(List.first(work_order.attempts).runs)

Expand Down
109 changes: 109 additions & 0 deletions test/lightning_web/live/work_order_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,115 @@ defmodule LightningWeb.RunWorkOrderTest do
refute has_element?(view, "#attempt_#{attempt_2.id}.hidden")
assert has_element?(view, "#attempt_#{attempt_2.id}")
end

test "workorder row gets expanded by default if workorder_id is supplied in the filter",
%{conn: conn, user: user} do
project =
insert(:project,
project_users: [%{role: :admin, user: user}]
)

workflow = insert(:workflow, project: project)

job =
insert(:job, %{
body: "fn(state => state)",
name: "some name",
adaptor: "@openfn/language-common",
workflow: workflow,
project_credential: nil
})

trigger =
insert(:trigger,
workflow: workflow,
type: :webhook
)

insert(:edge,
workflow: workflow,
source_trigger: trigger,
target_job: job,
condition: :always,
enabled: true
)

dataclip = insert(:dataclip, project: project)

workorder =
insert(:workorder,
state: :success,
workflow: workflow,
trigger: trigger,
dataclip: dataclip,
last_activity: DateTime.utc_now()
)

attempt_1 =
insert(:attempt,
work_order: workorder,
state: :failed,
starting_trigger: trigger,
inserted_at: build(:timestamp),
dataclip: dataclip,
runs:
build_list(1, :run, %{
job: job,
exit_reason: "fail",
started_at: build(:timestamp),
finished_at: build(:timestamp),
input_dataclip: dataclip
})
)

attempt_2 =
insert(:attempt,
state: :success,
work_order: workorder,
starting_job: job,
dataclip: dataclip,
runs:
build_list(1, :run,
job: job,
started_at: build(:timestamp),
finished_at: build(:timestamp),
input_dataclip: dataclip
)
)

{:ok, view, _html} =
live_async(
conn,
Routes.project_run_index_path(conn, :index, project.id)
)

# workorder is present
assert has_element?(view, "#workorder-#{workorder.id}")

# both attempts are not present
refute has_element?(view, "#attempt_#{attempt_1.id}")
refute has_element?(view, "#attempt_#{attempt_2.id}")

# lets add the workorder_id
{:ok, view, _html} =
live_async(
conn,
Routes.project_run_index_path(conn, :index, project.id,
filters: %{workorder_id: workorder.id}
)
)

# workorder is present
assert has_element?(view, "#workorder-#{workorder.id}")

# both attempts are present
assert has_element?(view, "#attempt_#{attempt_1.id}")
assert has_element?(view, "#attempt_#{attempt_2.id}")

# both attempts are visible
refute has_element?(view, "#attempt_#{attempt_1.id}.hidden")
refute has_element?(view, "#attempt_#{attempt_2.id}.hidden")
end
end

describe "handle_async/3" do
Expand Down

0 comments on commit 96ae4bf

Please sign in to comment.