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 metrics filtering to devices query #1598

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule NervesHub.Devices do
alias NervesHub.Devices.Device
alias NervesHub.Devices.DeviceCertificate
alias NervesHub.Devices.DeviceHealth
alias NervesHub.Devices.DeviceMetric
alias NervesHub.Devices.SharedSecretAuth
alias NervesHub.Devices.InflightUpdate
alias NervesHub.Devices.UpdatePayload
Expand Down Expand Up @@ -158,6 +159,23 @@ defmodule NervesHub.Devices do

defp sort_devices(sort), do: sort

def filter_on_metric(query, key, value, operator) do
query
|> join(:inner, [d], m in DeviceMetric, on: d.id == m.device_id)
|> where([_, m], m.inserted_at == subquery(latest_metric_for_key(key)))
|> where([d, m], m.key == ^key)
|> gt_or_lt(value, operator)
end

defp latest_metric_for_key(key) do
DeviceMetric
|> select([dm], max(dm.inserted_at))
|> where([dm], dm.key == ^key)
end

defp gt_or_lt(query, value, :gt), do: where(query, [_, dm], dm.value > ^value)
defp gt_or_lt(query, value, :lt), do: where(query, [_, dm], dm.value < ^value)

defp filtering(query, filters) do
Enum.reduce(filters, query, fn {key, value}, query ->
case {key, value} do
Expand All @@ -173,6 +191,12 @@ defmodule NervesHub.Devices do
{:alarm, value} ->
where(query, [d], d.id in subquery(Alarms.query_devices_with_alarm(value)))

{:metrics, %{key: key, value: value, operator: operator}} ->
filter_on_metric(query, key, value, operator)

{:metrics, _} ->
query

{:connection, _value} ->
where(query, [d], d.connection_status == ^String.to_atom(value))

Expand Down
2 changes: 2 additions & 0 deletions lib/nerves_hub/devices/device.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule NervesHub.Devices.Device do

alias NervesHub.Accounts.Org
alias NervesHub.Devices.DeviceCertificate
alias NervesHub.Devices.DeviceMetric
alias NervesHub.Deployments.Deployment
alias NervesHub.Firmwares.FirmwareMetadata
alias NervesHub.Products.Product
Expand Down Expand Up @@ -36,6 +37,7 @@ defmodule NervesHub.Devices.Device do
belongs_to(:deployment, Deployment)
embeds_one(:firmware_metadata, FirmwareMetadata, on_replace: :update)
has_many(:device_certificates, DeviceCertificate, on_delete: :delete_all)
has_many(:device_metrics, DeviceMetric, on_delete: :delete_all)

field(:identifier, :string)
field(:description, :string)
Expand Down
28 changes: 25 additions & 3 deletions lib/nerves_hub_web/live/devices/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ defmodule NervesHubWeb.Live.Devices.Index do
updates: "",
has_no_tags: false,
alarm_status: "",
alarm: ""
alarm: "",
metrics: %{}
}

@filter_types %{
Expand All @@ -44,7 +45,8 @@ defmodule NervesHubWeb.Live.Devices.Index do
updates: :string,
has_no_tags: :boolean,
alarm_status: :string,
alarm: :string
alarm: :string,
metrics: :map
}

@default_page 1
Expand Down Expand Up @@ -88,7 +90,13 @@ defmodule NervesHubWeb.Live.Devices.Index do
end

def handle_params(unsigned_params, _uri, socket) do
filters = Map.merge(@default_filters, filter_changes(unsigned_params))
filters =
Map.merge(@default_filters, filter_changes(unsigned_params))
# Until UI selectors for metrics are implemented,
# params can be added to the URL and be merged with filters.
# Example: ?metric=cpu_temp&metric_operator=gt&metric_value=37.0
|> filter_on_metrics_if_provided(unsigned_params)

pagination_opts = Map.merge(socket.assigns.paginate_opts, pagination_changes(unsigned_params))

socket
Expand All @@ -102,6 +110,20 @@ defmodule NervesHubWeb.Live.Devices.Index do
|> noreply()
end

defp filter_on_metrics_if_provided(filters, %{
"metric" => metric_type,
"metric_value" => metric_value,
"metric_operator" => operator
}) do
Map.put(filters, :metrics, %{
key: metric_type,
value: String.to_float(metric_value),
operator: String.to_existing_atom(operator)
})
end

defp filter_on_metrics_if_provided(filters, _), do: filters

defp self_path(socket, extra) do
params = Enum.into(stringify_keys(extra), socket.assigns.params)
pagination = pagination_changes(params)
Expand Down
35 changes: 35 additions & 0 deletions test/nerves_hub_web/live/devices/index_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule NervesHubWeb.Live.Devices.IndexTest do
use NervesHubWeb.ConnCase.Browser, async: false

alias NervesHub.Devices
alias NervesHub.Fixtures
alias NervesHubWeb.Endpoint

Expand Down Expand Up @@ -85,6 +86,40 @@ defmodule NervesHubWeb.Live.Devices.IndexTest do
device2.identifier
end

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

device2 = Fixtures.device_fixture(org, product, firmware, %{})
{:ok, _view, html} = live(conn, device_index_path(fixture))
assert html =~ device.identifier
assert html =~ device2.identifier

# Add metrics for device2, sleep between to secure order.
Devices.Metrics.save_metric(%{device_id: device2.id, key: "cpu_temp", value: 36})
:timer.sleep(100)
Devices.Metrics.save_metric(%{device_id: device2.id, key: "cpu_temp", value: 42})
:timer.sleep(100)
Devices.Metrics.save_metric(%{device_id: device2.id, key: "load_1min", value: 3})

greater_than_filter =
device_index_path(fixture) <> "?metric=cpu_temp&metric_operator=gt&metric_value=37.0"

{:ok, _view, html} = live(conn, greater_than_filter)

# Show only show device2, which has a value greater than 37 on most recent cpu_temp metric.
assert html =~ device2.identifier
refute html =~ device.identifier

less_than_filter =
device_index_path(fixture) <> "?metric=cpu_temp&metric_operator=lt&metric_value=37.0"

{:ok, _view, html} = live(conn, less_than_filter)

# Should not show any device since the query is for values less than 37
refute html =~ device2.identifier
refute html =~ device.identifier
end

test "filters devices by several tags", %{conn: conn, fixture: fixture} do
%{device: _device, firmware: firmware, org: org, product: product} = fixture

Expand Down
Loading