From 628495fc76d780b651037a25eae78d5b1c44c9ac Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sat, 12 Jul 2025 08:02:51 -0500 Subject: [PATCH] badpackets formatting --- .../live/bad_packets_live/index.html.heex | 161 +++++++++--------- lib/aprsme_web/plugs/ip_geolocation.ex | 11 +- .../aprsme_web/live/bad_packets_live_test.exs | 76 +++++++++ 3 files changed, 166 insertions(+), 82 deletions(-) create mode 100644 test/aprsme_web/live/bad_packets_live_test.exs diff --git a/lib/aprsme_web/live/bad_packets_live/index.html.heex b/lib/aprsme_web/live/bad_packets_live/index.html.heex index 1277bdb..042c058 100644 --- a/lib/aprsme_web/live/bad_packets_live/index.html.heex +++ b/lib/aprsme_web/live/bad_packets_live/index.html.heex @@ -1,88 +1,91 @@ -
-
- -
+
+
+
+
+ + {gettext("Auto-refreshes every 5 seconds")} + + <%= if @loading do %> + + + {gettext("Updating...")} + + <% end %> +
-
-
- - {gettext("Auto-refreshes every 5 seconds")} - - <%= if @loading do %> - - - - - - - - {gettext("Updating...")} - - <% end %> -
- <.table id="bad_packets" rows={@bad_packets}> - <:col :let={bad_packet} label={gettext("Attempted At")}> - - {Calendar.strftime( - bad_packet.attempted_at || bad_packet.inserted_at, - "%Y-%m-%d %H:%M:%S UTC" - )} - - - <:col :let={bad_packet} label={gettext("Error Type")}> - - {bad_packet.error_type || gettext("Unknown")} - - - <:col :let={bad_packet} label={gettext("Error Message")}> - - {bad_packet.error_message || gettext("No error message")} - - - <:col :let={bad_packet} label={gettext("Raw Packet")}> -
-
- <%= if is_binary(bad_packet.raw_packet) do %> - {Aprsme.EncodingUtils.sanitize_string(bad_packet.raw_packet)} - <% else %> - {bad_packet.raw_packet} +
+ + + + + + + + + + + <%= for bad_packet <- @bad_packets do %> + + + + + + <% end %> - - - - + +
{gettext("Attempted At")}{gettext("Error Type")}{gettext("Error Message")}{gettext("Raw Packet")}
+ + {Calendar.strftime( + bad_packet.attempted_at || bad_packet.inserted_at, + "%Y-%m-%d %H:%M:%S UTC" + )} + + +
+ {bad_packet.error_type || gettext("Unknown")} +
+
+ + {bad_packet.error_message || gettext("No error message")} + + +
+
+ <%= if is_binary(bad_packet.raw_packet) do %> + {Aprsme.EncodingUtils.sanitize_string(bad_packet.raw_packet)} + <% else %> + {bad_packet.raw_packet} + <% end %> +
+
+
+
+
<%= if length(@bad_packets) == 0 do %> -
-
- - - +
+
+
+
+ + + +
+
+

{gettext("No bad packets")}

+

{gettext("All packets are parsing successfully!")}

-

{gettext("No bad packets")}

-

{gettext("All packets are parsing successfully!")}

<% end %>
diff --git a/lib/aprsme_web/plugs/ip_geolocation.ex b/lib/aprsme_web/plugs/ip_geolocation.ex index 05bb036..f88f790 100644 --- a/lib/aprsme_web/plugs/ip_geolocation.ex +++ b/lib/aprsme_web/plugs/ip_geolocation.ex @@ -88,12 +88,17 @@ defmodule AprsmeWeb.Plugs.IPGeolocation do # Fall back to remote_ip ip_from_remote = case conn.remote_ip do - {a, b, c, d} -> "#{a}.#{b}.#{c}.#{d}" + {a, b, c, d} -> + "#{a}.#{b}.#{c}.#{d}" + {a, b, c, d, e, f, g, h} -> # Convert IPv6 tuple to proper IPv6 format - :inet.ntoa({a, b, c, d, e, f, g, h}) + {a, b, c, d, e, f, g, h} + |> :inet.ntoa() |> to_string() - _ -> nil + + _ -> + nil end if ip_from_remote do diff --git a/test/aprsme_web/live/bad_packets_live_test.exs b/test/aprsme_web/live/bad_packets_live_test.exs new file mode 100644 index 0000000..65c915a --- /dev/null +++ b/test/aprsme_web/live/bad_packets_live_test.exs @@ -0,0 +1,76 @@ +defmodule AprsmeWeb.BadPacketsLiveTest do + use AprsmeWeb.ConnCase + + import Phoenix.LiveViewTest + + alias Aprsme.BadPacket + alias Aprsme.Repo + + describe "Index" do + test "renders bad packets page with DaisyUI card", %{conn: conn} do + {:ok, _index_live, html} = live(conn, ~p"/badpackets") + + assert html =~ "card" + assert html =~ "bg-base-100" + assert html =~ "shadow-xl" + end + + test "lists all bad packets with DaisyUI table", %{conn: conn} do + bad_packet = + Repo.insert!(%BadPacket{ + raw_packet: "KD9PDP>APRS:Invalid packet data", + error_message: "Failed to parse APRS data", + error_type: "parse_error", + attempted_at: DateTime.utc_now() + }) + + {:ok, _index_live, html} = live(conn, ~p"/badpackets") + + assert html =~ bad_packet.error_message + assert html =~ bad_packet.error_type + assert html =~ "table" + assert html =~ "badge" + end + + test "displays empty state with DaisyUI components when no bad packets", %{conn: conn} do + {:ok, _index_live, html} = live(conn, ~p"/badpackets") + + assert html =~ "No bad packets" + assert html =~ "All packets are parsing successfully!" + assert html =~ "text-success" + end + + test "updates in real-time when new bad packet is created", %{conn: conn} do + {:ok, index_live, _html} = live(conn, ~p"/badpackets") + + # Create a bad packet after the live view is loaded + bad_packet = + Repo.insert!(%BadPacket{ + raw_packet: "KD9PDP>APRS:New invalid packet", + error_message: "New parse error", + error_type: "validation_error", + attempted_at: DateTime.utc_now() + }) + + # The LiveView listens to postgres notifications, so we need to trigger a refresh + # by sending the :do_refresh message directly + send(index_live.pid, :do_refresh) + + # Wait for the async update + :timer.sleep(100) + + html = render(index_live) + assert html =~ bad_packet.error_message + assert html =~ bad_packet.error_type + end + + test "works correctly in dark mode", %{conn: conn} do + {:ok, _index_live, html} = live(conn, ~p"/badpackets") + + # DaisyUI uses data-theme for theming + # The components should work with both light and dark themes + assert html =~ "bg-base-100" + assert html =~ "text-base-content" + end + end +end