Add covering index for map viewport query (6s -> 25ms)

This commit is contained in:
Graham McIntire 2026-03-31 09:36:36 -05:00
parent d5140267b1
commit e3c9207f57
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59
3 changed files with 35 additions and 6 deletions

View file

@ -0,0 +1,14 @@
defmodule Microwaveprop.Repo.Migrations.AddPropagationScoresViewportIndex do
use Ecto.Migration
def change do
# Covering index for the map viewport query:
# SELECT lat, lon, score WHERE band_mhz = X AND valid_time = Y
# AND lat BETWEEN a AND b AND lon BETWEEN c AND d
# INCLUDE (score) avoids heap lookups — index-only scan
create index(:propagation_scores, [:band_mhz, :valid_time, :lat, :lon],
include: [:score],
name: :propagation_scores_viewport_idx
)
end
end

View file

@ -115,6 +115,8 @@ end
defmodule Microwaveprop.Terrain.ElevationClientSrtmTest do
use ExUnit.Case, async: false
import ExUnit.CaptureLog
alias Microwaveprop.Terrain.ElevationClient
@tiles_dir Path.expand("~/srtm/tiles")
@ -153,9 +155,12 @@ defmodule Microwaveprop.Terrain.ElevationClientSrtmTest do
Req.Test.json(conn, %{"elevation" => [200.0, 250.0, 180.0]})
end)
assert {:ok, profile} =
ElevationClient.fetch_elevation_profile(32.9, -97.0, 30.3, -97.7, 2)
{result, _log} =
with_log(fn ->
ElevationClient.fetch_elevation_profile(32.9, -97.0, 30.3, -97.7, 2)
end)
assert {:ok, profile} = result
assert length(profile) == 3
assert hd(profile).elev == 200.0
end

View file

@ -1,6 +1,8 @@
defmodule Microwaveprop.Terrain.SrtmTest do
use ExUnit.Case, async: true
import ExUnit.CaptureLog
alias Microwaveprop.Terrain.Srtm
@tiles_dir Path.expand("~/srtm/tiles")
@ -62,7 +64,9 @@ defmodule Microwaveprop.Terrain.SrtmTest do
Plug.Conn.send_resp(conn, 404, "Not Found")
end)
assert {:error, :not_available} = Srtm.download_tile(80.0, 0.0, tmp_dir)
capture_log(fn ->
assert {:error, :not_available} = Srtm.download_tile(80.0, 0.0, tmp_dir)
end)
end
test "returns error message on other HTTP status" do
@ -75,7 +79,9 @@ defmodule Microwaveprop.Terrain.SrtmTest do
Plug.Conn.send_resp(conn, 500, "Internal Server Error")
end)
assert {:error, "SRTM download HTTP 500"} = Srtm.download_tile(32.0, -97.0, tmp_dir)
capture_log(fn ->
assert {:error, "SRTM download HTTP 500"} = Srtm.download_tile(32.0, -97.0, tmp_dir)
end)
end
end
@ -105,7 +111,9 @@ defmodule Microwaveprop.Terrain.SrtmTest do
Plug.Conn.send_resp(conn, 404, "Not Found")
end)
assert {:error, :no_tile} = Srtm.lookup(80.0, 0.0, tmp_dir)
capture_log(fn ->
assert {:error, :no_tile} = Srtm.lookup(80.0, 0.0, tmp_dir)
end)
end
end
@ -127,7 +135,9 @@ defmodule Microwaveprop.Terrain.SrtmTest do
Plug.Conn.send_resp(conn, 404, "Not Found")
end)
assert {:error, :no_tile} = Srtm.lookup(80.0, 0.0, @tiles_dir)
capture_log(fn ->
assert {:error, :no_tile} = Srtm.lookup(80.0, 0.0, @tiles_dir)
end)
end
end