-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into open-telemetry-tracing
- Loading branch information
Showing
12 changed files
with
260 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
defmodule NervesHub.Devices.Alarms do | ||
import Ecto.Query | ||
alias NervesHub.Repo | ||
alias NervesHub.Devices.Device | ||
alias NervesHub.Devices.DeviceHealth | ||
|
||
@doc """ | ||
Selects device id:s for devices that has alarm(s) in it's latest health record. | ||
Used when filtering devices. | ||
""" | ||
def query_devices_with_alarms() do | ||
(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 | ||
|
||
@doc """ | ||
Selects device id:s for devices that has provided alarm in it's latest health record. | ||
Used when filtering devices. | ||
""" | ||
def query_devices_with_alarm(alarm) do | ||
(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) | ||
end | ||
|
||
@doc """ | ||
Creates a list with all current alarm types for a product. | ||
""" | ||
def get_current_alarm_types(product_id) do | ||
query_current_alarms(product_id) | ||
|> Repo.all() | ||
|> Enum.map(fn %{data: data} -> | ||
Map.keys(data["alarms"]) | ||
end) | ||
|> List.flatten() | ||
|> Enum.uniq() | ||
|> Enum.map(&String.trim_leading(&1, "Elixir.")) | ||
end | ||
|
||
@doc """ | ||
Counts number of devices currently alarming, within a product. | ||
""" | ||
def current_alarms_count(product_id) do | ||
product_id | ||
|> query_current_alarms() | ||
|> select([a], count(a)) | ||
|> Repo.one!() | ||
end | ||
|
||
@doc """ | ||
Selects latest health per device if alarms is populated and device belongs to product. | ||
""" | ||
def query_current_alarms(product_id) do | ||
(lr in subquery(latest_row_query())) | ||
|> from() | ||
|> where([lr], lr.rn == 1) | ||
|> where([lr], fragment("?->'alarms' != '{}'", lr.data)) | ||
|> where([lr], lr.device_id in subquery(device_product_query(product_id))) | ||
end | ||
|
||
defp latest_row_query() do | ||
DeviceHealth | ||
|> select([dh], %{ | ||
device_id: dh.device_id, | ||
data: dh.data, | ||
inserted_at: dh.inserted_at, | ||
rn: row_number() |> over(partition_by: dh.device_id, order_by: [desc: dh.inserted_at]) | ||
}) | ||
end | ||
|
||
defp device_product_query(product_id) do | ||
Device | ||
|> select([:id]) | ||
|> where(product_id: ^product_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.