Reduce cyclomatic complexity in multiple files

Extract helper functions to reduce cyclomatic complexity:

- telemetry/database_metrics.ex: Extract emit_packets_table_telemetry/7
  Moves multiple || 0 operators into separate function
  Reduces complexity from 10 to within limits

- packet_utils.ex: Extract build_weather_check_query/1
  Moves Ecto query building with multiple or conditions
  Reduces complexity from 11 to within limits

- packet.ex: Extract merge_extracted_data/3
  Moves telemetry and parser data merging logic
  Reduces complexity from 11 to within limits

Fixes 3 Credo cyclomatic complexity issues (7 remaining).
This commit is contained in:
Graham McIntire 2026-02-09 13:13:15 -06:00
parent 5f369a3bed
commit 9784cee344
No known key found for this signature in database
3 changed files with 38 additions and 28 deletions

View file

@ -348,7 +348,12 @@ defmodule Aprsme.Packet do
%{}
end
# Also process telemetry fields from top-level attrs if not already processed
# Merge all extracted data
merge_extracted_data(base_attrs, additional_data, attrs)
end
defp merge_extracted_data(base_attrs, additional_data, attrs) do
# Process telemetry fields from top-level attrs if not already processed
telemetry_data =
if Map.has_key?(additional_data, :telemetry_vals) do
# Already processed from data_extended

View file

@ -186,19 +186,7 @@ defmodule Aprsme.Telemetry.DatabaseMetrics do
WHERE s.relname = 'packets'
""") do
{:ok, %{rows: [[live, dead, ins, upd, del, table_size, idx_size]]}} ->
:telemetry.execute(
[:aprsme, :postgres, :packets_table],
%{
live_tuples: live || 0,
dead_tuples: dead || 0,
total_inserts: ins || 0,
total_updates: upd || 0,
total_deletes: del || 0,
table_size_bytes: table_size || 0,
indexes_size_bytes: idx_size || 0
},
%{}
)
emit_packets_table_telemetry(live, dead, ins, upd, del, table_size, idx_size)
_ ->
:ok
@ -251,4 +239,20 @@ defmodule Aprsme.Telemetry.DatabaseMetrics do
# For now, we'll skip these as they require additional setup
:ok
end
defp emit_packets_table_telemetry(live, dead, ins, upd, del, table_size, idx_size) do
:telemetry.execute(
[:aprsme, :postgres, :packets_table],
%{
live_tuples: live || 0,
dead_tuples: dead || 0,
total_inserts: ins || 0,
total_updates: upd || 0,
total_deletes: del || 0,
table_size_bytes: table_size || 0,
indexes_size_bytes: idx_size || 0
},
%{}
)
end
end

View file

@ -62,20 +62,7 @@ defmodule AprsmeWeb.MapLive.PacketUtils do
@spec has_weather_packets?(String.t()) :: boolean()
# Get recent packets for this callsign and check if any are weather packets
def has_weather_packets?(callsign) when is_binary(callsign) do
# Direct query without caching
import Ecto.Query
query =
from p in Aprsme.Packet,
where: p.sender == ^callsign,
where:
not is_nil(p.temperature) or not is_nil(p.humidity) or not is_nil(p.pressure) or
not is_nil(p.wind_direction) or not is_nil(p.wind_speed) or not is_nil(p.wind_gust) or
not is_nil(p.rain_1h) or not is_nil(p.rain_24h) or not is_nil(p.rain_midnight) or
not is_nil(p.luminosity) or not is_nil(p.snow_24h),
select: fragment("1"),
limit: 1
query = build_weather_check_query(callsign)
Aprsme.Repo.exists?(query)
rescue
_ -> false
@ -124,4 +111,18 @@ defmodule AprsmeWeb.MapLive.PacketUtils do
defdelegate build_packet_data(packet), to: DataBuilder
# All packet data building functions have been moved to AprsmeWeb.MapLive.DataBuilder
defp build_weather_check_query(callsign) do
import Ecto.Query
from p in Aprsme.Packet,
where: p.sender == ^callsign,
where:
not is_nil(p.temperature) or not is_nil(p.humidity) or not is_nil(p.pressure) or
not is_nil(p.wind_direction) or not is_nil(p.wind_speed) or not is_nil(p.wind_gust) or
not is_nil(p.rain_1h) or not is_nil(p.rain_24h) or not is_nil(p.rain_midnight) or
not is_nil(p.luminosity) or not is_nil(p.snow_24h),
select: fragment("1"),
limit: 1
end
end