Skip to content

Commit 4cf5075

Browse files
glpecileElFantasmaJuArceuri-99MauroToscano
authored
fix(explorer): add real time updates (#497)
Co-authored-by: Esteban Dimitroff Hodi <[email protected]> Co-authored-by: Julian Arce <[email protected]> Co-authored-by: Urix <[email protected]> Co-authored-by: Mauro Toscano <[email protected]>
1 parent eb13bee commit 4cf5075

File tree

5 files changed

+81
-7
lines changed

5 files changed

+81
-7
lines changed

explorer/lib/explorer/models/batches.ex

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,13 @@ defmodule Batches do
125125
nil ->
126126
"New Batch, inserting to DB:" |> IO.puts()
127127
case Explorer.Repo.insert(changeset) do
128-
{:ok, _} -> "Batch inserted successfully" |> IO.puts()
129-
{:error, changeset} -> "Batch insert failed #{changeset}" |> IO.puts()
128+
{:ok, _} ->
129+
"Batch inserted successfully" |> IO.puts()
130+
{:ok, :empty}
131+
132+
{:error, changeset} ->
133+
"Batch insert failed #{changeset}" |> IO.puts()
134+
{:error, changeset}
130135
end
131136
existing_batch ->
132137
try do
@@ -143,8 +148,13 @@ defmodule Batches do
143148
"Batch values have changed, updating in DB" |> IO.puts()
144149
updated_changeset = Ecto.Changeset.change(existing_batch, changeset.changes)
145150
case Explorer.Repo.update(updated_changeset) do
146-
{:ok, _} -> "Batch updated successfully" |> IO.puts()
147-
{:error, changeset} -> "Batch update failed #{changeset}" |> IO.puts()
151+
{:ok, _} ->
152+
"Batch updated successfully" |> IO.puts()
153+
{:ok, :empty}
154+
155+
{:error, changeset} ->
156+
"Batch update failed #{changeset}" |> IO.puts()
157+
{:error, changeset}
148158
end
149159
end
150160
rescue

explorer/lib/explorer/periodically.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule Explorer.Periodically do
2+
alias Phoenix.PubSub
23
use GenServer
34

45
def start_link(_) do
@@ -44,6 +45,7 @@ defmodule Explorer.Periodically do
4445
rescue
4546
error -> IO.puts("An error occurred during batch processing:\n#{inspect(error)}")
4647
end
48+
4749
IO.inspect("Done processing from block #{fromBlock} to block #{toBlock}")
4850
end
4951

@@ -62,6 +64,16 @@ defmodule Explorer.Periodically do
6264
|> Utils.extract_amount_of_proofs
6365
|> Batches.generate_changeset
6466
|> Batches.insert_or_update
67+
|> case do
68+
{:ok, _} ->
69+
IO.puts("Broadcasting update_views")
70+
PubSub.broadcast(Explorer.PubSub, "update_views", %{})
71+
{:error, error} ->
72+
IO.puts("Some error in DB operation, not broadcasting update_views")
73+
IO.inspect(error)
74+
nil -> nil #no changes in DB
75+
76+
end
6577

6678
"Done processing batch: #{batch.merkle_root}" |> IO.inspect
6779
Mutex.release(BatchMutex, lock)

explorer/lib/explorer_web/live/pages/batch/index.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ defmodule ExplorerWeb.Batch.Index do
55
def mount(params, _, socket) do
66
merkle_root = params["merkle_root"]
77

8+
Phoenix.PubSub.subscribe(Explorer.PubSub, "update_views")
9+
810
if merkle_root == nil do
911
{
1012
:empty,
@@ -18,7 +20,6 @@ defmodule ExplorerWeb.Batch.Index do
1820
batch -> batch
1921
end
2022

21-
2223
{
2324
:ok,
2425
assign(socket,
@@ -32,5 +33,17 @@ defmodule ExplorerWeb.Batch.Index do
3233
{:ok, assign(socket, merkle_root: :empty, newBatchInfo: :empty, batchWasResponded: :empty)}
3334
end
3435

36+
def handle_info(_, socket) do
37+
IO.puts("Received batch update for #{socket.assigns.merkle_root} from PubSub")
38+
39+
{
40+
:noreply,
41+
assign(
42+
socket,
43+
current_batch: Batches.get_batch(%{merkle_root: socket.assigns.merkle_root})
44+
)
45+
}
46+
end
47+
3548
embed_templates "*"
3649
end

explorer/lib/explorer_web/live/pages/batches/index.ex

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
defmodule ExplorerWeb.Batches.Index do
2+
alias Phoenix.PubSub
23
require Logger
34
use ExplorerWeb, :live_view
45

6+
@page_size 12
7+
58
def mount(params, _, socket) do
69
current_page = get_current_page(params)
710

8-
page_size = 12
11+
batches = Batches.get_latest_batches(%{amount: @page_size * current_page})
912

10-
batches = Batches.get_latest_batches(%{amount: page_size * current_page})
13+
PubSub.subscribe(Explorer.PubSub, "update_views")
1114

1215
{:ok, assign(socket, current_page: current_page, batches: batches, page_title: "Batches")}
1316
end
1417

18+
def handle_info(_, socket) do
19+
IO.puts("Received update for batches from PubSub")
20+
21+
current_page = socket.assigns.current_page
22+
23+
batches = Batches.get_latest_batches(%{amount: @page_size * current_page})
24+
25+
{:noreply, assign(socket, batches: batches)}
26+
end
27+
1528
def get_current_page(params) do
1629
case params |> Map.get("page") do
1730
nil ->

explorer/lib/explorer_web/live/pages/home/index.ex

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@ defmodule ExplorerWeb.Home.Index do
1919
end
2020
end
2121

22+
def handle_info(_, socket) do
23+
IO.puts("Received update for home from PubSub")
24+
25+
verified_batches = Batches.get_amount_of_verified_batches()
26+
27+
operators_registered = get_operators_registered()
28+
29+
latest_batches =
30+
Batches.get_latest_batches(%{amount: 5})
31+
# extract only the merkle root
32+
|> Enum.map(fn %Batches{merkle_root: merkle_root} -> merkle_root end)
33+
34+
verified_proofs = Batches.get_amount_of_verified_proofs()
35+
36+
{:noreply,
37+
assign(
38+
socket,
39+
verified_batches: verified_batches,
40+
operators_registered: operators_registered,
41+
latest_batches: latest_batches,
42+
verified_proofs: verified_proofs
43+
)}
44+
end
45+
2246
def mount(_, _, socket) do
2347
verified_batches = Batches.get_amount_of_verified_batches()
2448

@@ -31,6 +55,8 @@ defmodule ExplorerWeb.Home.Index do
3155

3256
verified_proofs = Batches.get_amount_of_verified_proofs()
3357

58+
Phoenix.PubSub.subscribe(Explorer.PubSub, "update_views")
59+
3460
{:ok,
3561
assign(socket,
3662
verified_batches: verified_batches,

0 commit comments

Comments
 (0)