diff --git a/lib/aprsme/connection_monitor.ex b/lib/aprsme/connection_monitor.ex index 4a1fa48..9bd4552 100644 --- a/lib/aprsme/connection_monitor.ex +++ b/lib/aprsme/connection_monitor.ex @@ -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() diff --git a/lib/aprsme/device_identification.ex b/lib/aprsme/device_identification.ex index e618ec8..2efc6f0 100644 --- a/lib/aprsme/device_identification.ex +++ b/lib/aprsme/device_identification.ex @@ -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", %{}) diff --git a/lib/aprsme/regex_cache.ex b/lib/aprsme/regex_cache.ex index ff899c0..435b3ea 100644 --- a/lib/aprsme/regex_cache.ex +++ b/lib/aprsme/regex_cache.ex @@ -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