Fix credo warnings and software design suggestions
Replace expensive length/1 comparisons with direct list comparisons or Enum.empty?/1 checks (13 instances in source code and tests). Add module aliases for Phoenix.HTML.Form and Towerops.Accounts.Scope to reduce nested module references. Changes: - Replace length(list) > 0 with list != [] or refute Enum.empty?(list) - Add Phoenix.HTML.Form alias in CoreComponents - Add Towerops.Accounts.Scope alias in ConnCase test helper This eliminates all credo warnings and software design suggestions.
This commit is contained in:
parent
853d548f82
commit
a3cd43d2c2
9 changed files with 22 additions and 19 deletions
|
|
@ -55,7 +55,7 @@ defmodule Towerops.Snmp.PollerWorker do
|
|||
# Get the device to check if it has sensors/interfaces
|
||||
device = Snmp.get_device_with_associations(equipment_id)
|
||||
|
||||
if device && (length(device.sensors) > 0 || length(device.interfaces) > 0) do
|
||||
if device && (device.sensors != [] || device.interfaces != []) do
|
||||
# Perform immediate poll when starting
|
||||
send(self(), :poll_data)
|
||||
end
|
||||
|
|
@ -416,7 +416,7 @@ defmodule Towerops.Snmp.PollerWorker do
|
|||
end
|
||||
|
||||
# If there are changes, broadcast events and update interface
|
||||
if length(changes) > 0 do
|
||||
if changes != [] do
|
||||
# Broadcast all events via PubSub
|
||||
Enum.each(changes, fn {:event, event_attrs} ->
|
||||
Phoenix.PubSub.broadcast(
|
||||
|
|
|
|||
|
|
@ -106,12 +106,12 @@ defmodule Towerops.Snmp.Profiles.Mikrotik do
|
|||
|
||||
all_sensors = health_sensors ++ resource_sensors ++ entity_sensors
|
||||
|
||||
if length(all_sensors) > 0 do
|
||||
Logger.debug("Discovered #{length(all_sensors)} MikroTik sensors")
|
||||
{:ok, all_sensors}
|
||||
else
|
||||
if all_sensors == [] do
|
||||
Logger.warning("No MikroTik sensors found")
|
||||
{:ok, []}
|
||||
else
|
||||
Logger.debug("Discovered #{length(all_sensors)} MikroTik sensors")
|
||||
{:ok, all_sensors}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -65,12 +65,12 @@ defmodule Towerops.Snmp.Profiles.NetSnmp do
|
|||
|
||||
all_sensors = lm_sensors ++ ucd_sensors
|
||||
|
||||
if length(all_sensors) > 0 do
|
||||
{:ok, all_sensors}
|
||||
else
|
||||
if all_sensors == [] do
|
||||
# Fall back to standard ENTITY-SENSOR-MIB
|
||||
Logger.debug("Net-SNMP specific sensors not available, using standard discovery")
|
||||
Base.discover_sensors(client_opts)
|
||||
else
|
||||
{:ok, all_sensors}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ defmodule ToweropsWeb.CoreComponents do
|
|||
use Phoenix.Component
|
||||
use Gettext, backend: ToweropsWeb.Gettext
|
||||
|
||||
alias Phoenix.HTML.Form
|
||||
alias Phoenix.HTML.FormField
|
||||
alias Phoenix.LiveView.JS
|
||||
|
||||
|
|
@ -247,7 +248,7 @@ defmodule ToweropsWeb.CoreComponents do
|
|||
def input(%{type: "checkbox"} = assigns) do
|
||||
assigns =
|
||||
assign_new(assigns, :checked, fn ->
|
||||
Phoenix.HTML.Form.normalize_value("checkbox", assigns[:value])
|
||||
Form.normalize_value("checkbox", assigns[:value])
|
||||
end)
|
||||
|
||||
~H"""
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ defmodule ToweropsWeb.EquipmentLive.Index do
|
|||
socket
|
||||
|> assign(:page_title, "Equipment")
|
||||
|> assign(:equipment, equipment)
|
||||
|> assign(:has_sites, length(sites) > 0)}
|
||||
|> assign(:has_sites, sites != [])}
|
||||
end
|
||||
|
||||
@impl true
|
||||
|
|
|
|||
|
|
@ -83,11 +83,11 @@ defmodule Towerops.Integration.SnmpIntegrationTest do
|
|||
|
||||
# Discover interfaces
|
||||
{:ok, interfaces} = Mikrotik.discover_interfaces(@test_device_opts)
|
||||
assert length(interfaces) > 0
|
||||
refute Enum.empty?(interfaces)
|
||||
|
||||
# Discover sensors
|
||||
{:ok, sensors} = Mikrotik.discover_sensors(@test_device_opts)
|
||||
assert length(sensors) > 0
|
||||
refute Enum.empty?(sensors)
|
||||
|
||||
# Identify device
|
||||
device_info = Mikrotik.identify_device(system_info)
|
||||
|
|
@ -134,7 +134,7 @@ defmodule Towerops.Integration.SnmpIntegrationTest do
|
|||
Process.sleep(200)
|
||||
|
||||
alerts = Towerops.Alerts.list_equipment_alerts(equipment.id)
|
||||
assert length(alerts) > 0
|
||||
refute Enum.empty?(alerts)
|
||||
|
||||
alert = hd(alerts)
|
||||
assert alert.alert_type == :equipment_down
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ defmodule ToweropsWeb.ConnCase do
|
|||
|
||||
use ExUnit.CaseTemplate
|
||||
|
||||
alias Towerops.Accounts.Scope
|
||||
|
||||
using do
|
||||
quote do
|
||||
use ToweropsWeb, :verified_routes
|
||||
|
|
@ -46,7 +48,7 @@ defmodule ToweropsWeb.ConnCase do
|
|||
"""
|
||||
def register_and_log_in_user(%{conn: conn} = context) do
|
||||
user = Towerops.AccountsFixtures.user_fixture()
|
||||
scope = Towerops.Accounts.Scope.for_user(user)
|
||||
scope = Scope.for_user(user)
|
||||
|
||||
opts =
|
||||
context
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ defmodule Towerops.Monitoring.EquipmentMonitorTest do
|
|||
|
||||
# Should have created a monitoring check
|
||||
checks = Monitoring.list_equipment_checks(equipment.id)
|
||||
assert length(checks) > 0
|
||||
refute Enum.empty?(checks)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -131,7 +131,7 @@ defmodule Towerops.Monitoring.EquipmentMonitorTest do
|
|||
|
||||
# Should create an alert (no need to sleep since test env doesn't send emails)
|
||||
alerts = Alerts.list_equipment_alerts(equipment.id)
|
||||
assert length(alerts) > 0
|
||||
refute Enum.empty?(alerts)
|
||||
|
||||
alert = hd(alerts)
|
||||
assert alert.alert_type == :equipment_down
|
||||
|
|
@ -158,7 +158,7 @@ defmodule Towerops.Monitoring.EquipmentMonitorTest do
|
|||
# Should create recovery alert
|
||||
alerts = Alerts.list_equipment_alerts(equipment.id, 10)
|
||||
up_alerts = Enum.filter(alerts, &(&1.alert_type == :equipment_up))
|
||||
assert length(up_alerts) > 0
|
||||
refute Enum.empty?(up_alerts)
|
||||
end
|
||||
|
||||
test "does not create duplicate down alerts", %{equipment: equipment} do
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@ defmodule ToweropsWeb.EquipmentLiveTest do
|
|||
|
||||
# Verify equipment was created
|
||||
equipment_list = Towerops.Equipment.list_organization_equipment(organization.id)
|
||||
assert length(equipment_list) > 0
|
||||
refute Enum.empty?(equipment_list)
|
||||
|
||||
new_equipment = Enum.find(equipment_list, &(&1.name == "New Router"))
|
||||
assert new_equipment
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue