Skip to content

Commit

Permalink
Make alarms human friendly
Browse files Browse the repository at this point in the history
Use pipes only in alarm queries
  • Loading branch information
elinol committed Nov 4, 2024
1 parent 085201d commit f535dea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion assets/css/_form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ html {
.filter-form {
&.device-filters {
display: grid;
grid-template-columns: 3fr 3fr 3fr 2fr 2fr;
grid-template-columns: 3fr 3fr 3fr 2fr 3fr;
grid-column-gap: 1rem;

@media (max-width: 860px) {
Expand Down
35 changes: 21 additions & 14 deletions lib/nerves_hub/devices/alarms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ defmodule NervesHub.Devices.Alarms do
Used when filtering devices.
"""
def query_devices_with_alarms() do
from(
lr in subquery(latest_row_query()),
where: lr.rn == 1,
where: fragment("?->'alarms' != '{}'", lr.data)
)
(lr in subquery(latest_row_query()))
|> from()
|> where([lr], lr.rn == 1)
|> where([lr], fragment("?->'alarms' != '{}'", lr.data))
|> join(:inner, [lr], d in Device, on: lr.device_id == d.id)
|> select([lr, o], o.id)
end
Expand All @@ -23,15 +22,16 @@ defmodule NervesHub.Devices.Alarms do
Used when filtering devices.
"""
def query_devices_with_alarm(alarm) do
from(
lr in subquery(latest_row_query()),
where: lr.rn == 1,
where:
fragment(
"EXISTS (SELECT 1 FROM jsonb_each_text(?) WHERE value ILIKE ?)",
lr.data,
^"%#{alarm}%"
)
(lr in subquery(latest_row_query()))
|> from()
|> where([lr], lr.rn == 1)
|> where(
[lr],
fragment(
"EXISTS (SELECT 1 FROM jsonb_each_text(?) WHERE value ILIKE ?)",
lr.data,
^"%#{alarm}%"
)
)
|> join(:inner, [lr], d in Device, on: lr.device_id == d.id)
|> select([lr, o], o.id)
Expand All @@ -48,6 +48,7 @@ defmodule NervesHub.Devices.Alarms do
end)
|> List.flatten()
|> Enum.uniq()
|> Enum.map(&prettier_alarm/1)
end

@doc """
Expand Down Expand Up @@ -88,4 +89,10 @@ defmodule NervesHub.Devices.Alarms do
|> select([:id])
|> where(product_id: ^product_id)
end

defp prettier_alarm(alarm) do
alarm
|> String.split(".")
|> List.last()
end
end

0 comments on commit f535dea

Please sign in to comment.