Skip to content

Commit 9a1cf02

Browse files
committed
Wire up the "view devices" and "view firmware" links
1 parent 89fe9e8 commit 9a1cf02

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

lib/nerves_hub/devices/filtering.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule NervesHub.Devices.Filtering do
66
import Ecto.Query
77

88
alias NervesHub.Devices.Alarms
9+
alias NervesHub.Devices.Device
910
alias NervesHub.Devices.DeviceMetric
1011
alias NervesHub.Types.Tag
1112

@@ -62,6 +63,25 @@ defmodule NervesHub.Devices.Filtering do
6263
)
6364
end
6465

66+
def filter(query, _filters, :filter, "not_recently_seen") do
67+
query
68+
|> where([latest_connection: lc], lc.status == :disconnected)
69+
|> where([latest_connection: lc], lc.disconnected_at < ago(24, "hour"))
70+
end
71+
72+
def filter(query, _filters, :filter, "unstable_connections") do
73+
unstable_connections =
74+
Device
75+
|> select([d], %{id: d.id, count: count()})
76+
|> join(:left, [d], dc in assoc(d, :device_connections))
77+
|> where([_, dc], dc.established_at > ago(24, "hour"))
78+
|> group_by([d], d.id)
79+
80+
join(query, :inner, [d], uc in subquery(unstable_connections),
81+
on: d.id == uc.id and uc.count > 5
82+
)
83+
end
84+
6585
def filter(query, _filters, :firmware_version, value) do
6686
where(query, [d], d.firmware_metadata["version"] == ^value)
6787
end

lib/nerves_hub/firmwares.ex

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,25 @@ defmodule NervesHub.Firmwares do
116116
|> group_by([d], fragment("? ->> 'uuid'", d.firmware_metadata))
117117

118118
Firmware
119-
|> join(:left, [f], d in subquery(subquery), on: d.firmware_uuid == f.uuid)
119+
|> join(:left, [f], d in subquery(subquery),
120+
as: :install_count,
121+
on: d.firmware_uuid == f.uuid
122+
)
120123
|> where([f], f.product_id == ^product.id)
124+
|> filter_selection(opts[:filter])
121125
|> sort_firmware(sort_opts)
122-
|> select_merge([_f, d], %{install_count: d.install_count})
126+
|> select_merge([_f, install_count: ic], %{install_count: ic.install_count})
123127
|> Flop.run(flop)
124128
end
125129

130+
defp filter_selection(query, "active_firmware") do
131+
where(query, [_f, install_count: ic], ic.install_count > 0)
132+
end
133+
134+
defp filter_selection(query, _filter), do: query
135+
126136
defp sort_firmware(query, {direction, :install_count}) do
127-
order_by(query, [_f, d], {^direction, d.install_count})
137+
order_by(query, [_f, install_count: ic], {^direction, ic.install_count})
128138
end
129139

130140
defp sort_firmware(query, sort), do: order_by(query, ^sort)

lib/nerves_hub_web/live/devices/index.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
3030
@default_filters %{
3131
connection: "",
3232
connection_type: "",
33+
filter: "",
3334
firmware_version: "",
3435
platform: "",
3536
healthy: "",
@@ -51,6 +52,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
5152
@filter_types %{
5253
connection: :string,
5354
connection_type: :string,
55+
filter: :string,
5456
firmware_version: :string,
5557
platform: :string,
5658
healthy: :string,

lib/nerves_hub_web/live/firmware.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule NervesHubWeb.Live.Firmware do
99

1010
embed_templates("firmware_templates/*")
1111

12-
@pagination_opts ["page_number", "page_size", "sort", "sort_direction"]
12+
@pagination_opts ["page_number", "page_size", "sort", "sort_direction", "filter"]
1313

1414
@impl Phoenix.LiveView
1515
def mount(_params, _session, socket) do
@@ -203,7 +203,8 @@ defmodule NervesHubWeb.Live.Firmware do
203203
page: pagination_opts["page_number"],
204204
page_size: pagination_opts["page_size"],
205205
sort: pagination_opts["sort"] || "inserted_at",
206-
sort_direction: pagination_opts["sort_direction"]
206+
sort_direction: pagination_opts["sort_direction"],
207+
filter: pagination_opts["filter"]
207208
}
208209

209210
{entries, pager_meta} = Firmwares.filter(product, opts)

lib/nerves_hub_web/live/product/insights.html.heex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
<div class="px-4 py-3 text-xs border-t border-zinc-700">
8383
<.link
8484
class="text-xs font-normal text-zinc-400 underline underline-offset-4 decoration-dotted hover:text-neutral-50"
85-
href="https://github.com/nerves-hub/nerves_hub_link?tab=readme-ov-file#configure-health"
85+
navigate={~p"/org/#{@org.name}/#{@product.name}/devices?filter=not_recently_seen"}
8686
>
8787
View Devices
8888
</.link>
@@ -117,7 +117,7 @@
117117
<div class="px-4 py-3 text-xs border-t border-zinc-700">
118118
<.link
119119
class="text-xs font-normal text-zinc-400 underline underline-offset-4 decoration-dotted hover:text-neutral-50"
120-
href="https://github.com/nerves-hub/nerves_hub_link?tab=readme-ov-file#configure-health"
120+
navigate={~p"/org/#{@org.name}/#{@product.name}/devices?filter=unstable_connections"}
121121
>
122122
View Devices
123123
</.link>
@@ -161,7 +161,7 @@
161161
<div class="px-4 py-3 text-xs border-t border-zinc-700">
162162
<.link
163163
class="text-xs font-normal text-zinc-400 underline underline-offset-4 decoration-dotted hover:text-neutral-50"
164-
href="https://github.com/nerves-hub/nerves_hub_link?tab=readme-ov-file#configure-health"
164+
navigate={~p"/org/#{@org.name}/#{@product.name}/firmware?filter=active_firmware"}
165165
>
166166
View Firmware
167167
</.link>

0 commit comments

Comments
 (0)