Skip to content

Commit

Permalink
Various fixes found by Dialyzer (#1825)
Browse files Browse the repository at this point in the history
Mostly specs, some aliases and type defs, and some return types that
don't match. And one function no longer used.
  • Loading branch information
joshk authored Jan 24, 2025
1 parent 2cf5933 commit bbb15ce
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 76 deletions.
7 changes: 3 additions & 4 deletions lib/nerves_hub/archives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ defmodule NervesHub.Archives do
alias NervesHub.Repo
alias NervesHub.Workers.DeleteArchive

@spec filter(Product.t(), map()) ::
{:ok, {[Product.t()], Flop.Meta.t()}} | {:error, Flop.Meta.t()}
def filter(product_id, opts \\ %{}) do
@spec filter(Product.t(), map()) :: {[Product.t()], Flop.Meta.t()}
def filter(product, opts \\ %{}) do
opts = Map.reject(opts, fn {_key, val} -> is_nil(val) end)

sort = Map.get(opts, :sort, "inserted_at")
Expand All @@ -29,7 +28,7 @@ defmodule NervesHub.Archives do
}

Archive
|> where([f], f.product_id == ^product_id)
|> where([f], f.product_id == ^product.id)
|> order_by(^sort_opts)
|> Flop.run(flop)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule NervesHub.AuditLogs.DeploymentTemplates do
alias NervesHub.AuditLogs
alias NervesHub.AuditLogs.AuditLog
alias NervesHub.Deployments.Deployment
alias NervesHub.Devices.Device

@spec audit_deployment_created(User.t(), Deployment.t()) :: AuditLog.t()
def audit_deployment_created(user, deployment) do
Expand Down
3 changes: 2 additions & 1 deletion lib/nerves_hub/audit_logs/templates/device_templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ defmodule NervesHub.AuditLogs.DeviceTemplates do
description =
"Device #{device.identifier} could not get extensions: Unsupported API version."

AuditLogs.audit!(device, device, description)
Logger.info("[DeviceChannel] #{description}")

AuditLogs.audit!(device, device, description)
end

## Firmware and upgrades
Expand Down
7 changes: 3 additions & 4 deletions lib/nerves_hub/deployments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ defmodule NervesHub.Deployments do
Repo.all(Deployment)
end

@spec filter(Product.t(), map()) ::
{:ok, {[Product.t()], Flop.Meta.t()}} | {:error, Flop.Meta.t()}
def filter(product_id, opts \\ %{}) do
@spec filter(Product.t(), map()) :: {[Product.t()], Flop.Meta.t()}
def filter(product, opts \\ %{}) do
opts = Map.reject(opts, fn {_key, val} -> is_nil(val) end)

sort = Map.get(opts, :sort, "name")
Expand All @@ -48,7 +47,7 @@ defmodule NervesHub.Deployments do
Deployment
|> join(:left, [d], dev in subquery(subquery), on: dev.deployment_id == d.id)
|> join(:left, [d], f in assoc(d, :firmware))
|> where([d], d.product_id == ^product_id)
|> where([d], d.product_id == ^product.id)
|> sort_deployments(sort_opts)
|> preload([_d, _dev, f], firmware: f)
|> select_merge([_f, dev], %{device_count: dev.device_count})
Expand Down
49 changes: 3 additions & 46 deletions lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,22 @@ defmodule NervesHub.Devices do
|> Repo.one!()
end

