Performance: - schedule_live: preload page once instead of get_schedule!/1 per row - alert_live + alerts: DB-side status filter; Repo.aggregate counts replace length/Enum.count over 500-row fetches on every event - dashboard_live: drop duplicate get_device_status_counts; cap active alerts to 20 + use count_active_alerts/1 - maintenance.active_windows_for_device: 3 round-trips collapsed into one query (per-site-id branch keeps Postgres parameter types unambiguous) - sites.build_site_tree: O(N^2) -> O(N) via group_by(parent_site_id) - accounts.sole_owner_organizations: single group_by + having instead of per-org Repo.aggregate loop - agents + agent_live (org + admin): count_assigned_devices_batch/1 + count_agent_polling_targets/1 (no preloads) - alert_digest_worker: list_alerts_by_ids/1 batches digest fetch - gaiia: distinct: true at DB; limit 50 on bidirectional ilike Indexes: - maintenance_windows(organization_id, starts_at, ends_at) WHERE suppress_alerts = true - alerts(check_id) WHERE resolved_at IS NULL Antipatterns: - agents.delete_agent_token: PubSub.broadcast moved outside Repo.transaction so a rollback no longer leaves subscribers acting on a non-existent deletion - integrations_controller.to_atom_keys: replaced String.to_existing_atom on user-controlled JSON keys with explicit allowlist - 9 Task.start callsites converted to Task.Supervisor.start_child(Towerops.TaskSupervisor, ...) so background DB writes survive shutdown (test-mode discovery shims left as-is for sandbox semantics)
60 lines
1.7 KiB
Elixir
60 lines
1.7 KiB
Elixir
defmodule ToweropsWeb.GraphQLSocket do
|
|
@moduledoc """
|
|
WebSocket endpoint for GraphQL subscriptions.
|
|
|
|
Authenticates via either:
|
|
- API token (prefixed with "towerops_") → sets organization_id + user in context
|
|
- Mobile session token → sets user only (no organization_id)
|
|
"""
|
|
|
|
use Phoenix.Socket
|
|
use Absinthe.Phoenix.Socket, schema: ToweropsWeb.GraphQL.Schema
|
|
|
|
alias Absinthe.Phoenix.Socket
|
|
alias Towerops.Accounts
|
|
alias Towerops.ApiTokens
|
|
alias Towerops.MobileSessions
|
|
|
|
@impl true
|
|
def connect(%{"token" => "towerops_" <> _ = token}, socket, _connect_info) do
|
|
case ApiTokens.verify_token(token) do
|
|
{:ok, org_id, user} ->
|
|
socket =
|
|
socket
|
|
|> assign(:organization_id, org_id)
|
|
|> assign(:user, user)
|
|
|> Socket.put_options(context: %{organization_id: org_id, user: user})
|
|
|
|
{:ok, socket}
|
|
|
|
{:error, :invalid_token} ->
|
|
:error
|
|
end
|
|
end
|
|
|
|
def connect(%{"token" => token}, socket, _connect_info) when is_binary(token) do
|
|
with session when not is_nil(session) <- MobileSessions.get_session_by_token(token),
|
|
user when not is_nil(user) <- Accounts.get_user(session.user_id) do
|
|
_ =
|
|
Task.Supervisor.start_child(Towerops.TaskSupervisor, fn ->
|
|
MobileSessions.touch_session(session)
|
|
end)
|
|
|
|
socket =
|
|
socket
|
|
|> assign(:user, user)
|
|
|> Socket.put_options(context: %{user: user})
|
|
|
|
{:ok, socket}
|
|
else
|
|
_ -> :error
|
|
end
|
|
end
|
|
|
|
def connect(_params, _socket, _connect_info), do: :error
|
|
|
|
@impl true
|
|
def id(%{assigns: %{organization_id: org_id}}), do: "graphql_socket:#{org_id}"
|
|
def id(%{assigns: %{user: user}}), do: "graphql_socket:user:#{user.id}"
|
|
def id(_socket), do: nil
|
|
end
|