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:
parent
5f369a3bed
commit
9784cee344
3 changed files with 38 additions and 28 deletions
|
|
@ -348,7 +348,12 @@ defmodule Aprsme.Packet do
|
||||||
%{}
|
%{}
|
||||||
end
|
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 =
|
telemetry_data =
|
||||||
if Map.has_key?(additional_data, :telemetry_vals) do
|
if Map.has_key?(additional_data, :telemetry_vals) do
|
||||||
# Already processed from data_extended
|
# Already processed from data_extended
|
||||||
|
|
|
||||||
|
|
@ -186,19 +186,7 @@ defmodule Aprsme.Telemetry.DatabaseMetrics do
|
||||||
WHERE s.relname = 'packets'
|
WHERE s.relname = 'packets'
|
||||||
""") do
|
""") do
|
||||||
{:ok, %{rows: [[live, dead, ins, upd, del, table_size, idx_size]]}} ->
|
{:ok, %{rows: [[live, dead, ins, upd, del, table_size, idx_size]]}} ->
|
||||||
:telemetry.execute(
|
emit_packets_table_telemetry(live, dead, ins, upd, del, table_size, idx_size)
|
||||||
[: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
|
|
||||||
},
|
|
||||||
%{}
|
|
||||||
)
|
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
:ok
|
:ok
|
||||||
|
|
@ -251,4 +239,20 @@ defmodule Aprsme.Telemetry.DatabaseMetrics do
|
||||||
# For now, we'll skip these as they require additional setup
|
# For now, we'll skip these as they require additional setup
|
||||||
:ok
|
:ok
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -62,20 +62,7 @@ defmodule AprsmeWeb.MapLive.PacketUtils do
|
||||||
@spec has_weather_packets?(String.t()) :: boolean()
|
@spec has_weather_packets?(String.t()) :: boolean()
|
||||||
# Get recent packets for this callsign and check if any are weather packets
|
# Get recent packets for this callsign and check if any are weather packets
|
||||||
def has_weather_packets?(callsign) when is_binary(callsign) do
|
def has_weather_packets?(callsign) when is_binary(callsign) do
|
||||||
# Direct query without caching
|
query = build_weather_check_query(callsign)
|
||||||
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
|
|
||||||
|
|
||||||
Aprsme.Repo.exists?(query)
|
Aprsme.Repo.exists?(query)
|
||||||
rescue
|
rescue
|
||||||
_ -> false
|
_ -> false
|
||||||
|
|
@ -124,4 +111,18 @@ defmodule AprsmeWeb.MapLive.PacketUtils do
|
||||||
defdelegate build_packet_data(packet), to: DataBuilder
|
defdelegate build_packet_data(packet), to: DataBuilder
|
||||||
|
|
||||||
# All packet data building functions have been moved to AprsmeWeb.MapLive.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
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue