forked from phoenixframework/phoenix_live_dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.exs
More file actions
454 lines (396 loc) · 12.2 KB
/
dev.exs
File metadata and controls
454 lines (396 loc) · 12.2 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
# iex -S mix dev
Logger.configure(level: :debug)
pg_url = System.get_env("PG_URL") || "postgres:postgres@127.0.0.1"
pg_database = System.get_env("PG_DATABASE") || "phx_dashboard_dev"
Application.put_env(:phoenix_live_dashboard, Demo.Repo, url: "ecto://#{pg_url}/#{pg_database}")
defmodule Demo.Repo do
use Ecto.Repo, otp_app: :phoenix_live_dashboard, adapter: Ecto.Adapters.Postgres
end
_ = Ecto.Adapters.Postgres.storage_up(Demo.Repo.config())
# Configures the endpoint
Application.put_env(:phoenix_live_dashboard, DemoWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "Hu4qQN3iKzTV4fJxhorPQlA/osH9fAMtbtjVS58PFgfw3ja5Z18Q/WSNR9wP4OfW",
live_view: [signing_salt: "hMegieSe"],
http: [port: System.get_env("PORT") || 4000],
debug_errors: true,
check_origin: false,
pubsub_server: Demo.PubSub,
watchers: [
node: [
"node_modules/webpack/bin/webpack.js",
"--mode",
System.get_env("NODE_ENV") || "production",
"--watch-stdin",
cd: "assets"
]
],
live_reload: [
patterns: [
~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
~r"lib/phoenix/live_dashboard/(live|views)/.*(ex)$",
~r"lib/phoenix/live_dashboard/templates/.*(ex)$"
]
]
)
defmodule DemoWeb.History do
use GenServer
@history_buffer_size 50
def data(metric) do
GenServer.call(__MODULE__, {:data, metric})
end
def start_link(args) do
GenServer.start_link(__MODULE__, args, name: __MODULE__)
end
@impl true
def init(metrics) do
Process.flag(:trap_exit, true)
metric_histories_map =
metrics
|> Enum.map(fn metric ->
attach_handler(metric)
{metric, CircularBuffer.new(@history_buffer_size)}
end)
|> Map.new()
{:ok, metric_histories_map}
end
@impl true
def terminate(_, metrics) do
for {metric, _} <- metrics do
:telemetry.detach({__MODULE__, metric, self()})
end
:ok
end
defp attach_handler(%{name: name_list} = metric) do
:telemetry.attach(
{__MODULE__, metric, self()},
Enum.slice(name_list, 0, length(name_list) - 1),
&__MODULE__.handle_event/4,
metric
)
end
def handle_event(_event_name, data, metadata, metric) do
if data = Phoenix.LiveDashboard.extract_datapoint_for_metric(metric, data, metadata) do
GenServer.cast(__MODULE__, {:telemetry_metric, data, metric})
end
end
@impl true
def handle_cast({:telemetry_metric, data, metric}, state) do
{:noreply, update_in(state[metric], &CircularBuffer.insert(&1, data))}
end
@impl true
def handle_call({:data, metric}, _from, state) do
if history = state[metric] do
{:reply, CircularBuffer.to_list(history), state}
else
{:reply, [], state}
end
end
end
defmodule DemoWeb.Telemetry do
import Telemetry.Metrics
def metrics do
[
# Phoenix Metrics
last_value("phoenix.endpoint.stop.duration",
description: "Last value of phoenix.endpoint response time",
unit: {:native, :millisecond}
),
counter("phoenix.endpoint.stop.duration",
unit: {:native, :millisecond}
),
summary("phoenix.endpoint.stop.duration",
unit: {:native, :microsecond}
),
last_value("phoenix.router_dispatch.stop.duration",
tags: [:route],
unit: {:native, :millisecond}
),
counter("phoenix.router_dispatch.stop.duration",
tags: [:route],
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.stop.duration",
tags: [:route],
unit: {:native, :millisecond}
),
# VM Metrics
summary("vm.memory.total", unit: {:byte, :kilobyte}),
summary("vm.total_run_queue_lengths.total"),
summary("vm.total_run_queue_lengths.cpu"),
summary("vm.total_run_queue_lengths.io")
]
end
end
defmodule DemoWeb.PageController do
import Plug.Conn
def init(opts), do: opts
def call(conn, :index) do
content(conn, """
<h2>Phoenix LiveDashboard Dev</h2>
<a href="/dashboard" target="_blank">Open Dashboard</a>
""")
end
def call(conn, :hello) do
name = Map.get(conn.params, "name", "friend")
content(conn, "<p>Hello, #{name}!</p>")
end
defp content(conn, content) do
conn
|> put_resp_header("content-type", "text/html")
|> send_resp(200, "<!doctype html><html><body>#{content}</body></html>")
end
end
defmodule DemoWeb.GraphShowcasePage do
use Phoenix.LiveDashboard.PageBuilder, refresher?: false
@impl true
def menu_link(_, _) do
{:ok, "Graph component"}
end
@impl true
def mount(_params, _, socket) do
{:ok, socket}
end
@impl true
def render_page(_assigns) do
items = [
simple_graph: [name: "Simple", render: &simple/0],
groups_graph: [name: "Groups", render: &two_groups/0],
groups_with_intercalation_graph: [
name: "Groups with intercalation",
render: &two_groups_intercalation/0
],
broadway_graph: [name: "Broadway graph", render: &broadway_graph/0],
wider_graph: [name: "Wider graph", render: &wider_graph/0]
]
nav_bar(items: items)
end
defp simple do
layered_graph(
title: "Simple graph",
layers: [
[%{id: "a1", data: "a1", children: ["b1", "b2"]}],
[%{id: "b1", data: "b1", children: ["c1"]}, %{id: "b2", data: "b2", children: ["c1"]}],
[%{id: "c1", data: "c1", children: []}]
]
)
end
defp two_groups do
background = fn data -> if String.starts_with?(data, "a"), do: "#5d89c7", else: "#555" end
layered_graph(
title: "Two groups",
background: background,
hint: "This chart shows that we can have groups based on parent nodes.",
layers: [
[
%{id: "a1", data: "a1", children: ["b1", "b2"]},
%{id: "a2", data: "a2", children: ["b3", "b4"]},
%{id: "a3", data: "a3", children: ["b3", "b4"]},
%{id: "a4", data: "a4", children: ["b3", "b4"]},
%{id: "a5", data: "a5", children: ["b3", "b4"]}
],
[
%{id: "b1", data: "b1", children: []},
%{id: "b2", data: "b2", children: []},
%{id: "b3", data: "b3", children: []},
%{id: "b4", data: "b4", children: []}
]
]
)
end
defp two_groups_intercalation do
format_label = &String.upcase/1
layered_graph(
title: "Two groups with intercalation",
hint: "This chart shows that intercalation of children is correctly displayed.",
format_label: format_label,
layers: [
[
%{id: "a1", data: "a1", children: ["b1", "b3", "b5"]},
%{id: "a2", data: "a2", children: ["b2", "b4", "b6"]}
],
[
%{id: "b1", data: "b1", children: []},
%{id: "b2", data: "b2", children: []},
%{id: "b3", data: "b3", children: []},
%{id: "b4", data: "b4", children: []},
%{id: "b5", data: "b5", children: []},
%{id: "b6", data: "b6", children: []}
]
]
)
end
defp broadway_graph do
background = fn data ->
case data do
%{detail: perc} ->
hue = 100 - perc
"hsl(#{hue}, 80%, 35%)"
_ ->
"lightgray"
end
end
format_detail = fn data -> "#{data.detail}%" end
layers = [
[
%{
children: [
Demo.Pipeline.Broadway.Processor_default_0,
Demo.Pipeline.Broadway.Processor_default_1,
Demo.Pipeline.Broadway.Processor_default_2,
Demo.Pipeline.Broadway.Processor_default_3,
Demo.Pipeline.Broadway.Processor_default_4
],
data: "prod_0",
id: Demo.Pipeline.Broadway.Producer_0
}
],
[
%{
children: [Demo.Pipeline.Broadway.Batcher_default],
data: %{detail: 84, label: "proc_0"},
id: Demo.Pipeline.Broadway.Processor_default_0
},
%{
children: [Demo.Pipeline.Broadway.Batcher_default],
data: %{detail: 13, label: "proc_1"},
id: Demo.Pipeline.Broadway.Processor_default_1
},
%{
children: [Demo.Pipeline.Broadway.Batcher_default],
data: %{detail: 80, label: "proc_2"},
id: Demo.Pipeline.Broadway.Processor_default_2
},
%{
children: [Demo.Pipeline.Broadway.Batcher_default],
data: %{detail: 82, label: "proc_3"},
id: Demo.Pipeline.Broadway.Processor_default_3
},
%{
children: [Demo.Pipeline.Broadway.Batcher_default],
data: %{detail: 40, label: "proc_4"},
id: Demo.Pipeline.Broadway.Processor_default_4
}
],
[
%{
children: [
Demo.Pipeline.Broadway.BatchProcessor_default_0,
Demo.Pipeline.Broadway.BatchProcessor_default_1,
Demo.Pipeline.Broadway.BatchProcessor_default_2
],
data: %{detail: 33, label: "default"},
id: Demo.Pipeline.Broadway.Batcher_default
}
],
[
%{
children: [],
data: %{detail: 61, label: "proc_0"},
id: Demo.Pipeline.Broadway.BatchProcessor_default_0
},
%{
children: [],
data: %{detail: 61, label: "proc_1"},
id: Demo.Pipeline.Broadway.BatchProcessor_default_1
},
%{
children: [],
data: %{detail: 53, label: "proc_2"},
id: Demo.Pipeline.Broadway.BatchProcessor_default_2
}
]
]
layered_graph(
layers: layers,
background: background,
format_detail: format_detail,
title: "Broadway graph"
)
end
defp wider_graph do
bottom_layer = for i <- 1..20, do: %{id: "b#{i}", data: "b#{i}", children: []}
layered_graph(
title: "Simple graph",
layers: [
[%{id: "a1", data: "a1", children: Enum.map(1..20, &"b#{&1}")}],
bottom_layer
]
)
end
end
defmodule DemoWeb.Router do
use Phoenix.Router
import Phoenix.LiveDashboard.Router
pipeline :browser do
plug :fetch_session
# plug :put_csp
end
scope "/" do
pipe_through :browser
get "/", DemoWeb.PageController, :index
get "/hello", DemoWeb.PageController, :hello
get "/hello/:name", DemoWeb.PageController, :hello
live_dashboard("/dashboard",
env_keys: ["USER", "ROOTDIR"],
metrics: DemoWeb.Telemetry,
metrics_history: {DemoWeb.History, :data, []},
allow_destructive_actions: true,
ecto_repos: [Demo.Repo],
home_app: {"Erlang's stdlib", :stdlib},
additional_pages: [
components: DemoWeb.GraphShowcasePage
],
csp_nonce_assign_key: %{
img: :img_csp_nonce,
style: :style_csp_nonce,
script: :script_csp_nonce
},
ecto_psql_extras_options: [
long_running_queries: [threshold: "200 milliseconds"]
]
)
end
def put_csp(conn, _opts) do
[img_nonce, style_nonce, script_nonce] =
for _i <- 1..3, do: 16 |> :crypto.strong_rand_bytes() |> Base.url_encode64(padding: false)
conn
|> assign(:img_csp_nonce, img_nonce)
|> assign(:style_csp_nonce, style_nonce)
|> assign(:script_csp_nonce, script_nonce)
|> put_resp_header(
"content-security-policy",
"default-src; script-src 'nonce-#{script_nonce}'; style-src 'nonce-#{style_nonce}'; " <>
"img-src 'nonce-#{img_nonce}' data: ; font-src data: ; connect-src 'self'; frame-src 'self' ;"
)
end
end
defmodule DemoWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :phoenix_live_dashboard
socket "/live", Phoenix.LiveView.Socket
socket "/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket
plug Phoenix.LiveReloader
plug Phoenix.CodeReloader
plug Phoenix.LiveDashboard.RequestLogger,
param_key: "request_logger",
cookie_key: "request_logger"
plug Plug.Session,
store: :cookie,
key: "_live_view_key",
signing_salt: "/VEDsdfsffMnp5"
plug Plug.RequestId
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
plug DemoWeb.Router
end
Application.ensure_all_started(:os_mon)
Application.put_env(:phoenix, :serve_endpoints, true)
Task.start(fn ->
children = [
Demo.Repo,
{Phoenix.PubSub, [name: Demo.PubSub, adapter: Phoenix.PubSub.PG2]},
{DemoWeb.History, DemoWeb.Telemetry.metrics()},
DemoWeb.Endpoint
]
{:ok, _} = Supervisor.start_link(children, strategy: :one_for_one)
Process.sleep(:infinity)
end)