Skip to content

Commit c190512

Browse files
committed
Wire up the "view devices" and "view firmware" links
1 parent 8d2fa03 commit c190512

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

@@ -58,6 +59,25 @@ defmodule NervesHub.Devices.Filtering do
5859
)
5960
end
6061

62+
def filter(query, _filters, :filter, "not_recently_seen") do
63+
query
64+
|> where([latest_connection: lc], lc.status == :disconnected)
65+
|> where([latest_connection: lc], lc.disconnected_at < ago(24, "hour"))
66+
end
67+
68+
def filter(query, _filters, :filter, "unstable_connections") do
69+
unstable_connections =
70+
Device
71+
|> select([d], %{id: d.id, count: count()})
72+
|> join(:left, [d], dc in assoc(d, :device_connections))
73+
|> where([_, dc], dc.established_at > ago(24, "hour"))
74+
|> group_by([d], d.id)
75+
76+
join(query, :inner, [d], uc in subquery(unstable_connections),
77+
on: d.id == uc.id and uc.count > 5
78+
)
79+
end
80+
6181
def filter(query, _filters, :firmware_version, value) do
6282
where(query, [d], d.firmware_metadata["version"] == ^value)
6383
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
@@ -28,6 +28,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
2828
@default_filters %{
2929
connection: "",
3030
connection_type: "",
31+
filter: "",
3132
firmware_version: "",
3233
platform: "",
3334
healthy: "",
@@ -48,6 +49,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
4849
@filter_types %{
4950
connection: :string,
5051
connection_type: :string,
52+
filter: :string,
5153
firmware_version: :string,
5254
platform: :string,
5355
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
@@ -209,7 +209,8 @@ defmodule NervesHubWeb.Live.Firmware do
209209
page: pagination_opts["page_number"],
210210
page_size: pagination_opts["page_size"],
211211
sort: pagination_opts["sort"] || "inserted_at",
212-
sort_direction: pagination_opts["sort_direction"]
212+
sort_direction: pagination_opts["sort_direction"],
213+
filter: pagination_opts["filter"]
213214
}
214215

215216
{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)