Skip to content

Commit 8e6d2c2

Browse files
Oppenuri-99MarcosNicolauPatStilesavilagaston9
authored
feat: progress bar for next batch (#1631)
Co-authored-by: Urix <[email protected]> Co-authored-by: Marcos Nicolau <[email protected]> Co-authored-by: PatStiles <[email protected]> Co-authored-by: Avila Gastón <[email protected]> Co-authored-by: Marcos Nicolau <[email protected]> Co-authored-by: nicolau <[email protected]>
1 parent 4f63510 commit 8e6d2c2

File tree

12 files changed

+154
-9
lines changed

12 files changed

+154
-9
lines changed

explorer/.env.dev

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ MAX_BATCH_SIZE=268435456 # 256 MiB
2525

2626
# Time we wait for a batch to be verified before marking it stale
2727
BATCH_TTL_MINUTES=5
28+
29+
# Scheduled minutes between batch submissions. This is shown in the next batch progress bar component.
30+
SCHEDULED_BATCH_INTERVAL_MINUTES=10

explorer/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ TRACKER_API_URL=<tracker_api_url>
2323

2424
# Time we wait for a batch to be verified before marking it stale
2525
BATCH_TTL_MINUTES=5
26+
27+
# Scheduled minutes between batch submissions. This is shown in the next batch progress bar component.
28+
SCHEDULED_BATCH_INTERVAL_MINUTES=360

explorer/assets/vendor/charts/tooltip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export const alignedTooltip = (
106106
const position = context.chart.canvas.getBoundingClientRect();
107107

108108
tooltipEl.style.opacity = 1;
109-
tooltipEl.style.zIndex = 1;
109+
tooltipEl.style.zIndex = 100;
110110
tooltipEl.style.position = "absolute";
111111
tooltipEl.style.left =
112112
position.left -

explorer/lib/explorer/models/batches.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ defmodule Batches do
7676
Explorer.Repo.one(query)
7777
end
7878

79+
def get_latest_verified_batch() do
80+
query = from(b in Batches,
81+
order_by: [desc: b.submission_block_number],
82+
limit: 1,
83+
where: b.is_verified,
84+
select: b)
85+
86+
Explorer.Repo.one(query)
87+
end
88+
7989
def get_latest_batches(%{amount: amount} = %{order_by: :desc}) do
8090
query = from(b in Batches,
8191
order_by: [desc: b.submission_block_number],

explorer/lib/explorer/periodically.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ defmodule Explorer.Periodically do
1616
one_second = 1000
1717
seconds_in_an_hour = 60 * 60
1818

19+
:timer.send_interval(one_second * 60, :next_batch_progress) # every minute
1920
:timer.send_interval(one_second * 12, :batches) # every 12 seconds, once per block
2021
:timer.send_interval(one_second * seconds_in_an_hour, :restakings) # every 1 hour
2122
end
@@ -34,6 +35,18 @@ defmodule Explorer.Periodically do
3435
{:noreply, %{state | restakings_last_read_block: latest_block_number}}
3536
end
3637

38+
def handle_info(:next_batch_progress, state) do
39+
Logger.debug("handling block progress timer")
40+
remaining_time = ExplorerWeb.Helpers.get_next_scheduled_batch_remaining_time()
41+
PubSub.broadcast(Explorer.PubSub, "update_views", %{
42+
next_scheduled_batch_remaining_time_percentage:
43+
ExplorerWeb.Helpers.get_next_scheduled_batch_remaining_time_percentage(remaining_time),
44+
next_scheduled_batch_remaining_time: remaining_time
45+
})
46+
47+
{:noreply, state}
48+
end
49+
3750
# Reads and process last n blocks for new batches or batch changes
3851
def handle_info(:batches, state) do
3952
count = Map.get(state, :batches_count)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule LabeledProgressBarComponent do
2+
use ExplorerWeb, :live_component
3+
4+
attr(:percent_progress, :float, required: true)
5+
attr(:label, :string, required: true)
6+
7+
@impl true
8+
def render(assigns) do
9+
~H"""
10+
<div class="w-full relative weight-700 rounded-lg">
11+
<div class="w-full bg-accent/30 rounded-2xl">
12+
<p class="ml-2 normal-case text-center relative text-accent-foreground font-bold z-10">
13+
<%= @label %>
14+
</p>
15+
</div>
16+
<div
17+
class="top-0 left-0 h-full bg-accent p-1 rounded-2xl absolute transition-all"
18+
style={"width: #{@percent_progress}%"}
19+
>
20+
</div>
21+
</div>
22+
"""
23+
end
24+
25+
def update_assigns(assigns, socket) do
26+
progress = assigns[:percent_progress] || 0
27+
label = assigns[:label] || ""
28+
{:ok, assign(socket, percent_progress: progress, label: label)}
29+
end
30+
end

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@ defmodule ExplorerWeb.Batches.Index do
1010
def mount(params, _, socket) do
1111
current_page = get_current_page(params)
1212

13-
batches = Batches.get_paginated_batches(%{page: current_page, page_size: @page_size})
14-
|> Helpers.enrich_batches()
13+
batches =
14+
Batches.get_paginated_batches(%{page: current_page, page_size: @page_size})
15+
|> Helpers.enrich_batches()
1516

1617
if connected?(socket), do: PubSub.subscribe(Explorer.PubSub, "update_views")
1718

19+
remaining_time = Helpers.get_next_scheduled_batch_remaining_time()
20+
1821
{:ok,
1922
assign(socket,
2023
current_page: current_page,
2124
batches: batches,
25+
next_scheduled_batch_remaining_time_percentage:
26+
Helpers.get_next_scheduled_batch_remaining_time_percentage(remaining_time),
27+
next_scheduled_batch_remaining_time: remaining_time,
2228
last_page: Batches.get_last_page(@page_size),
2329
page_title: "Batches"
2430
)}
@@ -28,10 +34,20 @@ defmodule ExplorerWeb.Batches.Index do
2834
def handle_info(_, socket) do
2935
current_page = socket.assigns.current_page
3036

31-
batches = Batches.get_paginated_batches(%{page: current_page, page_size: @page_size})
32-
|> Helpers.enrich_batches()
37+
batches =
38+
Batches.get_paginated_batches(%{page: current_page, page_size: @page_size})
39+
|> Helpers.enrich_batches()
40+
41+
remaining_time = Helpers.get_next_scheduled_batch_remaining_time()
3342

34-
{:noreply, assign(socket, batches: batches, last_page: Batches.get_last_page(@page_size))}
43+
{:noreply,
44+
assign(socket,
45+
batches: batches,
46+
next_scheduled_batch_remaining_time_percentage:
47+
Helpers.get_next_scheduled_batch_remaining_time_percentage(remaining_time),
48+
next_scheduled_batch_remaining_time: remaining_time,
49+
last_page: Batches.get_last_page(@page_size)
50+
)}
3551
end
3652

3753
@impl true
@@ -55,5 +71,5 @@ defmodule ExplorerWeb.Batches.Index do
5571
end
5672
end
5773

58-
embed_templates "*"
74+
embed_templates("*")
5975
end

explorer/lib/explorer_web/live/pages/batches/index.html.heex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
<div class="flex flex-col space-y-3 text-foreground px-1 sm:max-w-lg md:max-w-3xl lg:max-w-5xl mx-auto capitalize">
22
<.card_preheding>Batches</.card_preheding>
3+
<!-- Progress bar based on https://dev.to/mikekennedydev/progress-bar-text-colour-1gg6 -->
4+
<.live_component
5+
id="labeled-progress-bar"
6+
module={LabeledProgressBarComponent}
7+
percent_progress={@next_scheduled_batch_remaining_time_percentage}
8+
label={
9+
case @next_scheduled_batch_remaining_time do
10+
0 -> "Next batch is around the corner!"
11+
time -> "Next batch in #{time} minutes"
12+
end
13+
}/>
314
<%= if @batches != :empty and @batches != [] do %>
415
<.card_background class="w-full overflow-x-auto sm:col-span-2">
516
<.batches_table batches={@batches} />

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,29 +155,36 @@ defmodule ExplorerWeb.Home.Index do
155155
stats: [],
156156
latest_batches: [],
157157
cost_per_proof_chart: %{points: [], extra_data: %{}},
158-
batch_size_chart_data: %{points: [], extra_data: %{}}
158+
batch_size_chart_data: %{points: [], extra_data: %{}},
159+
next_scheduled_batch_remaining_time_percentage: 0,
160+
next_scheduled_batch_remaining_time: 0
159161
)
160162
end
161163

162164
@impl true
163165
def handle_info(_, socket) do
164166
latest_batches = Batches.get_latest_batches(%{amount: 10, order_by: :desc})
165167
charts_query_limit = 20
168+
remaining_time = Helpers.get_next_scheduled_batch_remaining_time()
166169

167170
{:noreply,
168171
assign(
169172
socket,
170173
stats: get_stats(),
171174
latest_batches: latest_batches,
172175
cost_per_proof_chart: get_cost_per_proof_chart_data(charts_query_limit),
173-
batch_size_chart_data: get_batch_size_chart_data(charts_query_limit)
176+
batch_size_chart_data: get_batch_size_chart_data(charts_query_limit),
177+
next_scheduled_batch_remaining_time_percentage:
178+
Helpers.get_next_scheduled_batch_remaining_time_percentage(remaining_time),
179+
next_scheduled_batch_remaining_time: remaining_time
174180
)}
175181
end
176182

177183
@impl true
178184
def mount(_, _, socket) do
179185
latest_batches = Batches.get_latest_batches(%{amount: 10, order_by: :desc})
180186
charts_query_limit = 20
187+
remaining_time = Helpers.get_next_scheduled_batch_remaining_time()
181188

182189
if connected?(socket), do: Phoenix.PubSub.subscribe(Explorer.PubSub, "update_views")
183190

@@ -187,6 +194,9 @@ defmodule ExplorerWeb.Home.Index do
187194
latest_batches: latest_batches,
188195
cost_per_proof_chart: get_cost_per_proof_chart_data(charts_query_limit),
189196
batch_size_chart_data: get_batch_size_chart_data(charts_query_limit),
197+
next_scheduled_batch_remaining_time_percentage:
198+
Helpers.get_next_scheduled_batch_remaining_time_percentage(remaining_time),
199+
next_scheduled_batch_remaining_time: remaining_time,
190200
page_title: "Welcome"
191201
)}
192202
rescue

explorer/lib/explorer_web/live/pages/home/index.html.heex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131
</div>
3232
<% end %>
3333
</.card_background>
34+
<.live_component
35+
id="labeled-progress-bar"
36+
module={LabeledProgressBarComponent}
37+
percent_progress={@next_scheduled_batch_remaining_time_percentage}
38+
label={
39+
case @next_scheduled_batch_remaining_time do
40+
0 -> "Next batch is around the corner!"
41+
time -> "Next batch in #{time} minutes"
42+
end
43+
}/>
3444
<div class="flex flex-wrap md:flex-row flex-col gap-5">
3545
<.card
3646
title="Cost per proof"

0 commit comments

Comments
 (0)