Skip to content

Commit 41d8482

Browse files
committed
Add latency status
1 parent d55f212 commit 41d8482

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
defmodule ComponentsGuideWeb.LatencyStatusLive do
2+
use ComponentsGuideWeb,
3+
{:live_view, container: {:div, class: "max-w-6xl mx-auto text-lg text-white pb-24"}}
4+
5+
alias ComponentsGuide.Fetch
6+
7+
defmodule State do
8+
defstruct responses: nil
9+
10+
def default() do
11+
%__MODULE__{}
12+
end
13+
14+
def add_responses(
15+
%__MODULE__{} = state,
16+
responses = [%Fetch.Response{} | _]
17+
) do
18+
%__MODULE__{
19+
state
20+
| responses: responses
21+
}
22+
end
23+
end
24+
25+
@impl true
26+
def render(assigns) do
27+
~H"""
28+
<.form
29+
let={f}
30+
for={:editor}
31+
id="view_source_form"
32+
phx-submit="submitted"
33+
class="max-w-2xl mt-12 mx-auto space-y-2"
34+
>
35+
<div class="flex justify-center">
36+
<button type="submit" class="px-3 py-1 text-xl text-blue-900 bg-blue-200 rounded" autofocus>Load</button>
37+
</div>
38+
</.form>
39+
40+
<output form="view_source_form" class="flex flex-col gap-4 pt-4 max-w-none text-center">
41+
<%= for response <- @state.responses || [], response != nil do %>
42+
<div class="p-4 text-white bg-black font-mono">
43+
<pre class="bg-transparent"><%= response.url %></pre>
44+
<p>
45+
<span class="text-green-100"><%= response.status %></span>
46+
in <data class="text-lg text-sky-200"><%= System.convert_time_unit(response.timings.duration, :native, :millisecond) %>ms</data>
47+
</p>
48+
</div>
49+
<% end %>
50+
</output>
51+
"""
52+
end
53+
54+
defp assign_state(socket, state) do
55+
assign(socket, state: state)
56+
end
57+
58+
@impl true
59+
def mount(%{}, _session, socket) do
60+
state = State.default()
61+
socket = assign_state(socket, state)
62+
{:ok, socket}
63+
end
64+
65+
@impl true
66+
def handle_event("submitted", _form_values, socket) do
67+
urls = [
68+
"https://workers.cloudflare.com/cf.json",
69+
"https://api.github.com/rate_limit",
70+
"https://unpkg.com/robots.txt"
71+
]
72+
73+
responses =
74+
for url <- urls do
75+
req = Fetch.Request.new!(url)
76+
Fetch.load!(req)
77+
end
78+
79+
# redis_timings = Fetch.Timings.start()
80+
# {duration_microseconds, _result} =
81+
# :timer.tc(fn ->
82+
# case Redix.command(:redix_cache, ["GET", "whatever"]) do
83+
# {:ok, value} -> value
84+
# _ -> nil
85+
# end
86+
# end)
87+
# redis_timings = Fetch.Timings.finish(redis_timings)
88+
# redis_res = Fetch.Response.new("redis:") |> Fetch.Response.add_timings(redis_timings)
89+
# responses = [redis_res | responses]
90+
91+
state = socket.assigns.state |> State.add_responses(responses)
92+
socket = socket |> assign_state(state)
93+
{:noreply, socket}
94+
end
95+
end

lib/components_guide_web/router.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ defmodule ComponentsGuideWeb.Router do
7171

7272
live("/view-source", ViewSourceLive)
7373
live("/view-github-repo", ViewGithubRepoLive)
74+
7475
live("/latency-calculator", LatencyCalculatorLive)
76+
live("/latency-status", LatencyStatusLive)
7577

7678
live("/color", ColorLive, :index)
7779
live("/color/:definition", ColorLive, :show)

0 commit comments

Comments
 (0)