Skip to content

Commit

Permalink
Batch insert device metrics
Browse files Browse the repository at this point in the history
This decreases the event processing time in half, and reduces the number of Ecto queries, including no longer needing to use a transaction.
  • Loading branch information
joshk committed Oct 26, 2024
1 parent 3aab023 commit 5d44109
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
9 changes: 5 additions & 4 deletions lib/nerves_hub/devices/metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,12 @@ defmodule NervesHub.Devices.Metrics do
Saves map of metrics.
"""
def save_metrics(device_id, metrics) do
Repo.transaction(fn ->
Enum.map(metrics, fn {key, val} ->
save_metric(%{device_id: device_id, key: key, value: val})
end)
entries = Enum.map(metrics, fn {key, val} ->
DeviceMetric.save(%{device_id: device_id, key: key, value: val}).changes
|> Map.merge(%{inserted_at: {:placeholder, :now}})
end)

Repo.insert_all(DeviceMetric, entries, placeholders: %{now: DateTime.utc_now()})
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/channels/device_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ defmodule NervesHubWeb.DeviceChannel do

with {:health_report, {:ok, _}} <-
{:health_report, Devices.save_device_health(device_health)},
{:metrics_report, {:ok, _}} <-
{:metrics_report, {_, _}} <-
{:metrics_report, Metrics.save_metrics(socket.assigns.device.id, metrics)} do
device_internal_broadcast!(socket.assigns.device, "health_check_report", %{})
else
Expand Down
3 changes: 1 addition & 2 deletions test/nerves_hub/device_metrics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ defmodule NervesHub.DeviceMetricsTest do
"used_percent" => 2
}

assert {:ok, result} = Metrics.save_metrics(device.id, metrics)
assert length(result) == map_size(metrics)
assert {7, nil} = Metrics.save_metrics(device.id, metrics)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/nerves_hub_web/live/devices/health_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ defmodule NervesHubWeb.Devices.HealthTest do
"used_percent" => 2
}

assert {:ok, _} = Metrics.save_metrics(device.id, metrics)
assert {7, nil} = Metrics.save_metrics(device.id, metrics)

conn
|> visit("/org/#{org.name}/#{product.name}/devices/#{device.identifier}/health")
Expand Down
4 changes: 2 additions & 2 deletions test/nerves_hub_web/live/devices/show_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ defmodule NervesHubWeb.Live.Devices.ShowTest do
"used_percent" => 60
}

assert {:ok, _} = Metrics.save_metrics(device.id, metrics)
assert {7, nil} = Metrics.save_metrics(device.id, metrics)

conn
|> visit("/org/#{org.name}/#{product.name}/devices/#{device.identifier}")
Expand Down Expand Up @@ -278,7 +278,7 @@ defmodule NervesHubWeb.Live.Devices.ShowTest do
"used_percent" => 60
}

assert {:ok, _} = Metrics.save_metrics(device.id, metrics)
assert {6, nil} = Metrics.save_metrics(device.id, metrics)

conn
|> visit("/org/#{org.name}/#{product.name}/devices/#{device.identifier}")
Expand Down

0 comments on commit 5d44109

Please sign in to comment.