@spec filter(integer(), map()) :: %{
@spec filter(%Product{}, map()) :: %{
entries: list(Device.t()),
current_page: non_neg_integer(),
page_size: non_neg_integer(),
total_pages: non_neg_integer(),
total_count: non_neg_integer()
}
def filter(product_id, opts) do
def filter(product, opts) do
pagination = Map.get(opts, :pagination, %{})
sorting = Map.get(opts, :sort, {:asc, :identifier})
filters = Map.get(opts, :filters, %{})

flop = %Flop{page: pagination.page, page_size: pagination.page_size}

Device
|> where([d], d.product_id == ^product_id)
|> where([d], d.product_id == ^product.id)
|> Repo.exclude_deleted()
|> join(:left, [d], dc in assoc(d, :latest_connection), as: :latest_connection)
|> preload([latest_connection: lc], latest_connection: lc)
Expand Down Expand Up @@ -149,49 +149,6 @@ defmodule NervesHub.Devices do
|> Repo.all()
end

def get_health_by_org_id_and_product_id(org_id, product_id, opts) do
query =
from(
d in Device,
as: :device,
join: dh in DeviceHealth,
as: :device_health,
on: dh.device_id == d.id,
select: [dh.device_id, dh.data, d.deleted_at],
distinct: dh.device_id,
order_by: [desc: dh.inserted_at],
where: d.org_id == ^org_id,
where: d.product_id == ^product_id
)

filters = Map.get(opts, :filters, %{})

query
|> Repo.exclude_deleted()
|> Filtering.build_filters(filters)
|> Repo.all()
|> Enum.reduce(%{max_cpu: 0, max_memory_percent: 0, max_load_15: 0}, fn health, acc ->
case Enum.at(health, 1) do
%{
"metrics" => %{
"cpu_temp" => cpu_temp,
"used_percent" => memory_percent,
"load_15min" => load_15_min
}
} ->
%{
acc
| max_cpu: max(cpu_temp, acc.max_cpu),
max_memory_percent: max(memory_percent, acc.max_memory_percent),
max_load_15: max(load_15_min, acc.max_load_15)
}

_ ->
acc
end
end)
end

defp sort_devices(query, {:asc, :connection_last_seen_at}) do
order_by(query, [latest_connection: latest_connection],
desc_nulls_last: latest_connection.last_seen_at
Expand Down
7 changes: 3 additions & 4 deletions lib/nerves_hub/firmwares.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ defmodule NervesHub.Firmwares do
|> Repo.all()
end

@spec filter(Product.t(), map()) ::
{:ok, {[Product.t()], Flop.Meta.t()}} | {:error, Flop.Meta.t()}
def filter(product_id, opts \\ %{}) do
@spec filter(Product.t(), map()) :: {[Product.t()], Flop.Meta.t()}
def filter(product, opts \\ %{}) do
opts = Map.reject(opts, fn {_key, val} -> is_nil(val) end)

sort = Map.get(opts, :sort, "inserted_at")
Expand All @@ -60,7 +59,7 @@ defmodule NervesHub.Firmwares do

Firmware
|> join(:left, [f], d in subquery(subquery), on: d.firmware_uuid == f.uuid)
|> where([f], f.product_id == ^product_id)
|> where([f], f.product_id == ^product.id)
|> sort_firmware(sort_opts)
|> select_merge([_f, d], %{install_count: d.install_count})
|> Flop.run(flop)
Expand Down
7 changes: 3 additions & 4 deletions lib/nerves_hub/scripts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ defmodule NervesHub.Scripts do

alias NervesHub.Repo

@spec filter(Product.t(), map()) ::
{:ok, {[Product.t()], Flop.Meta.t()}} | {:error, Flop.Meta.t()}
def filter(product_id, opts \\ %{}) do
@spec filter(Product.t(), map()) :: {[Product.t()], Flop.Meta.t()}
def filter(product, opts \\ %{}) do
opts = Map.reject(opts, fn {_key, val} -> is_nil(val) end)

sort = Map.get(opts, :sort, "name")
Expand All @@ -26,7 +25,7 @@ defmodule NervesHub.Scripts do
}

Script
|> where([f], f.product_id == ^product_id)
|> where([f], f.product_id == ^product.id)
|> order_by(^sort_opts)
|> Flop.run(flop)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/nerves_hub/scripts/script.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule NervesHub.Scripts.Script do

alias NervesHub.Products.Product

@type t :: %__MODULE__{}

schema "scripts" do
belongs_to(:product, Product)

Expand Down
16 changes: 8 additions & 8 deletions lib/nerves_hub_web/components/deployment_page/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ defmodule NervesHubWeb.Components.DeploymentPage.Settings do
<div class="text-sm mt-2">sec</div>
</div>
<div class="flex flex-col gap-1 text-xs text-zinc-400 pt-1">
Maximum number of device install failures from this deployment within X seconds before being marked unhealthy.
{help_message_for(:failure_rate)}
</div>
<NervesHubWeb.CoreComponents.error :for={msg <- Enum.map(@form[:failure_rate].errors ++ @form[:failure_rate_seconds].errors, &NervesHubWeb.CoreComponents.translate_error(&1))}>
<NervesHubWeb.CoreComponents.error :for={msg <- Enum.map(@form[:failure_rate_amount].errors ++ @form[:failure_rate_seconds].errors, &NervesHubWeb.CoreComponents.translate_error(&1))}>
{msg}
</NervesHubWeb.CoreComponents.error>
</div>
Expand Down Expand Up @@ -203,7 +203,7 @@ defmodule NervesHubWeb.Components.DeploymentPage.Settings do
<div class="text-sm mt-2">sec</div>
</div>
<div class="flex flex-col gap-1 text-xs text-zinc-400 pt-1">
Maximum number of device failures within X seconds a device can have for this deployment before being marked unhealthy
{help_message_for(:device_failure_rate)}
</div>
<.error :for={msg <- Enum.map(@form[:device_failure_rate_amount].errors ++ @form[:device_failure_rate_seconds].errors, &NervesHubWeb.CoreComponents.translate_error(&1))}>
{msg}
Expand Down Expand Up @@ -387,19 +387,19 @@ defmodule NervesHubWeb.Components.DeploymentPage.Settings do
defp help_message_for(field) do
case field do
:failure_threshold ->
"Maximum number of target devices from this deployment that can be in an unhealthy state before marking the deployment unhealthy"
"Maximum number of target devices from this deployment that can be in an unhealthy state before marking the deployment unhealthy."

:failure_rate ->
"Maximum number of device install failures from this deployment within X seconds before being marked unhealthy"
"Maximum number of device install failures from this deployment within X seconds before being marked unhealthy."

:device_failure_rate ->
"Maximum number of device failures within X seconds a device can have for this deployment before being marked unhealthy"
"Maximum number of device failures within X seconds a device can have for this deployment before being marked unhealthy."

:device_failure_threshold ->
"Maximum number of install attempts and/or failures a device can have for this deployment before being marked unhealthy"
"Maximum number of install attempts and/or failures a device can have for this deployment before being marked unhealthy."

:penalty_timeout_minutes ->
"Number of minutes a device is placed in the penalty box for reaching the failure rate and threshold"
"Number of minutes a device is placed in the penalty box for reaching the failure rate and threshold."
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/live/archives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ defmodule NervesHubWeb.Live.Archives do
sort_direction: pagination_opts["sort_direction"]
}

