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
|
# Fallback to scheduler wall time
|
||||||
:scheduler_wall_time_all
|
:scheduler_wall_time_all
|
||||||
|> :erlang.statistics()
|
|> :erlang.statistics()
|
||||||
|> Enum.map(fn {_, active, total} ->
|
|> Enum.map(&calculate_scheduler_utilization/1)
|
||||||
if total > 0, do: active / total, else: 0.0
|
|
||||||
end)
|
|
||||||
|> Enum.sum()
|
|> Enum.sum()
|
||||||
|> Kernel./(System.schedulers_online())
|
|> Kernel./(System.schedulers_online())
|
||||||
end
|
end
|
||||||
|
|
@ -214,6 +212,10 @@ defmodule Aprsme.ConnectionMonitor do
|
||||||
_ -> 0.0
|
_ -> 0.0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp calculate_scheduler_utilization({_, active, total}) do
|
||||||
|
if total > 0, do: active / total, else: 0.0
|
||||||
|
end
|
||||||
|
|
||||||
defp get_memory_usage do
|
defp get_memory_usage do
|
||||||
# Get memory usage as a percentage
|
# Get memory usage as a percentage
|
||||||
mem_data = :erlang.memory()
|
mem_data = :erlang.memory()
|
||||||
|
|
|
||||||
|
|
@ -136,27 +136,25 @@ defmodule Aprsme.DeviceIdentification do
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_and_upsert_devices do
|
def fetch_and_upsert_devices do
|
||||||
case CircuitBreaker.call(
|
case CircuitBreaker.call(:aprs_foundation_api, &fetch_devices_from_url/0, 15_000) do
|
||||||
: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
|
|
||||||
{:ok, result} -> result
|
{:ok, result} -> result
|
||||||
{:error, reason} -> {:error, reason}
|
{:error, reason} -> {:error, reason}
|
||||||
end
|
end
|
||||||
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
|
def upsert_devices(json) do
|
||||||
tocalls = Map.get(json, "tocalls", %{})
|
tocalls = Map.get(json, "tocalls", %{})
|
||||||
mice = Map.get(json, "mice", %{})
|
mice = Map.get(json, "mice", %{})
|
||||||
|
|
|
||||||
|
|
@ -31,19 +31,23 @@ defmodule Aprsme.RegexCache do
|
||||||
{:ok, regex}
|
{:ok, regex}
|
||||||
|
|
||||||
[] ->
|
[] ->
|
||||||
case Regex.compile(pattern_string) do
|
compile_and_cache(pattern_string)
|
||||||
{:ok, regex} ->
|
end
|
||||||
# Check cache size and clear if needed
|
end
|
||||||
if :ets.info(@table_name, :size) >= @max_cache_size do
|
|
||||||
clear_oldest_entries()
|
|
||||||
end
|
|
||||||
|
|
||||||
:ets.insert(@table_name, {pattern_string, regex})
|
defp compile_and_cache(pattern_string) do
|
||||||
{:ok, regex}
|
case Regex.compile(pattern_string) do
|
||||||
|
{:ok, regex} ->
|
||||||
error ->
|
# Check cache size and clear if needed
|
||||||
error
|
if :ets.info(@table_name, :size) >= @max_cache_size do
|
||||||
|
clear_oldest_entries()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
:ets.insert(@table_name, {pattern_string, regex})
|
||||||
|
{:ok, regex}
|
||||||
|
|
||||||
|
error ->
|
||||||
|
error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue