Some checks failed
Build and Push / Build and Push Docker Image (push) Failing after 4m52s
- Switch polylines and circle markers to shared L.canvas() renderer, eliminating ~58k SVG DOM nodes in favor of 1 GPU-composited canvas - Add server-side callsign/date filtering to /api/contacts/map endpoint to reduce payload size when filters are active - Add early-exit fast path in applyCallsignFilter when no filter active - Batch rebuildDots with requestAnimationFrame to avoid redundant work during rapid filter changes
84 lines
2.6 KiB
Elixir
84 lines
2.6 KiB
Elixir
defmodule MicrowavepropWeb.ContactMapController do
|
|
@moduledoc """
|
|
Serves the contact map JSON payload as a standalone HTTP endpoint. Keeps
|
|
the ~5 MB blob out of the `/contacts/map` LiveView's initial HTML response
|
|
and lets the browser's native `Content-Encoding: gzip` handling do the
|
|
compression. The payload itself is served from `Microwaveprop.Cache` via
|
|
`Radio.contact_map_payload/0`.
|
|
"""
|
|
use MicrowavepropWeb, :controller
|
|
|
|
alias Microwaveprop.Cache
|
|
alias Microwaveprop.Radio
|
|
alias MicrowavepropWeb.Api.RateLimiter
|
|
|
|
plug RateLimiter, anon_limit: 10, auth_limit: 60
|
|
|
|
@cache_key {__MODULE__, :gzipped_payload}
|
|
@cache_ttl_ms 10 * 60 * 1_000
|
|
|
|
@doc false
|
|
@spec cache_key() :: {module(), :gzipped_payload}
|
|
def cache_key, do: @cache_key
|
|
|
|
@spec show(Plug.Conn.t(), map()) :: Plug.Conn.t()
|
|
def show(conn, params) do
|
|
filters = extract_filters(params)
|
|
{body, encoding} = gzipped_or_plain_body(conn, filters)
|
|
|
|
conn
|
|
|> put_resp_content_type("application/json")
|
|
|> maybe_put_encoding(encoding)
|
|
|> put_resp_header("cache-control", "public, max-age=60")
|
|
|> send_resp(200, body)
|
|
end
|
|
|
|
defp extract_filters(params) do
|
|
[]
|
|
|> maybe_add_filter(:callsign, params["callsign"])
|
|
|> maybe_add_filter(:start_date, params["start_date"])
|
|
|> maybe_add_filter(:end_date, params["end_date"])
|
|
end
|
|
|
|
defp maybe_add_filter(filters, _key, nil), do: filters
|
|
defp maybe_add_filter(filters, _key, ""), do: filters
|
|
defp maybe_add_filter(filters, key, value), do: [{key, value} | filters]
|
|
|
|
defp gzipped_or_plain_body(conn, [] = filters) do
|
|
if accepts_gzip?(conn) do
|
|
{gzipped_payload(), :gzip}
|
|
else
|
|
%{json: iodata} = Radio.contact_map_payload(filters)
|
|
{IO.iodata_to_binary(iodata), :identity}
|
|
end
|
|
end
|
|
|
|
defp gzipped_or_plain_body(conn, filters) do
|
|
# Filtered requests vary by parameter — don't cache the gzip blob.
|
|
%{json: iodata} = Radio.contact_map_payload(filters)
|
|
body = IO.iodata_to_binary(iodata)
|
|
|
|
if accepts_gzip?(conn) do
|
|
{:zlib.gzip(body), :gzip}
|
|
else
|
|
{body, :identity}
|
|
end
|
|
end
|
|
|
|
defp gzipped_payload do
|
|
Cache.fetch_or_store(@cache_key, @cache_ttl_ms, fn ->
|
|
%{json: iodata} = Radio.contact_map_payload([])
|
|
:zlib.gzip(IO.iodata_to_binary(iodata))
|
|
end)
|
|
end
|
|
|
|
defp accepts_gzip?(conn) do
|
|
case get_req_header(conn, "accept-encoding") do
|
|
[value | _] -> String.contains?(value, "gzip")
|
|
_ -> false
|
|
end
|
|
end
|
|
|
|
defp maybe_put_encoding(conn, :gzip), do: put_resp_header(conn, "content-encoding", "gzip")
|
|
defp maybe_put_encoding(conn, :identity), do: conn
|
|
end
|