From 6f6627a54d183d203ea411b0c4f37b1421476b58 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Wed, 4 Feb 2026 17:53:18 -0600 Subject: [PATCH] refactor: consolidate device count calculations in agent_live MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove duplicate device counting logic by consolidating three identical implementations into a single reusable function. **Changes:** - Refactor agent_live/index.ex mount/3 to use calculate_device_counts/1 - Delete duplicate calculate_cloud_poller_counts/1 function - Replace all usages with calculate_device_counts/1 **Before:** - Lines 31-47: Inline counting for agent_tokens and cloud_pollers - Lines 312-318: calculate_device_counts/1 - Lines 320-326: calculate_cloud_poller_counts/1 (identical logic) **After:** - Single calculate_device_counts/1 function used everywhere - ~20 lines removed **Benefits:** - Eliminates triple duplication of counting logic - Single source of truth for device counting - Easier to maintain and test 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- lib/towerops_web/live/agent_live/index.ex | 25 ++++------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/lib/towerops_web/live/agent_live/index.ex b/lib/towerops_web/live/agent_live/index.ex index f67f5007..0937648a 100644 --- a/lib/towerops_web/live/agent_live/index.ex +++ b/lib/towerops_web/live/agent_live/index.ex @@ -27,21 +27,12 @@ defmodule ToweropsWeb.AgentLive.Index do end # device counts for each agent (both direct and total with inheritance) - equipment_counts = - Map.new(agent_tokens, fn token -> - direct = Agents.count_assigned_devices(token.id) - total = length(Agents.list_agent_polling_targets(token.id)) - {token.id, %{direct: direct, total: total}} - end) + equipment_counts = calculate_device_counts(agent_tokens) # device counts for cloud pollers (if superuser) cloud_poller_counts = if is_superuser do - Map.new(cloud_pollers, fn token -> - direct = Agents.count_assigned_devices(token.id) - total = length(Agents.list_agent_polling_targets(token.id)) - {token.id, %{direct: direct, total: total}} - end) + calculate_device_counts(cloud_pollers) else %{} end @@ -179,7 +170,7 @@ defmodule ToweropsWeb.AgentLive.Index do {token.id, %{direct: direct, total: total}} end) - cloud_poller_counts = calculate_cloud_poller_counts(cloud_pollers) + cloud_poller_counts = calculate_device_counts(cloud_pollers) # Refresh health statistics agent_health_stats = Stats.get_organization_agent_health(organization.id) @@ -282,7 +273,7 @@ defmodule ToweropsWeb.AgentLive.Index do cloud_pollers = load_cloud_pollers_if_superuser(socket.assigns.current_scope) equipment_counts = calculate_device_counts(agent_tokens) - cloud_poller_counts = calculate_cloud_poller_counts(cloud_pollers) + cloud_poller_counts = calculate_device_counts(cloud_pollers) agent_health_stats = Stats.get_organization_agent_health(organization.id) assignment_breakdown = Stats.get_device_assignment_breakdown(organization.id) @@ -317,14 +308,6 @@ defmodule ToweropsWeb.AgentLive.Index do end) end - defp calculate_cloud_poller_counts(cloud_pollers) do - Map.new(cloud_pollers, fn t -> - direct = Agents.count_assigned_devices(t.id) - total = length(Agents.list_agent_polling_targets(t.id)) - {t.id, %{direct: direct, total: total}} - end) - end - defp get_success_message(is_cloud_poller) do if is_cloud_poller, do: t_equipment("Cloud poller created successfully"),