Skip to content

Commit

Permalink
Relax restrictions when querying for eligible deployments (#1736)
Browse files Browse the repository at this point in the history
Eligible deployments now only match against product, platform, and
architecture. When there are no eligible deployments, helper text is
displayed.
  • Loading branch information
nshoes authored Jan 7, 2025
1 parent 24151c8 commit 8e4fc91
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/nerves_hub/deployments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,22 @@ defmodule NervesHub.Deployments do
defp ignore_same_deployment(query, %{deployment_id: deployment_id}) do
where(query, [d], d.id != ^deployment_id)
end

@doc """
Find all eligible deployments for a device, based on the firmware platform,
firmware architecture, and product.
This is purposefully less-strict then Deployments.matching_deployments/2
and should only be used when a human is choosing the deployment for a device.
"""
@spec eligible_deployments(Device.t()) :: [Deployment.t()]
def eligible_deployments(device) do
Deployment
|> join(:inner, [d], assoc(d, :firmware), as: :firmware)
|> preload([_, firmware: f], firmware: f)
|> where([d, _], d.product_id == ^device.product_id)
|> where([d, firmware: f], f.platform == ^device.firmware_metadata.platform)
|> where([d, firmware: f], f.architecture == ^device.firmware_metadata.architecture)
|> Repo.all()
end
end
4 changes: 2 additions & 2 deletions lib/nerves_hub_web/live/devices/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule NervesHubWeb.Live.Devices.Show do
|> schedule_health_check_timer()
|> assign(:fwup_progress, nil)
|> audit_log_assigns(1)
|> assign(:eligible_deployments, Deployments.matching_deployments(device))
|> assign(:eligible_deployments, Deployments.eligible_deployments(device))
|> ok()
end

Expand Down Expand Up @@ -382,7 +382,7 @@ defmodule NervesHubWeb.Live.Devices.Show do
socket
|> assign(:device, device)
|> assign(:deployment, nil)
|> assign(:eligible_deployments, Deployments.matching_deployments(device))
|> assign(:eligible_deployments, Deployments.eligible_deployments(device))
|> put_flash(:info, "Device successfully removed from the deployment")
|> noreply()
end
Expand Down
4 changes: 4 additions & 0 deletions lib/nerves_hub_web/live/devices/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@
<span :if={is_nil(@deployment)} class="color-white-50">No Assigned Deployment</span>
</div>

<div :if={Enum.empty?(@eligible_deployments) && is_nil(@device.deployment_id)}>
No Eligible Deployments
</div>

<div :if={Enum.any?(@eligible_deployments) && is_nil(@device.deployment_id)}>
<div class="help-text mb-1">
Eligible Deployments
Expand Down
14 changes: 14 additions & 0 deletions test/nerves_hub_web/live/devices/show_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,20 @@ defmodule NervesHubWeb.Live.Devices.ShowTest do
assert Repo.reload(device) |> Map.get(:deployment_id)
assert length(AuditLogs.logs_for(device)) == 1
end

test "'no eligible deployments' text displays properly", %{
conn: conn,
org: org,
product: product,
device: device,
deployment: deployment
} do
_ = Repo.delete!(deployment)

conn
|> visit("/org/#{org.name}/#{product.name}/devices/#{device.identifier}")
|> assert_has("div", text: "No Eligible Deployments")
end
end

def device_show_path(%{device: device, org: org, product: product}) do
Expand Down

0 comments on commit 8e4fc91

Please sign in to comment.