Skip to content

Commit 76954ee

Browse files
PatStilesMarcosNicolauuri-99
authored
feat(explorer): batch-size chart (#1641)
Co-authored-by: nicolau <[email protected]> Co-authored-by: Marcos Nicolau <[email protected]> Co-authored-by: Urix <[email protected]> Co-authored-by: Marcos Nicolau <[email protected]>
1 parent f473f6d commit 76954ee

File tree

5 files changed

+100
-5
lines changed

5 files changed

+100
-5
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { alignedTooltip } from "./tooltip";
2+
3+
export const batchSizeCustomOptions = (options, data) => {
4+
// show only min and max values
5+
options.scales.y.ticks.callback = (_value, index, values) => {
6+
const dataY = data.datasets[0].data.map((point) => parseFloat(point.y));
7+
if (index === 0) return `${Math.min(...dataY)} proofs`;
8+
if (index === values.length - 1) {
9+
return `${Math.max(...dataY)} proofs`;
10+
}
11+
return "";
12+
};
13+
14+
// show age min, mean and max age in x axis
15+
options.scales.x.ticks.callback = (_value, index, values) => {
16+
const age = data.datasets[0].age;
17+
if (index === 0) return age[0];
18+
if (index === Math.floor((age.length - 1) / 2))
19+
return age[Math.floor((age.length - 1) / 2)];
20+
if (index === values.length - 1) return age[age.length - 1];
21+
return "";
22+
};
23+
24+
options.plugins.tooltip.external = (context) =>
25+
alignedTooltip(context, {
26+
title: "Batch size",
27+
items: [
28+
{ title: "Cost", id: "cost" },
29+
{ title: "Age", id: "age" },
30+
{ title: "Merkle root", id: "merkle_root" },
31+
{ title: "Block number", id: "block_number" },
32+
{ title: "Amount of proofs", id: "amount_of_proofs" },
33+
],
34+
onTooltipClick: (tooltipModel) => {
35+
const dataset = tooltipModel.dataPoints[0].dataset;
36+
const idx = tooltipModel.dataPoints[0].dataIndex;
37+
const merkleRootHash = dataset.merkle_root[idx];
38+
window.location.href = `/batches/${merkleRootHash}`;
39+
},
40+
onTooltipUpdate: (tooltipModel) => {
41+
const dataset = tooltipModel.dataPoints[0].dataset;
42+
const idx = tooltipModel.dataPoints[0].dataIndex;
43+
44+
const cost = `${dataset.data[idx].y} USD`;
45+
const age = dataset.age[idx];
46+
const merkleRootHash = dataset.merkle_root[idx];
47+
const merkle_root = `${merkleRootHash.slice(
48+
0,
49+
6
50+
)}...${merkleRootHash.slice(merkleRootHash.length - 4)}`;
51+
const block_number = dataset.data[idx].x;
52+
const amount_of_proofs = dataset.amount_of_proofs[idx];
53+
54+
return {
55+
cost,
56+
age,
57+
merkle_root,
58+
block_number,
59+
amount_of_proofs,
60+
};
61+
},
62+
});
63+
};

explorer/assets/vendor/charts/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Chart from "chart.js/auto";
22
import { costPerProofCustomOptions } from "./cost_per_proof";
3+
import { batchSizeCustomOptions } from "./batch_size";
34

45
const applyCommonChartOptions = (options, data) => {
56
// tooltip disabled by default, each chart should implement its own with alignedTooltip
@@ -11,6 +12,7 @@ const applyCommonChartOptions = (options, data) => {
1112
const applyOptionsByChartId = (id, options, data) => {
1213
const idOptionsMap = {
1314
cost_per_proof_chart: () => costPerProofCustomOptions(options, data),
15+
batch_size_chart: () => batchSizeCustomOptions(options, data),
1416
};
1517

1618
idOptionsMap[id] && idOptionsMap[id]();

explorer/lib/explorer_web/components/charts.ex

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ defmodule ExplorerWeb.ChartComponents do
2424

2525
@doc """
2626
Renders a line chart with aligned style.
27-
2827
## Examples
2928
<.line
3029
id="exchanges"
@@ -83,7 +82,6 @@ defmodule ExplorerWeb.ChartComponents do
8382
ticks: %{
8483
display: @show_ticks.x,
8584
autoSkip: false,
86-
sampleSize: 1,
8785
maxRotation: 0,
8886
font: %{
8987
weight: "700"
@@ -102,7 +100,6 @@ defmodule ExplorerWeb.ChartComponents do
102100
ticks: %{
103101
display: @show_ticks.y,
104102
autoSkip: false,
105-
sampleSize: 1,
106103
maxRotation: 0,
107104
font: %{
108105
weight: "700"

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ defmodule ExplorerWeb.Home.Index do
3434
}
3535
end
3636

37+
def get_batch_size_chart_data() do
38+
batches = Enum.reverse(Batches.get_latest_batches(%{amount: 100, order_by: :desc}))
39+
40+
extra_data =
41+
%{
42+
merkle_root: Enum.map(batches, fn b -> b.merkle_root end),
43+
amount_of_proofs: Enum.map(batches, fn b -> b.amount_of_proofs end),
44+
age: Enum.map(batches, fn b -> Helpers.parse_timeago(b.submission_timestamp) end)
45+
}
46+
47+
points =
48+
Enum.map(batches, fn b ->
49+
%{x: b.submission_block_number, y: b.amount_of_proofs}
50+
end)
51+
52+
%{
53+
points: points,
54+
extra_data: extra_data
55+
}
56+
end
57+
3758
defp set_empty_values(socket) do
3859
Logger.info("Setting empty values")
3960

@@ -45,7 +66,8 @@ defmodule ExplorerWeb.Home.Index do
4566
verified_proofs: :empty,
4667
restaked_amount_eth: :empty,
4768
restaked_amount_usd: :empty,
48-
cost_per_proof_data: %{points: [], extra_data: %{}}
69+
cost_per_proof_data: %{points: [], extra_data: %{}},
70+
batch_size_chart_data: %{points: [], extra_data: %{}}
4971
)
5072
end
5173

@@ -73,7 +95,8 @@ defmodule ExplorerWeb.Home.Index do
7395
verified_proofs: verified_proofs,
7496
restaked_amount_eth: restaked_amount_eth,
7597
restaked_amount_usd: restaked_amount_usd,
76-
cost_per_proof_chart: get_cost_per_proof_chart_data()
98+
cost_per_proof_chart: get_cost_per_proof_chart_data(),
99+
batch_size_chart_data: get_batch_size_chart_data()
77100
)}
78101
end
79102

@@ -105,6 +128,7 @@ defmodule ExplorerWeb.Home.Index do
105128
restaked_amount_eth: restaked_amount_eth,
106129
restaked_amount_usd: restaked_amount_usd,
107130
cost_per_proof_chart: get_cost_per_proof_chart_data(),
131+
batch_size_chart_data: get_batch_size_chart_data(),
108132
page_title: "Welcome"
109133
)}
110134
rescue

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@
4545
/>
4646
</div>
4747
</.card>
48+
<.card title="Batch Size" class="w-full sm:col-span-2">
49+
<div style="height: 200px;" class="mt-4">
50+
<.line_chart
51+
id="batch_size_chart"
52+
points={@batch_size_chart_data.points}
53+
extra_data={@batch_size_chart_data.extra_data}
54+
/>
55+
</div>
56+
</.card>
4857
<div class="relative truncate sm:col-span-2">
4958
<h1 class="text-xl font-bold font-foreground text-left py-2">
5059
Recent Batches

0 commit comments

Comments
 (0)