Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deployment filter for devices #1914

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/nerves_hub/devices/filtering.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ defmodule NervesHub.Devices.Filtering do
where(query, [d], ilike(d.identifier, ^"%#{value}%"))
end

def filter(query, _filters, :deployment_id, nil), do: query

def filter(query, _filters, :deployment_id, value) do
where(query, [d], d.deployment_id == ^value)
end

def filter(query, _filters, :tag, value) do
build_tag_filter(query, value)
end
Expand Down
9 changes: 9 additions & 0 deletions lib/nerves_hub_web/live/devices/index-new.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,15 @@
<option {selected?(@current_filters, :connection_type, "wifi")} value="wifi">WiFi</option>
</select>
</div>
<div class="mt-6">
<label class="sidebar-label" for="deployment_id">Deployment</label>
<select class="sidebar-select" name="deployment_id" id="deployment_id">
<option {selected?(@current_filters, :deployment_id, "")} value="">All</option>
<%= for deployment <- @deployments do %>
<option {selected?(@current_filters, :deployment_id, deployment.id)} value={deployment.id}>{deployment.name}</option>
<% end %>
</select>
</div>
<div class="mt-6">
<label class="sidebar-label" for="input_health">Firmware Updates</label>
<select class="sidebar-select" name="updates" id="input_health">
Expand Down
6 changes: 4 additions & 2 deletions lib/nerves_hub_web/live/devices/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ defmodule NervesHubWeb.Live.Devices.Index do
alarm: "",
metrics_key: "",
metrics_operator: "gt",
metrics_value: ""
metrics_value: "",
deployment_id: nil
}

@filter_types %{
Expand All @@ -58,7 +59,8 @@ defmodule NervesHubWeb.Live.Devices.Index do
alarm: :string,
metrics_key: :string,
metrics_operator: :string,
metrics_value: :string
metrics_value: :string,
deployment_id: :integer
}

@default_page 1
Expand Down
9 changes: 9 additions & 0 deletions lib/nerves_hub_web/live/devices/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@
<div class="select-icon"></div>
</div>
</div>
<div class="form-group">
<label for="deployment_id">Deployment</label>
<select name="deployment_id" id="deployment_id" class="form-control">
<option {selected?(@current_filters, :deployment_id, "")} value="">All</option>
<%= for deployment <- @deployments do %>
<option {selected?(@current_filters, :deployment_id, deployment.id)} value={deployment.id}>{deployment.name}</option>
<% end %>
</select>
</div>
<div class="form-group">
<label for="input_health">Firmware Updates</label>
<div class="pos-rel">
Expand Down
16 changes: 16 additions & 0 deletions test/nerves_hub_web/live/devices/index_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ defmodule NervesHubWeb.Live.Devices.IndexTest do
assert change =~ "1 devices found"
end

test "filters devices by deployment_id", %{conn: conn, fixture: fixture} do
%{device: device, firmware: firmware, org: org, product: product, deployment: deployment} =
fixture

device2 = Fixtures.device_fixture(org, product, firmware)

Repo.update!(Ecto.Changeset.change(device, deployment_id: deployment.id))

{:ok, view, _html} = live(conn, device_index_path(fixture))

change = render_change(view, "update-filters", %{"deployment_id" => deployment.id})
assert change =~ device.identifier
refute change =~ device2.identifier
assert change =~ "1 devices found"
end

test "select device", %{conn: conn, fixture: fixture} do
%{device: _device, firmware: firmware, org: org, product: product} = fixture

Expand Down
Loading