towerops/test/towerops_web/live/capacity_live_test.exs
Graham McIntire 1c222f8ff5
feat: add capacity insight worker, UI, and reporting views
Add CapacityInsightWorker (every 15 min) that generates critical/warning
insights when backhaul utilization exceeds 90%/75% and auto-resolves
when it drops below 70%.

Add capacity and utilization columns to the device ports tab with
set/clear capacity controls. Add organization-level /capacity page
with summary cards and per-site capacity table. Add capacity summary
card to site show page. Add Capacity link to nav menu.
2026-03-07 14:42:40 -06:00

97 lines
2.7 KiB
Elixir

defmodule ToweropsWeb.CapacityLiveTest do
use ToweropsWeb.ConnCase, async: true
import Phoenix.LiveViewTest
import Towerops.AccountsFixtures
alias Towerops.Snmp.Device, as: SnmpDevice
alias Towerops.Snmp.Interface
alias Towerops.Snmp.InterfaceStat
setup do
user = user_fixture(enable_totp: true)
{:ok, organization} = Towerops.Organizations.create_organization(%{name: "Test ISP"}, user.id)
{:ok, site} =
Towerops.Sites.create_site(%{
name: "Tower Alpha",
organization_id: organization.id
})
{:ok, device} =
Towerops.Devices.create_device(%{
name: "Backhaul Radio",
ip_address: "10.0.0.1",
snmp_enabled: true,
snmp_version: "2c",
snmp_community: "public",
site_id: site.id,
organization_id: organization.id
})
snmp_device =
%SnmpDevice{}
|> SnmpDevice.changeset(%{device_id: device.id, sys_name: "radio", sys_descr: "PTP"})
|> Towerops.Repo.insert!()
interface =
%Interface{}
|> Interface.changeset(%{
snmp_device_id: snmp_device.id,
if_index: 1,
if_name: "eth0",
if_speed: 1_000_000_000,
if_type: 6,
configured_capacity_bps: 300_000_000,
capacity_source: "manual"
})
|> Towerops.Repo.insert!()
now = DateTime.utc_now()
%InterfaceStat{}
|> InterfaceStat.changeset(%{
interface_id: interface.id,
if_in_octets: 0,
if_out_octets: 0,
checked_at: DateTime.add(now, -120, :second)
})
|> Towerops.Repo.insert!()
%InterfaceStat{}
|> InterfaceStat.changeset(%{
interface_id: interface.id,
if_in_octets: 0,
if_out_octets: 1_125_000_000,
checked_at: DateTime.add(now, -60, :second)
})
|> Towerops.Repo.insert!()
%{user: user, organization: organization, site: site, device: device, interface: interface}
end
describe "capacity index page" do
test "redirects to login when not authenticated", %{conn: conn} do
assert {:error, redirect} = live(conn, ~p"/capacity")
assert {:redirect, %{to: to}} = redirect
assert to =~ "/users/log-in"
end
test "displays capacity page with site data", %{conn: conn, user: user} do
conn = log_in_user(conn, user)
{:ok, _view, html} = live(conn, ~p"/capacity")
assert html =~ "Capacity"
assert html =~ "Tower Alpha"
assert html =~ "300.0 Mbps"
end
test "shows utilization percentage for site", %{conn: conn, user: user} do
conn = log_in_user(conn, user)
{:ok, _view, html} = live(conn, ~p"/capacity")
# Site has 300 Mbps capacity with ~150 Mbps throughput = ~50%
assert html =~ "%"
end
end
end