towerops/lib/towerops_web/live/site_live/form.ex
Graham McIntire 1bd72dac78
1. Sites Management
- Sites schema with hierarchical parent-child relationships
  - CRUD operations for sites
  - Sites context with helper functions
  - LiveView pages:
    - /orgs/:slug/sites - List all sites
    - /orgs/:slug/sites/new - Create new site
    - /orgs/:slug/sites/:id - View site details
    - /orgs/:slug/sites/:id/edit - Edit site
  - Support for site locations and descriptions
  - Site tree builder for hierarchy visualization

  2. Equipment Management
  - Equipment schema with monitoring fields
  - IP address validation (IPv4 & IPv6)
  - Equipment status tracking (up/down/unknown)
  - Customizable check intervals per equipment
  - Equipment context with CRUD operations
  - LiveView pages:
    - /orgs/:slug/equipment - List all equipment
    - /orgs/:slug/equipment/new - Add equipment
    - /orgs/:slug/equipment/:id - View equipment details
    - /orgs/:slug/equipment/:id/edit - Edit equipment
  - Equipment can be added from site pages
  - Status badges and last checked timestamps

  3. Database Schema
  - sites table with self-referencing parent_site_id
  - equipment table with status tracking
  - All migrations run successfully
  - Proper indexes on foreign keys and status

  4. Features Implemented
  -  IP address validation using :inet.parse_address
  -  Site hierarchy with parent-child relationships
  -  Equipment linked to sites
  -  Monitoring enabled/disabled per equipment
  -  Customizable check intervals (30s - 3600s)
  -  Status tracking (up/down/unknown)
  -  Timestamps for last check and last status change
  -  Organization-scoped data (users only see their org's data)
2025-12-21 13:39:19 -06:00

89 lines
2.7 KiB
Elixir

defmodule ToweropsWeb.SiteLive.Form do
@moduledoc false
use ToweropsWeb, :live_view
alias Towerops.Sites
alias Towerops.Sites.Site
@impl true
def mount(_params, _session, socket) do
organization = socket.assigns.current_organization
{:ok,
socket
|> assign(:organization, organization)
|> assign(:available_parent_sites, Sites.list_organization_sites(organization.id))}
end
@impl true
def handle_params(params, _url, socket) do
{:noreply, apply_action(socket, socket.assigns.live_action, params)}
end
defp apply_action(socket, :new, _params) do
changeset = Sites.change_site(%Site{organization_id: socket.assigns.organization.id})
socket
|> assign(:page_title, "New Site")
|> assign(:site, %Site{})
|> assign(:form, to_form(changeset))
end
defp apply_action(socket, :edit, %{"id" => id}) do
site = Sites.get_organization_site!(socket.assigns.organization.id, id)
changeset = Sites.change_site(site)
# Filter out the current site and its descendants from parent options
available_parent_sites =
Enum.reject(socket.assigns.available_parent_sites, fn s -> s.id == site.id end)
socket
|> assign(:page_title, "Edit Site")
|> assign(:site, site)
|> assign(:form, to_form(changeset))
|> assign(:available_parent_sites, available_parent_sites)
end
@impl true
def handle_event("validate", %{"site" => site_params}, socket) do
changeset =
socket.assigns.site
|> Sites.change_site(site_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :form, to_form(changeset))}
end
@impl true
def handle_event("save", %{"site" => site_params}, socket) do
save_site(socket, socket.assigns.live_action, site_params)
end
defp save_site(socket, :new, site_params) do
site_params = Map.put(site_params, "organization_id", socket.assigns.organization.id)
case Sites.create_site(site_params) do
{:ok, _site} ->
{:noreply,
socket
|> put_flash(:info, "Site created successfully")
|> push_navigate(to: ~p"/orgs/#{socket.assigns.organization.slug}/sites")}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :form, to_form(changeset))}
end
end
defp save_site(socket, :edit, site_params) do
case Sites.update_site(socket.assigns.site, site_params) do
{:ok, _site} ->
{:noreply,
socket
|> put_flash(:info, "Site updated successfully")
|> push_navigate(to: ~p"/orgs/#{socket.assigns.organization.slug}/sites")}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :form, to_form(changeset))}
end
end
end