-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtraces_list.ex
337 lines (298 loc) · 9.63 KB
/
traces_list.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
defmodule LiveDebugger.LiveComponents.TracesList do
@moduledoc """
This module provides a LiveComponent to display traces.
"""
use LiveDebuggerWeb, :live_component
require Logger
alias LiveDebugger.LiveHelpers.TracingHelper
alias LiveDebugger.Structs.Trace
alias LiveDebugger.Components.ElixirDisplay
alias LiveDebugger.Services.TraceService
alias LiveDebugger.Utils.TermParser
alias LiveDebugger.Utils.Parsers
alias LiveDebugger.Structs.TraceDisplay
@stream_limit 128
@impl true
def mount(socket) do
socket
|> assign(:displayed_trace, nil)
|> ok()
end
@impl true
def update(%{new_trace: trace}, socket) do
socket
|> TracingHelper.check_fuse()
|> case do
{:ok, socket} ->
trace_display = TraceDisplay.from_live_trace(trace)
stream_insert(socket, :existing_traces, trace_display, at: 0, limit: @stream_limit)
{_, socket} ->
# Add disappearing flash here in case of :stopped. (Issue 173)
socket
end
|> ok()
end
def update(assigns, socket) do
socket
|> TracingHelper.init()
|> assign(debugged_node_id: assigns.debugged_node_id)
|> assign(id: assigns.id)
|> assign(ets_table_id: TraceService.ets_table_id(assigns.socket_id))
|> assign_async_existing_traces()
|> ok()
end
attr(:id, :string, required: true)
attr(:debugged_node_id, :map, required: true)
attr(:socket_id, :string, required: true)
@impl true
def render(assigns) do
~H"""
<div class="max-w-full">
<.collapsible_section title="Callback traces" id="traces" inner_class="p-4">
<:right_panel>
<div class="flex gap-2 items-center">
<.toggle_tracing_button
myself={@myself}
tracing_started?={@tracing_helper.tracing_started?}
/>
<.button
:if={not @tracing_helper.tracing_started?}
phx-click="refresh-history"
phx-target={@myself}
class="flex gap-2"
variant="secondary"
size="sm"
>
Refresh
</.button>
<.button
:if={not @tracing_helper.tracing_started?}
variant="secondary"
size="sm"
phx-click="clear-traces"
phx-target={@myself}
>
Clear
</.button>
</div>
</:right_panel>
<div class="w-full h-full lg:min-h-[10.25rem]">
<div id={"#{assigns.id}-stream"} phx-update="stream" class="flex flex-col gap-2">
<div id={"#{assigns.id}-stream-empty"} class="only:block hidden text-gray-700">
<div :if={@existing_traces_status == :ok}>
No traces have been recorded yet.
</div>
<div
:if={@existing_traces_status == :loading}
class="w-full flex items-center justify-center"
>
<.spinner size="sm" />
</div>
<.alert
:if={@existing_traces_status == :error}
variant="danger"
with_icon
heading="Error fetching historical callback traces"
>
New events will still be displayed as they come. Check logs for more information
</.alert>
</div>
<%= for {dom_id, wrapped_trace} <- @streams.existing_traces do %>
<.trace id={dom_id} wrapped_trace={wrapped_trace} myself={@myself} />
<% end %>
</div>
</div>
</.collapsible_section>
<.trace_fullscreen id="trace-fullscreen" trace={@displayed_trace} />
</div>
"""
end
@impl true
def handle_async(:fetch_existing_traces, {:ok, trace_list}, socket) do
trace_list = Enum.map(trace_list, &TraceDisplay.from_historical_trace/1)
socket
|> assign(existing_traces_status: :ok)
|> stream(:existing_traces, trace_list, limit: @stream_limit)
|> noreply()
end
def handle_async(:fetch_existing_traces, {:exit, reason}, socket) do
Logger.error(
"LiveDebugger encountered unexpected error while fetching existing traces: #{inspect(reason)}"
)
socket
|> assign(existing_traces_status: :error)
|> noreply()
end
@impl true
def handle_event("switch-tracing", _, socket) do
socket
|> TracingHelper.switch_tracing()
|> noreply()
end
@impl true
def handle_event("clear-traces", _, socket) do
ets_table_id = socket.assigns.ets_table_id
node_id = socket.assigns.debugged_node_id
TraceService.clear_traces(ets_table_id, node_id)
socket
|> stream(:existing_traces, [], reset: true)
|> noreply()
end
@impl true
def handle_event("open-trace", %{"data" => string_id}, socket) do
trace_id = String.to_integer(string_id)
socket.assigns.ets_table_id
|> TraceService.get(trace_id)
|> case do
nil ->
socket
trace ->
socket
|> assign(displayed_trace: trace)
|> push_event("trace-fullscreen-open", %{})
end
|> noreply()
end
@impl true
def handle_event("toggle-collapsible", %{"trace-id" => string_trace_id}, socket) do
trace_id = String.to_integer(string_trace_id)
socket.assigns.ets_table_id
|> TraceService.get(trace_id)
|> case do
nil ->
socket
trace ->
socket
|> stream_insert(
:existing_traces,
TraceDisplay.from_historical_trace(trace),
at: abs(trace.id),
limit: @stream_limit
)
end
|> noreply()
end
@impl true
def handle_event("refresh-history", _, socket) do
socket
|> assign_async_existing_traces()
|> noreply()
end
attr(:tracing_started?, :boolean, required: true)
attr(:myself, :any, required: true)
defp toggle_tracing_button(assigns) do
~H"""
<.button phx-click="switch-tracing" phx-target={@myself} class="flex gap-2" size="sm">
<div class="flex gap-1.5 items-center w-12">
<%= if @tracing_started? do %>
<.icon name="icon-stop" class="w-4 h-4" />
<div>Stop</div>
<% else %>
<.icon name="icon-play" class="w-3.5 h-3.5" />
<div>Start</div>
<% end %>
</div>
</.button>
"""
end
attr(:id, :string, required: true)
attr(:wrapped_trace, :map, required: true, doc: "The Trace to render")
attr(:myself, :any, required: true)
defp trace(assigns) do
assigns =
assigns
|> assign(:trace, assigns.wrapped_trace.trace)
|> assign(:render_body?, assigns.wrapped_trace.render_body?)
|> assign(:callback_name, Trace.callback_name(assigns.wrapped_trace.trace))
~H"""
<.collapsible
id={@id}
icon="icon-chevron-right"
chevron_class="w-5 h-5 text-primary-900"
class="max-w-full border border-secondary-200 rounded"
label_class="font-semibold bg-secondary-50 h-10 p-2"
phx-click={if(@render_body?, do: nil, else: "toggle-collapsible")}
phx-target={@myself}
phx-value-trace-id={@trace.id}
>
<:label>
<div
id={@id <> "-label"}
class="w-[90%] grow flex items-center ml-2 gap-1.5"
phx-update="ignore"
>
<p class="font-medium text-sm"><%= @callback_name %></p>
<.short_trace_content trace={@trace} />
<p class="w-max text-xs font-normal text-secondary-600 align-center">
<%= Parsers.parse_timestamp(@trace.timestamp) %>
</p>
</div>
</:label>
<div class="relative flex flex-col gap-4 overflow-x-auto max-w-full h-[30vh] max-h-max overflow-y-auto p-4">
<.fullscreen_button
id={"trace-fullscreen-#{@id}"}
class="absolute right-2 top-2"
on_click="open-trace"
on_click_target={@myself}
on_click_data={@trace.id}
/>
<%= if @render_body? do %>
<%= for {args, index} <- Enum.with_index(@trace.args) do %>
<ElixirDisplay.term
id={@id <> "-#{index}"}
node={TermParser.term_to_display_tree(args)}
level={1}
/>
<% end %>
<% end %>
</div>
</.collapsible>
"""
end
defp short_trace_content(assigns) do
assigns = assign(assigns, :content, Enum.map_join(assigns.trace.args, " ", &inspect/1))
~H"""
<div class="grow shrink text-secondary-600 font-code font-normal text-3xs truncate">
<p class="hide-on-open mt-0.5"><%= @content %></p>
</div>
"""
end
attr(:id, :string, required: true)
attr(:trace, :map, default: nil)
defp trace_fullscreen(assigns) do
assigns =
case assigns.trace do
nil ->
assigns
|> assign(:callback_name, "Unknown trace")
|> assign(:trace_args, [])
trace ->
assigns
|> assign(:callback_name, Trace.callback_name(trace))
|> assign(:trace_args, trace.args)
end
~H"""
<.fullscreen id={@id} title={@callback_name}>
<div class="w-full flex flex-col gap-4 items-start justify-center">
<%= for {args, index} <- Enum.with_index(@trace_args) do %>
<ElixirDisplay.term
id={@id <> "-#{index}-fullscreen"}
node={TermParser.term_to_display_tree(args)}
level={1}
/>
<% end %>
</div>
</.fullscreen>
"""
end
defp assign_async_existing_traces(socket) do
ets_table_id = socket.assigns.ets_table_id
node_id = socket.assigns.debugged_node_id
socket
|> assign(:existing_traces_status, :loading)
|> stream(:existing_traces, [], reset: true)
|> start_async(:fetch_existing_traces, fn ->
TraceService.existing_traces(ets_table_id, node_id)
end)
end
end