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 4 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
32 changes: 32 additions & 0 deletions lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 @@ -244,10 +245,41 @@ defmodule NervesHub.Devices do
else
query
end

{:metrics_value, _value} ->
filter_on_metric(query, filters)

{_, _} ->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this (realized now it could be just a _ ..) instead of cases for {:metrics_key, _} and {:metrics_operator, _} since they depend on :metrics_value to execute anything. Any thoughts on that @joshk ?

query
end
end)
end

defp filter_on_metric(
query,
%{metrics_key: key, metrics_operator: operator, metrics_value: value}
)
when key != "" do
{value_as_float, _} = Float.parse(value)

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_as_float, operator)
end

defp filter_on_metric(query, _), do: query

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)

def get_device_count_by_org_id(org_id) do
q =
from(
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 @@ -6,6 +6,7 @@ defmodule NervesHub.Devices.Device do
alias NervesHub.Accounts.Org
alias NervesHub.Devices.DeviceCertificate
alias NervesHub.Devices.DeviceConnection
alias NervesHub.Devices.DeviceMetric
alias NervesHub.Deployments.Deployment
alias NervesHub.Firmwares.FirmwareMetadata
alias NervesHub.Products.Product
Expand Down Expand Up @@ -40,6 +41,7 @@ defmodule NervesHub.Devices.Device do
embeds_one(:firmware_metadata, FirmwareMetadata, on_replace: :update)
has_many(:device_certificates, DeviceCertificate, on_delete: :delete_all)
has_many(:device_connections, DeviceConnection, on_delete: :delete_all)
has_many(:device_metrics, DeviceMetric, on_delete: :delete_all)

field(:identifier, :string)
field(:description, :string)
Expand Down
16 changes: 13 additions & 3 deletions lib/nerves_hub_web/live/devices/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
alias NervesHub.AuditLogs
alias NervesHub.Devices
alias NervesHub.Devices.Alarms
alias NervesHub.Devices.Metrics
alias NervesHub.Firmwares
alias NervesHub.Products.Product
alias NervesHub.Tracker
Expand All @@ -30,7 +31,10 @@ defmodule NervesHubWeb.Live.Devices.Index do
updates: "",
has_no_tags: false,
alarm_status: "",
alarm: ""
alarm: "",
metrics_key: "",
metrics_operator: "gt",
metrics_value: ""
}

@filter_types %{
Expand All @@ -44,7 +48,10 @@ defmodule NervesHubWeb.Live.Devices.Index do
updates: :string,
has_no_tags: :boolean,
alarm_status: :string,
alarm: :string
alarm: :string,
metrics_key: :string,
metrics_operator: :string,
metrics_value: :string
}

@default_page 1
Expand Down Expand Up @@ -83,12 +90,15 @@ defmodule NervesHubWeb.Live.Devices.Index do
|> assign(:device_tags, "")
|> assign(:total_entries, 0)
|> assign(:current_alarms, Alarms.get_current_alarm_types(product.id))
|> assign(:metrics_keys, Metrics.default_metric_types())
|> subscribe_and_refresh_device_list_timer()
|> ok()
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))

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

socket
Expand Down
24 changes: 24 additions & 0 deletions lib/nerves_hub_web/live/devices/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@
<div class="select-icon"></div>
</div>
</div>
<div class="form-group">
<label for="metrics">Metrics</label>
<div class="pos-rel">
<select name="metrics_key" id="metrics_key" class="form-control">
<option {selected?(@current_filters, :metrics_key, "")} value="">All</option>
<%= for key <- @metrics_keys do %>
<option {selected?(@current_filters, :metrics_key, Atom.to_string(key))}><%= Atom.to_string(key) %></option>
<% end %>
</select>
<div class="select-icon"></div>
</div>
</div>
<div :if={@current_filters.metrics_key != ""} class="form-group">
<label for="metrics_operator">Operator</label>
<select name="metrics_operator" id="metrics_operator" class="form-control">
<option {selected?(@current_filters, :metrics_operator, "gt")} value="gt">Greater Than</option>
<option {selected?(@current_filters, :metrics_operator, "lt")} value="lt">Less than</option>
</select>
<div class="select-icon"></div>
</div>
<div :if={@current_filters.metrics_key != ""} class="form-group">
<label for="metrics_value">Value</label>
<input type="number" name="metrics_value" id="metrics_value" class="form-control" value={@current_filters[:metrics_value]} phx-debounce="100" />
</div>
</form>
<button class="btn btn-secondary" type="button" phx-click="reset-filters">Reset Filters</button>
</div>
Expand Down
39 changes: 39 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,44 @@ 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})

change =
render_change(view, "update-filters", %{
"metrics_key" => "cpu_temp",
"metrics_operator" => "gt",
"metrics_value" => "37"
})

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

change2 =
render_change(view, "update-filters", %{
"metrics_key" => "cpu_temp",
"metrics_operator" => "lt",
"metrics_value" => "37"
})

# Should not show any device since the query is for values less than 37
refute change2 =~ device2.identifier
refute change2 =~ 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