diff --git a/lib/towerops/alerts.ex b/lib/towerops/alerts.ex index 0cedfd6d..89dc366d 100644 --- a/lib/towerops/alerts.ex +++ b/lib/towerops/alerts.ex @@ -6,18 +6,40 @@ defmodule Towerops.Alerts do import Ecto.Query alias Towerops.Alerts.Alert + alias Towerops.Gaiia.ImpactAnalysis alias Towerops.Repo @doc """ Creates an alert for device status change. + + For device_down alerts, automatically computes and stores Gaiia impact analysis + if the device belongs to an organization with a Gaiia integration. """ @spec create_alert(map()) :: {:ok, Alert.t()} | {:error, Ecto.Changeset.t()} def create_alert(attrs) do - %Alert{} - |> Alert.changeset(attrs) - |> Repo.insert() + with {:ok, alert} <- Repo.insert(Alert.changeset(%Alert{}, attrs)) do + maybe_compute_gaiia_impact(alert) + {:ok, alert} + end end + defp maybe_compute_gaiia_impact(%Alert{alert_type: :device_down, device_id: device_id} = alert) + when not is_nil(device_id) do + device = Towerops.Devices.get_device(device_id) + + if device do + impact = ImpactAnalysis.analyze_device_impact(device.organization_id, device_id) + + if impact.total_subscribers > 0 do + store_gaiia_impact(alert, ImpactAnalysis.to_json(impact)) + end + end + rescue + _ -> :ok + end + + defp maybe_compute_gaiia_impact(_alert), do: :ok + @doc """ Returns the list of alerts for an device. """ @@ -183,6 +205,15 @@ defmodule Towerops.Alerts do |> Repo.update() end + @doc """ + Stores Gaiia impact analysis data on an alert record. + """ + def store_gaiia_impact(%Alert{} = alert, impact_data) when is_map(impact_data) do + alert + |> Alert.changeset(%{gaiia_impact: impact_data}) + |> Repo.update() + end + @doc """ Checks if there's an active alert of the same type for the device. """ diff --git a/lib/towerops/alerts/alert.ex b/lib/towerops/alerts/alert.ex index bc9b343f..788c2fc1 100644 --- a/lib/towerops/alerts/alert.ex +++ b/lib/towerops/alerts/alert.ex @@ -22,6 +22,7 @@ defmodule Towerops.Alerts.Alert do field :resolved_at, :utc_datetime field :email_sent_at, :utc_datetime field :message, :string + field :gaiia_impact, :map belongs_to :device, Device belongs_to :acknowledged_by, User @@ -37,6 +38,7 @@ defmodule Towerops.Alerts.Alert do resolved_at: DateTime.t() | nil, email_sent_at: DateTime.t() | nil, message: String.t() | nil, + gaiia_impact: map() | nil, device_id: Ecto.UUID.t(), device: NotLoaded.t() | Device.t(), acknowledged_by_id: Ecto.UUID.t() | nil, @@ -56,7 +58,8 @@ defmodule Towerops.Alerts.Alert do :acknowledged_by_id, :resolved_at, :email_sent_at, - :message + :message, + :gaiia_impact ]) |> validate_required([:device_id, :alert_type, :triggered_at]) |> foreign_key_constraint(:device_id) diff --git a/lib/towerops/gaiia/impact_analysis.ex b/lib/towerops/gaiia/impact_analysis.ex new file mode 100644 index 00000000..1d2d06f5 --- /dev/null +++ b/lib/towerops/gaiia/impact_analysis.ex @@ -0,0 +1,128 @@ +defmodule Towerops.Gaiia.ImpactAnalysis do + @moduledoc """ + Calculates subscriber impact when a Towerops device goes down. + + Three layers of impact analysis (broadest to most precise): + 1. Site-level: Device → Site → Gaiia Network Site → all subscribers at that site + 2. Device-level: Device → Gaiia Inventory Item → directly assigned subscriber + """ + + alias Towerops.Devices + alias Towerops.Gaiia + + @doc """ + Analyze the subscriber impact of a device going down. + + Returns a map with: + - `:device_level` - direct subscriber impact (from inventory item assignment) + - `:site_level` - broader site-level impact (from network site mapping) + - `:total_subscribers` - broadest subscriber count (site > device) + - `:total_mrr` - broadest MRR estimate + - `:analyzed_at` - timestamp of analysis + """ + @spec analyze_device_impact(String.t(), String.t()) :: map() + def analyze_device_impact(organization_id, device_id) do + device = Devices.get_device(device_id) + device_level = analyze_device_level(organization_id, device_id) + site_level = analyze_site_level(organization_id, device) + + {total_subscribers, total_mrr} = compute_totals(device_level, site_level) + + %{ + device_level: device_level, + site_level: site_level, + total_subscribers: total_subscribers, + total_mrr: total_mrr, + analyzed_at: DateTime.truncate(DateTime.utc_now(), :second) + } + end + + @doc """ + Convert impact analysis result to a JSON-serializable map for storage. + """ + @spec to_json(map()) :: map() + def to_json(impact) do + %{ + "device_level" => encode_level(impact.device_level), + "site_level" => encode_level(impact.site_level), + "total_subscribers" => impact.total_subscribers, + "total_mrr" => to_string(impact.total_mrr), + "analyzed_at" => DateTime.to_iso8601(impact.analyzed_at) + } + end + + defp analyze_device_level(organization_id, device_id) do + case Gaiia.get_inventory_item_for_device(device_id) do + nil -> + nil + + item -> + {subscribers, mrr} = get_item_subscriber_impact(organization_id, item) + + if subscribers > 0 do + %{ + subscriber_count: subscribers, + mrr: mrr, + inventory_item_name: item.name, + confidence: :device_level + } + end + end + end + + defp analyze_site_level(_organization_id, nil), do: nil + + defp analyze_site_level(_organization_id, %{site_id: nil}), do: nil + + defp analyze_site_level(_organization_id, device) do + case Gaiia.get_network_site_for_site(device.site_id) do + nil -> + nil + + network_site -> + count = network_site.account_count || 0 + mrr = network_site.total_mrr || Decimal.new("0") + + %{ + subscriber_count: count, + mrr: mrr, + site_name: network_site.name, + confidence: :site_level + } + end + end + + defp get_item_subscriber_impact(organization_id, item) do + case item.assigned_account_gaiia_id do + nil -> + {0, Decimal.new("0")} + + account_gaiia_id -> + case Gaiia.get_account(organization_id, account_gaiia_id) do + nil -> {1, Decimal.new("0")} + account -> {1, account.mrr || Decimal.new("0")} + end + end + end + + defp compute_totals(nil, nil), do: {0, Decimal.new("0")} + defp compute_totals(nil, site), do: {site.subscriber_count, site.mrr} + defp compute_totals(device, nil), do: {device.subscriber_count, device.mrr} + + defp compute_totals(_device, site) do + # Site-level is the broadest impact estimate + {site.subscriber_count, site.mrr} + end + + defp encode_level(nil), do: nil + + defp encode_level(level) do + level + |> maybe_from_struct() + |> Map.update(:mrr, "0", &to_string/1) + |> Map.update(:confidence, nil, &to_string/1) + end + + defp maybe_from_struct(%_{} = struct), do: Map.from_struct(struct) + defp maybe_from_struct(map) when is_map(map), do: map +end diff --git a/lib/towerops_web/live/alert_live/index.html.heex b/lib/towerops_web/live/alert_live/index.html.heex index c75f06af..81d7b442 100644 --- a/lib/towerops_web/live/alert_live/index.html.heex +++ b/lib/towerops_web/live/alert_live/index.html.heex @@ -107,6 +107,21 @@
{alert.message}
+ <%= if alert.gaiia_impact && alert.gaiia_impact["total_subscribers"] && alert.gaiia_impact["total_subscribers"] > 0 do %> +