Reduce nesting depth in regex_cache, connection_monitor, and device_identification
Extract nested logic into helper functions to improve readability: - regex_cache.ex: Extract compile_and_cache/1 for regex compilation and caching - connection_monitor.ex: Extract calculate_scheduler_utilization/1 for CPU stat calculation - device_identification.ex: Extract fetch_devices_from_url/0 for HTTP request handling Co-Authored-By: Graham <noreply@anthropic.com>
This commit is contained in:
parent
73448d7811
commit
48be64583a
3 changed files with 34 additions and 30 deletions
|
|
@ -204,9 +204,7 @@ defmodule Aprsme.ConnectionMonitor do
|
|||
# Fallback to scheduler wall time
|
||||
:scheduler_wall_time_all
|
||||
|> :erlang.statistics()
|
||||
|> Enum.map(fn {_, active, total} ->
|
||||
if total > 0, do: active / total, else: 0.0
|
||||
end)
|
||||
|> Enum.map(&calculate_scheduler_utilization/1)
|
||||
|> Enum.sum()
|
||||
|> Kernel./(System.schedulers_online())
|
||||
end
|
||||
|
|
@ -214,6 +212,10 @@ defmodule Aprsme.ConnectionMonitor do
|
|||
_ -> 0.0
|
||||
end
|
||||
|
||||
defp calculate_scheduler_utilization({_, active, total}) do
|
||||
if total > 0, do: active / total, else: 0.0
|
||||
end
|
||||
|
||||
defp get_memory_usage do
|
||||
# Get memory usage as a percentage
|
||||
mem_data = :erlang.memory()
|
||||
|
|
|
|||
|
|
@ -136,27 +136,25 @@ defmodule Aprsme.DeviceIdentification do
|
|||
end
|
||||
|
||||
def fetch_and_upsert_devices do
|
||||
case CircuitBreaker.call(
|
||||
:aprs_foundation_api,
|
||||
fn ->
|
||||
case Req.get(@url) do
|
||||
{:ok, %Req.Response{status: 200, body: body}} ->
|
||||
upsert_devices(body)
|
||||
|
||||
{:ok, %Req.Response{status: status}} ->
|
||||
{:error, {:http_error, status}}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end,
|
||||
15_000
|
||||
) do
|
||||
case CircuitBreaker.call(:aprs_foundation_api, &fetch_devices_from_url/0, 15_000) do
|
||||
{:ok, result} -> result
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp fetch_devices_from_url do
|
||||
case Req.get(@url) do
|
||||
{:ok, %Req.Response{status: 200, body: body}} ->
|
||||
upsert_devices(body)
|
||||
|
||||
{:ok, %Req.Response{status: status}} ->
|
||||
{:error, {:http_error, status}}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
def upsert_devices(json) do
|
||||
tocalls = Map.get(json, "tocalls", %{})
|
||||
mice = Map.get(json, "mice", %{})
|
||||
|
|
|
|||
|
|
@ -31,19 +31,23 @@ defmodule Aprsme.RegexCache do
|
|||
{:ok, regex}
|
||||
|
||||
[] ->
|
||||
case Regex.compile(pattern_string) do
|
||||
{:ok, regex} ->
|
||||
# Check cache size and clear if needed
|
||||
if :ets.info(@table_name, :size) >= @max_cache_size do
|
||||
clear_oldest_entries()
|
||||
end
|
||||
compile_and_cache(pattern_string)
|
||||
end
|
||||
end
|
||||
|
||||
:ets.insert(@table_name, {pattern_string, regex})
|
||||
{:ok, regex}
|
||||
|
||||
error ->
|
||||
error
|
||||
defp compile_and_cache(pattern_string) do
|
||||
case Regex.compile(pattern_string) do
|
||||
{:ok, regex} ->
|
||||
# Check cache size and clear if needed
|
||||
if :ets.info(@table_name, :size) >= @max_cache_size do
|
||||
clear_oldest_entries()
|
||||
end
|
||||
|
||||
:ets.insert(@table_name, {pattern_string, regex})
|
||||
{:ok, regex}
|
||||
|
||||
error ->
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue