From 2d93f7910b28d749e3341720862b662c65ca16a8 Mon Sep 17 00:00:00 2001 From: Ji Hwan Date: Wed, 2 Oct 2024 12:16:17 +0900 Subject: [PATCH 1/3] refactor: change monitor gas price logic calculation Signed-off-by: Ji Hwan --- cmd/monitor/monitor.go | 11 ++++++++++- cmd/monitor/ui/ui.go | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/cmd/monitor/monitor.go b/cmd/monitor/monitor.go index 0e0ccc17..53beab54 100644 --- a/cmd/monitor/monitor.go +++ b/cmd/monitor/monitor.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "math/big" + "strconv" "strings" "sync" "time" @@ -568,7 +569,15 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu ms.BlocksLock.RUnlock() renderedBlocks = renderedBlocksTemp - skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, ms.GasPrice, ms.PeerCount, ms.ChainID, rpcUrl) + // First initialization will render no gas price because the GasPriceChart will have no data. + if skeleton.GasPriceChart.Data == nil { + skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, "--", ms.PeerCount, ms.ChainID, rpcUrl) + } else { + // Under normal cases, the gas price will be derived from the last element of the GasPriceChart with 2 decimal places precision. + gasPriceStr := strconv.FormatFloat(skeleton.GasPriceChart.Data[len(skeleton.GasPriceChart.Data)-1]/1000000000, 'f', 2, 64) + skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, gasPriceStr, ms.PeerCount, ms.ChainID, rpcUrl) + } + if txPoolStatusSupported { skeleton.TxPool.Text = ui.GetTxPoolText(skeleton.TxPool, ms.TxPoolStatus.pending, ms.TxPoolStatus.queued) } diff --git a/cmd/monitor/ui/ui.go b/cmd/monitor/ui/ui.go index d705ec9f..3ebfde9e 100644 --- a/cmd/monitor/ui/ui.go +++ b/cmd/monitor/ui/ui.go @@ -36,11 +36,11 @@ type UiSkeleton struct { Receipts *widgets.List } -func GetCurrentText(widget *widgets.Paragraph, headBlock, gasPrice *big.Int, peerCount uint64, chainID *big.Int, rpcURL string) string { +func GetCurrentText(widget *widgets.Paragraph, headBlock *big.Int, gasPrice string, peerCount uint64, chainID *big.Int, rpcURL string) string { // First column height := fmt.Sprintf("Height: %s", headBlock.String()) timeInfo := fmt.Sprintf("Time: %s", time.Now().Format("02 Jan 06 15:04:05 MST")) - gasPriceString := fmt.Sprintf("Gas Price: %s gwei", new(big.Int).Div(gasPrice, metrics.UnitShannon).String()) + gasPriceString := fmt.Sprintf("Gas Price: %s gwei", gasPrice) peers := fmt.Sprintf("Peers: %d", peerCount) // Second column From cd3e2ec9a42984f4cb6803d6d09e0fe5bde0cfc6 Mon Sep 17 00:00:00 2001 From: Ji Hwan Date: Wed, 2 Oct 2024 23:22:54 +0900 Subject: [PATCH 2/3] chore: reuse metrics.GetMeanGasPricePerBlock instead of deriving data from skeleton Signed-off-by: Ji Hwan --- cmd/monitor/monitor.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/monitor/monitor.go b/cmd/monitor/monitor.go index 53beab54..eee56dbc 100644 --- a/cmd/monitor/monitor.go +++ b/cmd/monitor/monitor.go @@ -568,13 +568,12 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu } ms.BlocksLock.RUnlock() renderedBlocks = renderedBlocksTemp - // First initialization will render no gas price because the GasPriceChart will have no data. - if skeleton.GasPriceChart.Data == nil { + if metrics.GetMeanGasPricePerBlock(renderedBlocks) == nil { skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, "--", ms.PeerCount, ms.ChainID, rpcUrl) } else { // Under normal cases, the gas price will be derived from the last element of the GasPriceChart with 2 decimal places precision. - gasPriceStr := strconv.FormatFloat(skeleton.GasPriceChart.Data[len(skeleton.GasPriceChart.Data)-1]/1000000000, 'f', 2, 64) + gasPriceStr := strconv.FormatFloat(metrics.GetMeanGasPricePerBlock(renderedBlocks)[len(metrics.GetMeanGasPricePerBlock(renderedBlocks))-1]/1000000000, 'f', 2, 64) skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, gasPriceStr, ms.PeerCount, ms.ChainID, rpcUrl) } From 2598527a9c97cfd8b9e77b05bff5927e42f76a5c Mon Sep 17 00:00:00 2001 From: Ji Hwan Date: Thu, 3 Oct 2024 08:22:36 +0900 Subject: [PATCH 3/3] chore: store mean gas price values in variable Signed-off-by: Ji Hwan --- cmd/monitor/monitor.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/monitor/monitor.go b/cmd/monitor/monitor.go index eee56dbc..2395bf0c 100644 --- a/cmd/monitor/monitor.go +++ b/cmd/monitor/monitor.go @@ -568,12 +568,13 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu } ms.BlocksLock.RUnlock() renderedBlocks = renderedBlocksTemp + renderedBlocksMeanGasPrice := metrics.GetMeanGasPricePerBlock(renderedBlocks) // First initialization will render no gas price because the GasPriceChart will have no data. - if metrics.GetMeanGasPricePerBlock(renderedBlocks) == nil { + if renderedBlocksMeanGasPrice == nil { skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, "--", ms.PeerCount, ms.ChainID, rpcUrl) } else { // Under normal cases, the gas price will be derived from the last element of the GasPriceChart with 2 decimal places precision. - gasPriceStr := strconv.FormatFloat(metrics.GetMeanGasPricePerBlock(renderedBlocks)[len(metrics.GetMeanGasPricePerBlock(renderedBlocks))-1]/1000000000, 'f', 2, 64) + gasPriceStr := strconv.FormatFloat(renderedBlocksMeanGasPrice[len(renderedBlocksMeanGasPrice)-1]/1000000000, 'f', 2, 64) skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, gasPriceStr, ms.PeerCount, ms.ChainID, rpcUrl) } @@ -586,7 +587,7 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu } skeleton.TxPerBlockChart.Data = metrics.GetTxsPerBlock(renderedBlocks) - skeleton.GasPriceChart.Data = metrics.GetMeanGasPricePerBlock(renderedBlocks) + skeleton.GasPriceChart.Data = renderedBlocksMeanGasPrice // equivalent to metrics.GetMeanGasPricePerBlock(renderedBlocks) skeleton.BlockSizeChart.Data = metrics.GetSizePerBlock(renderedBlocks) // skeleton.pendingTxChart.Data = metrics.GetUnclesPerBlock(renderedBlocks) skeleton.PendingTxChart.Data = observedPendingTxs.getValues(25)