refactor: adopt Towerops.Result helpers in 8 call sites

Use Result.map, Result.unwrap_or, and Result.ok? where they replace
verbose case statements. Skipped sites where return shapes (3-tuples,
:skipped atoms) prevent clean adoption.

- proto/wire.ex: skip_field varint and length-delimited use Result.map
- organizations.ex: list_organization_ids uses Result.unwrap_or([])
- security/four_oh_four_tracker.ex: count_404s uses Result.unwrap_or(0)
- billing/billing_notifier.ex: at-least-one-success check uses Result.ok?
- topology.ex: two success-counting sites use Result.ok?
- snmp/wireless_client_discovery.ex: walk_or_empty uses Result.unwrap_or(%{})
This commit is contained in:
Graham McIntire 2026-04-30 14:43:24 -05:00
parent 115efc0314
commit 6c90e8bd4f
6 changed files with 15 additions and 23 deletions

View file

@ -12,6 +12,7 @@ defmodule Towerops.Billing.BillingNotifier do
alias Towerops.Mailer
alias Towerops.Organizations
alias Towerops.Result
require Logger
@ -84,7 +85,7 @@ defmodule Towerops.Billing.BillingNotifier do
end)
# Return success if at least one email was sent
if Enum.any?(results, &match?({:ok, _}, &1)) do
if Enum.any?(results, &Result.ok?/1) do
{:ok, results}
else
{:error, :all_emails_failed}

View file

@ -18,6 +18,7 @@ defmodule Towerops.Organizations do
alias Towerops.Organizations.Policy
alias Towerops.Organizations.SubscriptionLimits
alias Towerops.Repo
alias Towerops.Result
alias Towerops.Sites.Site
## Organizations
@ -35,10 +36,7 @@ defmodule Towerops.Organizations do
|> Enum.to_list()
end
|> Repo.transaction()
|> case do
{:ok, ids} -> ids
{:error, _} -> []
end
|> Result.unwrap_or([])
end
@doc """

View file

@ -8,6 +8,8 @@ defmodule Towerops.Proto.Wire do
import Bitwise
alias Towerops.Result
# Wire type constants
@wire_varint 0
@wire_64bit 1
@ -179,10 +181,7 @@ defmodule Towerops.Proto.Wire do
"""
@spec skip_field(0..5, binary()) :: {:ok, binary()} | {:error, wire_error()}
def skip_field(@wire_varint, data) do
case decode_varint(data) do
{:ok, {_value, rest}} -> {:ok, rest}
{:error, e} -> {:error, e}
end
Result.map(decode_varint(data), fn {_value, rest} -> rest end)
end
def skip_field(@wire_64bit, data) when byte_size(data) >= 8 do
@ -193,10 +192,7 @@ defmodule Towerops.Proto.Wire do
def skip_field(@wire_64bit, _data), do: {:error, :unexpected_eof}
def skip_field(@wire_length_delimited, data) do
case decode_bytes(data) do
{:ok, {_field_data, rest}} -> {:ok, rest}
{:error, e} -> {:error, e}
end
Result.map(decode_bytes(data), fn {_field_data, rest} -> rest end)
end
def skip_field(@wire_32bit, data) when byte_size(data) >= 4 do

View file

@ -6,6 +6,7 @@ defmodule Towerops.Security.FourOhFourTracker do
When an IP hits 5+ unique 404s, a ban is created/escalated.
"""
alias Towerops.Result
alias Towerops.Security.BruteForce
require Logger
@ -59,10 +60,7 @@ defmodule Towerops.Security.FourOhFourTracker do
conn = Towerops.Redix
try do
case Redix.command(conn, ["SCARD", key]) do
{:ok, count} -> count
{:error, _} -> 0
end
Result.unwrap_or(Redix.command(conn, ["SCARD", key]), 0)
catch
:exit, _ -> 0
end

View file

@ -8,6 +8,7 @@ defmodule Towerops.Snmp.WirelessClientDiscovery do
- MikroTik RouterOS: mtxrWlRtabTable
"""
alias Towerops.Result
alias Towerops.Snmp.Client
alias Towerops.Snmp.WirelessClientDiscovery.Parser
@ -186,10 +187,7 @@ defmodule Towerops.Snmp.WirelessClientDiscovery do
# --- Helpers ---
defp walk_or_empty(client_opts, oid) do
case Client.walk(client_opts, oid) do
{:ok, entries} -> {:ok, entries}
{:error, _} -> {:ok, %{}}
end
{:ok, Result.unwrap_or(Client.walk(client_opts, oid), %{})}
end
# Group walk results by row index, extracting the index suffix from OID keys

View file

@ -10,6 +10,7 @@ defmodule Towerops.Topology do
alias Towerops.Devices
alias Towerops.Devices.Device
alias Towerops.Repo
alias Towerops.Result
alias Towerops.Snmp.ArpEntry
alias Towerops.Snmp.Device, as: SnmpDevice
alias Towerops.Snmp.Interface
@ -388,7 +389,7 @@ defmodule Towerops.Topology do
|> Enum.map(&upsert_grouped_evidence(&1, device.id, lookup, now))
|> Enum.reject(&(&1 == :skip))
has_changes = Enum.any?(results, &match?({:ok, _}, &1))
has_changes = Enum.any?(results, &Result.ok?/1)
# Auto-inference disabled - device type is now manual-only
# maybe_update_device_role(device)
@ -1111,7 +1112,7 @@ defmodule Towerops.Topology do
{:ok, %{neighbors: neighbors}} ->
now = DateTime.utc_now()
results = Enum.map(neighbors, &upsert_device_neighbor(device_id, &1, now))
success_count = Enum.count(results, &match?({:ok, _}, &1))
success_count = Enum.count(results, &Result.ok?/1)
{:ok, success_count}
{:error, reason} = error ->