Skip to content

Commit

Permalink
Move some Deployment info around
Browse files Browse the repository at this point in the history
And fix the device count
  • Loading branch information
joshk committed Jan 7, 2025
1 parent 0aab022 commit 933174c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
11 changes: 8 additions & 3 deletions lib/nerves_hub/deployments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ defmodule NervesHub.Deployments do
|> Map.new()
end

@spec get_deployment_device_count(integer()) :: %{integer() => integer()}
@spec get_deployment_device_count(Deployment.t()) :: term() | nil
def get_deployment_device_count(%Deployment{id: id}) do
get_deployment_device_count(id)
end

@spec get_deployment_device_count(integer()) :: term() | nil
def get_deployment_device_count(deployment_id) do
Device
|> select([d], count(d.id))
|> where([d], d.deployment_id == ^deployment_id)
|> Repo.one()
|> Repo.exclude_deleted()
|> Repo.aggregate(:count)
end

@spec get_deployments_by_firmware(integer()) :: [Deployment.t()]
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/live/deployments/edit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule NervesHubWeb.Live.Deployments.Edit do
deployment =
Deployments.get_by_product_and_name!(product, name) |> NervesHub.Repo.preload(:firmware)

current_device_count = Deployments.get_deployment_device_count(deployment.id)
current_device_count = Deployments.get_deployment_device_count(deployment)

archives = Archives.all_by_product(deployment.product)
firmwares = Firmwares.get_firmwares_for_deployment(deployment)
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/live/deployments/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule NervesHubWeb.Live.Deployments.Show do
|> Map.put(:anchor, "latest-activity")

inflight_updates = Devices.inflight_updates_for(deployment)
current_device_count = Deployments.get_deployment_device_count(deployment.id)
current_device_count = Deployments.get_deployment_device_count(deployment)

socket
|> page_title("Deployment - #{deployment.name} - #{product.name}")
Expand Down
22 changes: 18 additions & 4 deletions lib/nerves_hub_web/live/deployments/show.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@
<%= if @deployment.is_active, do: "On", else: "Off" %>
</p>
</div>
<div>
<div class="help-text">Arch</div>
<p>
<%= @firmware.architecture %>
</p>
</div>
<div>
<div class="help-text">Platform</div>
<p>
<%= @firmware.platform %>
</p>
</div>
<div id="device-count">
<div class="help-text">Device count</div>
<p>
<%= @current_device_count %>
</p>
</div>
</div>

<div class="divider"></div>
Expand All @@ -51,10 +69,6 @@
</.link>
</span>
</div>
<div>
<div class="help-text mb-1">Current device count</div>
<p><%= @current_device_count %></p>
</div>
<div class="row">
<div class="col-lg-6 mb-1">
<div class="help-text mb-1 tooltip-label">
Expand Down
34 changes: 34 additions & 0 deletions test/nerves_hub_web/live/deployments/show_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule NervesHubWeb.Live.Deployments.ShowTest do

alias NervesHub.AuditLogs
alias NervesHub.Deployments
alias NervesHub.Devices
alias NervesHub.Devices.Device
alias NervesHub.Fixtures

test "shows the deployment", %{
Expand All @@ -20,6 +22,38 @@ defmodule NervesHubWeb.Live.Deployments.ShowTest do
|> visit("/org/#{org.name}/#{product.name}/deployments/#{deployment.name}")
|> assert_has("h1", text: deployment.name)
|> assert_has("p.deployment-state", text: "Off")
|> assert_has("div#device-count p", text: "0")
|> then(fn conn ->
for tag <- deployment.conditions["tags"] do
assert_has(conn, "span", text: tag)
end

conn
end)
end

test "shows the deployment with device count", %{
conn: conn,
user: user,
org: org,
org_key: org_key,
device: device,
tmp_dir: tmp_dir
} do
product = Fixtures.product_fixture(user, org)
firmware = Fixtures.firmware_fixture(org_key, product, %{dir: tmp_dir})
deployment = Fixtures.deployment_fixture(org, firmware)
%Device{} = Devices.update_deployment(device, deployment)

# deleted devices shouldn't be included in the count
device_2 = Fixtures.device_fixture(org, product, firmware, %{deleted_at: DateTime.utc_now()})
%Device{} = Devices.update_deployment(device_2, deployment)

conn
|> visit("/org/#{org.name}/#{product.name}/deployments/#{deployment.name}")
|> assert_has("h1", text: deployment.name)
|> assert_has("p.deployment-state", text: "Off")
|> assert_has("div#device-count p", text: "1")
|> then(fn conn ->
for tag <- deployment.conditions["tags"] do
assert_has(conn, "span", text: tag)
Expand Down

0 comments on commit 933174c

Please sign in to comment.