towerops/lib/towerops_web/channels/mobile_socket.ex
Graham McIntire 701ce12f08 perf+refactor: codebase-wide query and antipattern audit
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)
2026-04-28 16:58:51 -05:00

52 lines
1.5 KiB
Elixir

defmodule ToweropsWeb.MobileSocket do
@moduledoc """
WebSocket endpoint for mobile app communication.
Mobile clients connect to: ws://server/mobile/socket/websocket
Authentication happens at connect time using a bearer token from
the mobile session system (QR code login flow).
The token is passed as a `"token"` parameter during the WebSocket
handshake. It is validated against the `mobile_sessions` table,
and expired sessions are rejected.
"""
use Phoenix.Socket
alias Towerops.MobileSessions
channel "mobile:org:*", ToweropsWeb.MobileChannel
channel "mobile:device:*", ToweropsWeb.MobileChannel
@impl true
@spec connect(map(), Phoenix.Socket.t(), map()) :: {:ok, Phoenix.Socket.t()} | :error
def connect(%{"token" => token}, socket, _connect_info) when is_binary(token) do
case MobileSessions.get_session_by_token(token) do
nil ->
:error
session ->
session = Towerops.Repo.preload(session, :user)
_ =
Task.Supervisor.start_child(Towerops.TaskSupervisor, fn ->
MobileSessions.touch_session(session)
end)
socket =
socket
|> assign(:user_id, session.user_id)
|> assign(:mobile_session_id, session.id)
{:ok, socket}
end
end
def connect(_params, _socket, _connect_info), do: :error
@impl true
@spec id(Phoenix.Socket.t()) :: String.t()
def id(socket) do
"mobile_socket:#{socket.assigns.user_id}"
end
end