-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathapp_session_live.ex
More file actions
554 lines (479 loc) · 17 KB
/
Copy pathapp_session_live.ex
File metadata and controls
554 lines (479 loc) · 17 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
defmodule LivebookWeb.AppSessionLive do
use LivebookWeb, :live_view
import LivebookWeb.AppComponents
alias Livebook.Session
alias Livebook.Notebook
alias Livebook.Notebook.Cell
@impl true
def mount(%{"slug" => slug, "id" => session_id}, _session, socket)
when socket.assigns.app_authenticated? do
{:ok, app} = Livebook.Apps.fetch_app(slug)
app_session = Enum.find(app.sessions, &(&1.id == session_id))
if app_session && app_session.app_status.lifecycle == :active do
%{pid: session_pid} = app_session
session = Session.get_by_pid(session_pid)
{data, client_id} =
if connected?(socket) do
{data, client_id} =
Session.register_client(session_pid, self(), socket.assigns.current_user)
Session.subscribe(session_id)
{data, client_id}
else
data = Session.get_data(session_pid)
{data, nil}
end
{:ok,
socket
|> assign(
slug: slug,
session: session,
page_title: get_page_title(data.notebook.name),
client_id: client_id,
data_view: data_to_view(data)
)
|> assign_private(data: data)
|> prune_outputs()}
else
{:ok,
assign(socket,
nonexistent?: true,
slug: slug,
page_title: get_page_title(app.notebook_name)
)}
end
end
def mount(%{"slug" => slug} = params, _session, socket) do
if connected?(socket) do
to =
if id = params["id"] do
~p"/apps/#{slug}/authenticate?id=#{id}"
else
~p"/apps/#{slug}/authenticate"
end
{:ok, push_navigate(socket, to: to)}
else
{:ok, socket}
end
end
# Puts the given assigns in `socket.private`,
# to ensure they are not used for rendering.
defp assign_private(socket, assigns) do
Enum.reduce(assigns, socket, fn {key, value}, socket ->
put_in(socket.private[key], value)
end)
end
@impl true
def render(%{nonexistent?: true} = assigns) when assigns.app_authenticated? do
~H"""
<div class="h-screen flex items-center justify-center">
<div class="flex flex-col space-y-4 items-center">
<a href={~p"/"}>
<img src={~p"/images/logo.png"} height="128" width="128" alt="livebook" />
</a>
<div class="text-2xl text-gray-800">
This app session does not exist
</div>
<div class="max-w-2xl text-center text-gray-700">
<span>Visit the</span>
<.link class="border-b border-gray-700 hover:border-none" navigate={~p"/apps/#{@slug}"}>app page</.link>.
</div>
</div>
</div>
"""
end
def render(assigns) when assigns.app_authenticated? do
~H"""
<div class="h-full relative overflow-y-auto px-4 md:px-20" data-el-notebook>
<div class="w-full max-w-screen-lg py-4 mx-auto" data-el-notebook-content>
<div class="absolute md:fixed right-4 md:left-4 md:right-auto top-3">
<.menu id="app-menu" position="bottom-right" md_position="bottom-left">
<:toggle>
<button class="flex items-center text-gray-900">
<img src={~p"/images/logo.png"} height="40" width="40" alt="logo livebook" />
<.remix_icon icon="arrow-down-s-line" />
</button>
</:toggle>
<.menu_item :if={@livebook_authenticated?}>
<.link navigate={~p"/"} role="menuitem">
<.remix_icon icon="home-6-line" />
<span>Home</span>
</.link>
</.menu_item>
<.menu_item>
<.link navigate={~p"/apps"} role="menuitem">
<.remix_icon icon="layout-grid-fill" />
<span>Apps</span>
</.link>
</.menu_item>
<.menu_item :if={@data_view.multi_session}>
<.link navigate={~p"/apps/#{@data_view.slug}"} role="menuitem">
<.remix_icon icon="play-list-add-line" />
<span>Sessions</span>
</.link>
</.menu_item>
<.menu_item :if={@data_view.show_source}>
<.link
patch={~p"/apps/#{@data_view.slug}/sessions/#{@session.id}/source"}
role="menuitem"
>
<.remix_icon icon="code-line" />
<span>View source</span>
</.link>
</.menu_item>
<.menu_item :if={@livebook_authenticated?}>
<.link patch={~p"/sessions/#{@session.id}"} role="menuitem">
<.remix_icon icon="terminal-line" />
<span>Debug</span>
</.link>
</.menu_item>
<.menu_item :if={Livebook.Config.logout_enabled?() and @current_user.email != nil}>
<button phx-click="logout" role="menuitem">
<.remix_icon icon="logout-box-line" />
<span>Logout</span>
</button>
</.menu_item>
</.menu>
</div>
<div data-el-js-view-iframes phx-update="ignore" id="js-view-iframes"></div>
<div class="flex items-center pb-4 mb-2 space-x-4 border-b border-gray-200 pr-20 md:pr-0">
<h1 class="text-3xl font-semibold text-gray-800">
{@data_view.notebook_name}
</h1>
</div>
<div class="pt-4 flex flex-col gap-6">
<.live_component
:for={cell_view <- @data_view.cell_views}
module={LivebookWeb.AppSessionLive.CellOutputsComponent}
id={"outputs-#{cell_view.id}"}
cell_view={cell_view}
session={@session}
client_id={@client_id}
/>
<%= if @data_view.app_status.execution == :error do %>
<div class={[
"flex justify-between items-center px-4 py-2 border-l-4 shadow-custom-1",
"text-red-400 border-red-400"
]}>
<div>
Something went wrong
</div>
<div class="flex items-center gap-6">
<span class="tooltip top" data-tooltip="Debug">
<.link
:if={@livebook_authenticated?}
navigate={~p"/sessions/#{@session.id}" <> "#cell-#{@data_view.errored_cell_id}"}
>
<.remix_icon icon="terminal-line" />
</.link>
</span>
<button
class="px-5 py-2 font-medium text-sm inline-flex rounded-lg border whitespace-nowrap items-center justify-center gap-1 border-red-400 text-red-400 hover:bg-red-50 focus:bg-red-50"
phx-click="queue_errored_cells_evaluation"
>
<.remix_icon icon="play-circle-fill" />
<span>Retry</span>
</button>
</div>
</div>
<% end %>
</div>
<div style="height: 80vh"></div>
</div>
<div class="fixed right-3 bottom-4 flex flex-col gap-2 items-center text-gray-600 w-10">
<span
:if={
@data_view.app_status.execution == :executed and
@data_view.any_stale?
}
class="tooltip left"
data-tooltip={
~S'''
Some inputs have changed.
Click this button to process with latest values.
'''
}
>
<.icon_button phx-click="queue_full_evaluation">
<.remix_icon icon="play-circle-fill" class="text-3xl leading-none" />
</.icon_button>
</span>
<.app_status_circle status={@data_view.app_status} />
</div>
</div>
<.modal
:if={@live_action == :source and @data_view.show_source}
id="source-modal"
show
width="big"
patch={~p"/apps/#{@data_view.slug}/sessions/#{@session.id}"}
>
<.live_component
module={LivebookWeb.AppSessionLive.SourceComponent}
id="source"
session={@session}
/>
</.modal>
"""
end
def render(assigns), do: auth_placeholder(assigns)
attr :status, :map, required: true
defp app_status_circle(%{status: %{lifecycle: :shutting_down}} = assigns) do
~H"""
<.app_status_indicator text="Shutting down" variant="inactive" icon="stop-line" />
"""
end
defp app_status_circle(%{status: %{lifecycle: :deactivated}} = assigns) do
~H"""
<.app_status_indicator text="Deactivated" variant="inactive" icon="stop-line" />
"""
end
defp app_status_circle(%{status: %{execution: :executing}} = assigns) do
~H"""
<.app_status_indicator text="Executing" variant="progressing" icon="loader-3-line" spinning />
"""
end
defp app_status_circle(%{status: %{execution: :executed}} = assigns) do
~H"""
<.app_status_indicator text="Executed" variant="success" icon="check-line" />
"""
end
defp app_status_circle(%{status: %{execution: :error}} = assigns) do
~H"""
<.app_status_indicator text="Error" variant="error" icon="close-line" />
"""
end
defp app_status_circle(%{status: %{execution: :interrupted}} = assigns) do
~H"""
<.app_status_indicator text="Interrupted" variant="waiting" icon="pause-line" />
"""
end
attr :text, :string, required: true
attr :variant, :string, required: true
attr :icon, :string, required: true
attr :spinning, :boolean, default: false
defp app_status_indicator(assigns) do
~H"""
<span class="tooltip left" data-tooltip={@text}>
<span class={[
status_circle_class(@variant),
"opacity-75",
"w-6 h-6 rounded-full flex items-center justify-center",
@spinning && "animate-spin"
]}>
<.remix_icon icon={@icon} class="text-white font-bold" />
</span>
</span>
"""
end
defp get_page_title(notebook_name) do
"Livebook - #{notebook_name}"
end
@impl true
def handle_params(_params, _url, socket), do: {:noreply, socket}
@impl true
def handle_event("queue_interrupted_cell_evaluation", %{"cell_id" => cell_id}, socket) do
data = socket.private.data
with {:ok, cell, _section} <- Notebook.fetch_cell_and_section(data.notebook, cell_id),
true <- data.cell_infos[cell.id].eval.interrupted do
Session.queue_full_evaluation(socket.assigns.session.pid, [cell_id])
end
{:noreply, socket}
end
def handle_event("queue_errored_cells_evaluation", %{}, socket) do
data = socket.private.data
errored_cell_ids =
for {cell_id, %{eval: eval_info}} <- data.cell_infos, eval_info.errored, do: cell_id
Session.queue_full_evaluation(socket.assigns.session.pid, errored_cell_ids)
{:noreply, socket}
end
def handle_event("queue_full_evaluation", %{}, socket) do
Session.queue_full_evaluation(socket.assigns.session.pid, [])
{:noreply, socket}
end
@impl true
def handle_call({:get_input_value, input_id}, _from, socket) do
reply =
case socket.private.data.input_infos do
%{^input_id => %{value: value}} -> {:ok, socket.assigns.session.id, value}
%{} -> :error
end
{:reply, reply, socket}
end
@impl true
def handle_info({:operation, operation}, socket) do
{:noreply, handle_operation(socket, operation)}
end
def handle_info({:set_input_values, values, local}, socket) do
if local do
socket =
Enum.reduce(values, socket, fn {input_id, value}, socket ->
operation = {:set_input_value, socket.assigns.client_id, input_id, value}
handle_operation(socket, operation)
end)
{:noreply, socket}
else
for {input_id, value} <- values do
Session.set_input_value(socket.assigns.session.pid, input_id, value)
end
{:noreply, socket}
end
end
def handle_info(:session_closed, socket) do
{:noreply, redirect_on_closed(socket)}
end
def handle_info(_message, socket), do: {:noreply, socket}
defp handle_operation(socket, operation) do
case Session.Data.apply_operation(socket.private.data, operation) do
{:ok, data, _actions} ->
socket
|> assign_private(data: data)
|> assign(
data_view:
update_data_view(socket.assigns.data_view, socket.private.data, data, operation)
)
|> after_operation(socket, operation)
:error ->
socket
end
end
defp after_operation(
socket,
_prev_socket,
{:add_cell_evaluation_output, _client_id, _cell_id, _output}
) do
prune_outputs(socket)
end
defp after_operation(
socket,
_prev_socket,
{:add_cell_evaluation_response, _client_id, _cell_id, _output, _metadata}
) do
prune_outputs(socket)
end
defp after_operation(socket, _prev_socket, {:app_deactivate, _client_id}) do
redirect_on_closed(socket)
end
defp after_operation(socket, _prev_socket, {:app_shutdown, _client_id}) do
put_flash(
socket,
:info,
"A new version has been deployed, this session will close once everybody leaves"
)
end
defp after_operation(socket, _prev_socket, _operation), do: socket
defp redirect_on_closed(socket) do
socket
|> put_flash(:info, "Session has been closed")
|> push_navigate(to: ~p"/")
end
defp update_data_view(data_view, prev_data, data, operation) do
case operation do
# See LivebookWeb.SessionLive for more details
{:add_cell_evaluation_output, _client_id, cell_id, output} ->
case LivebookWeb.SessionLive.send_output_update(prev_data, data, cell_id, output) do
:ok -> data_view
:continue -> data_to_view(data)
end
_ ->
data_to_view(data)
end
end
defp prune_outputs(%{private: %{data: data}} = socket) do
assign_private(
socket,
data: update_in(data.notebook, &Notebook.prune_cell_outputs/1)
)
end
defp data_to_view(data) do
changed_input_ids = Session.Data.changed_input_ids(data)
shall_render = fn cell ->
if data.notebook.app_settings.render_static do
Cell.evaluable?(cell) or Cell.static?(cell)
else
Cell.evaluable?(cell)
end
end
cell_views =
data.notebook
|> Notebook.cells_with_section()
|> Enum.filter(fn {cell, _section} -> shall_render.(cell) end)
|> Enum.map(fn
{%Livebook.Notebook.Cell.Markdown{} = cell, _section} ->
out_id = :rand.uniform(31337)
output = {out_id, %{type: :markdown_static, text: cell.source, chunk: false}}
%{
id: cell.id,
input_views: [],
outputs: [output],
outputs_batch_number: 0
}
{cell, _section} ->
%{
id: cell.id,
input_views: input_views_for_cell(cell, data, changed_input_ids),
outputs: filter_outputs(cell.outputs, data.notebook.app_settings.output_type),
outputs_batch_number: data.cell_infos[cell.id].eval.outputs_batch_number
}
end)
%{
notebook_name: data.notebook.name,
cell_views: cell_views,
app_status: data.app_data.status,
show_source: data.notebook.app_settings.show_source,
slug: data.notebook.app_settings.slug,
multi_session: data.notebook.app_settings.multi_session,
errored_cell_id: errored_cell_id(data),
any_stale?: any_stale?(data)
}
end
defp errored_cell_id(data) do
data.notebook
|> Notebook.evaluable_cells_with_section()
|> Enum.find_value(fn {cell, _section} ->
data.cell_infos[cell.id].eval.errored && cell.id
end)
end
defp any_stale?(data) do
Enum.any?(data.cell_infos, &match?({_, %{eval: %{validity: :stale}}}, &1))
end
defp input_views_for_cell(cell, data, changed_input_ids) do
input_ids =
for output <- cell.outputs,
input <- Cell.find_inputs_in_output(output),
do: input.id
data.input_infos
|> Map.take(input_ids)
|> Map.new(fn {input_id, %{value: value}} ->
{input_id, %{value: value, changed: MapSet.member?(changed_input_ids, input_id)}}
end)
end
defp filter_outputs(outputs, :all), do: outputs
defp filter_outputs(outputs, :rich), do: rich_outputs(outputs)
defp rich_outputs(outputs) do
for output <- outputs, output = filter_output(output), do: output
end
defp filter_output({idx, output})
when output.type in [:plain_text, :markdown, :image, :js, :control, :input],
do: {idx, output}
defp filter_output({idx, %{type: :tabs} = output}) do
outputs_with_labels =
for {output, label} <- Enum.zip(output.outputs, output.labels),
output = filter_output(output),
do: {output, label}
{outputs, labels} = Enum.unzip(outputs_with_labels)
{idx, %{output | outputs: outputs, labels: labels}}
end
defp filter_output({idx, %{type: :grid} = output}) do
outputs = rich_outputs(output.outputs)
if outputs != [] do
{idx, %{output | outputs: outputs}}
end
end
defp filter_output({idx, %{type: :frame} = output}) do
outputs = rich_outputs(output.outputs)
{idx, %{output | outputs: outputs}}
end
defp filter_output({idx, %{type: :error, context: {:interrupt, _, _}} = output}),
do: {idx, output}
defp filter_output(_output), do: nil
end