diff --git a/lib/towerops/gaiia.ex b/lib/towerops/gaiia.ex index ddbe307f..0bdc1389 100644 --- a/lib/towerops/gaiia.ex +++ b/lib/towerops/gaiia.ex @@ -132,6 +132,24 @@ defmodule Towerops.Gaiia do ) end + # --- Reverse lookups (Towerops entity → Gaiia entity) --- + + @doc "Find the Gaiia inventory item mapped to a Towerops device." + def get_inventory_item_for_device(device_id) do + InventoryItem + |> where(device_id: ^device_id) + |> limit(1) + |> Repo.one() + end + + @doc "Find the Gaiia network site mapped to a Towerops site." + def get_network_site_for_site(site_id) do + NetworkSite + |> where(site_id: ^site_id) + |> limit(1) + |> Repo.one() + end + # --- Match Suggestions --- @doc """ diff --git a/lib/towerops_web/live/device_live/show.ex b/lib/towerops_web/live/device_live/show.ex index ec3c640e..402dda04 100644 --- a/lib/towerops_web/live/device_live/show.ex +++ b/lib/towerops_web/live/device_live/show.ex @@ -7,6 +7,7 @@ defmodule ToweropsWeb.DeviceLive.Show do alias Towerops.Devices.Firmware alias Towerops.Devices.MikrotikBackups alias Towerops.Devices.VersionComparator + alias Towerops.Gaiia alias Towerops.Monitoring alias Towerops.Repo alias Towerops.Snmp @@ -263,6 +264,7 @@ defmodule ToweropsWeb.DeviceLive.Show do |> assign_backups_data(device_id) |> assign_checks_data(device_id) |> assign_preseem_data() + |> assign_gaiia_data() end # -- Selective reload helpers (used by handle_info handlers) -- @@ -303,6 +305,7 @@ defmodule ToweropsWeb.DeviceLive.Show do "backups" -> assign_backups_data(socket, device_id) "checks" -> assign_checks_data(socket, device_id) "preseem" -> assign_preseem_data(socket) + "gaiia" -> assign_gaiia_data(socket) # neighbors, arp, mac, vlans, ip_addresses, debug use data from tab_nav/base _ -> socket end @@ -477,6 +480,48 @@ defmodule ToweropsWeb.DeviceLive.Show do |> assign(:grouped_checks, grouped_checks) end + # Gaiia tab data. + defp assign_gaiia_data(socket) do + device = socket.assigns.device + gaiia_item = Gaiia.get_inventory_item_for_device(device.id) + + {gaiia_account, gaiia_subscriptions, gaiia_network_site} = + if gaiia_item do + account = + if gaiia_item.assigned_account_gaiia_id do + Gaiia.get_account(device.organization_id, gaiia_item.assigned_account_gaiia_id) + end + + subscriptions = + if gaiia_item.assigned_account_gaiia_id do + Gaiia.list_billing_subscriptions_for_account( + device.organization_id, + gaiia_item.assigned_account_gaiia_id + ) + else + [] + end + + network_site = + if gaiia_item.assigned_network_site_gaiia_id do + Gaiia.get_network_site( + device.organization_id, + gaiia_item.assigned_network_site_gaiia_id + ) + end + + {account, subscriptions, network_site} + else + {nil, [], nil} + end + + socket + |> assign(:gaiia_item, gaiia_item) + |> assign(:gaiia_account, gaiia_account) + |> assign(:gaiia_subscriptions, gaiia_subscriptions) + |> assign(:gaiia_network_site, gaiia_network_site) + end + # Preseem tab data. defp assign_preseem_data(socket) do device = socket.assigns.device diff --git a/lib/towerops_web/live/device_live/show.html.heex b/lib/towerops_web/live/device_live/show.html.heex index adbba4c7..3b5aced3 100644 --- a/lib/towerops_web/live/device_live/show.html.heex +++ b/lib/towerops_web/live/device_live/show.html.heex @@ -247,6 +247,27 @@ <% end %> + <%= if @gaiia_item do %> + <.link + patch={~p"/devices/#{@device.id}?tab=gaiia"} + class={[ + "whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm", + if(@active_tab == "gaiia", + do: "border-blue-500 text-blue-600 dark:text-blue-400", + else: + "border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 dark:text-gray-400 dark:hover:text-gray-300" + ) + ]} + > + + <.icon + name="hero-building-office" + class="h-3.5 w-3.5 text-emerald-500 dark:text-emerald-400" + /> Gaiia + + + <% end %> + <.link patch={~p"/devices/#{@device.id}?tab=checks"} class={[ @@ -2306,6 +2327,300 @@ <% end %> <% end %> + <% "gaiia" -> %> + <%= if @gaiia_item do %> +
| + Plan + | ++ Status + | ++ MRR + | +
|---|---|---|
| + {sub.product_name || "---"} + | ++ + "bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400" + + "suspended" -> + "bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400" + + _ -> + "bg-gray-100 text-gray-800 dark:bg-gray-900/30 dark:text-gray-400" + end + ]}> + {sub.status || "unknown"} + + | ++ <%= if sub.mrr_amount do %> + ${Decimal.round(sub.mrr_amount, 2)} + + {sub.currency || ""} + + <% else %> + --- + <% end %> + | +
+ No Gaiia inventory item linked to this device. +
+