{entries, pager_meta} = Archives.filter(product.id, opts)
{entries, pager_meta} = Archives.filter(product, opts)

socket
|> assign(:current_sort, opts.sort)
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/live/deployments/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule NervesHubWeb.Live.Deployments.Index do
sort_direction: pagination_opts["sort_direction"]
}

{entries, pager_meta} = Deployments.filter(product.id, opts)
{entries, pager_meta} = Deployments.filter(product, opts)

socket
|> assign(:current_sort, opts.sort)
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/live/devices/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
filters: socket.assigns.current_filters
}

page = Devices.filter(product.id, opts)
page = Devices.filter(product, opts)

statuses =
Enum.into(page.entries, %{}, fn device ->
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/live/firmware.ex
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ defmodule NervesHubWeb.Live.Firmware do
sort_direction: pagination_opts["sort_direction"]
}

{entries, pager_meta} = Firmwares.filter(product.id, opts)
{entries, pager_meta} = Firmwares.filter(product, opts)

socket
|> assign(:current_sort, opts.sort)
Expand Down
2 changes: 1 addition & 1 deletion lib/nerves_hub_web/live/support_scripts/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ defmodule NervesHubWeb.Live.SupportScripts.Index do
sort_direction: pagination_opts["sort_direction"]
}

{entries, pager_meta} = Scripts.filter(product.id, opts)
{entries, pager_meta} = Scripts.filter(product, opts)

socket
|> assign(:current_sort, opts.sort)
Expand Down

0 comments on commit bbb15ce

Please sign in to comment.