Skip to content

Commit

Permalink
replace unuseless widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Mar 28, 2024
1 parent 7f72bfd commit c2b11ea
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 62 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/mum4k/termdash v0.20.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
golang.org/x/sync v0.6.0
golang.org/x/tools v0.19.0
golang.org/x/vuln v1.0.4
mvdan.cc/gofumpt v0.6.0
Expand Down Expand Up @@ -283,7 +284,6 @@ require (
golang.org/x/exp/typeparams v0.0.0-20240314144324-c7f7c6466f7f // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
20 changes: 10 additions & 10 deletions pkg/widget/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ func (w *Widget) drawView() (*container.Container, error) {
),
container.Right(
container.Border(linestyle.Light),
container.BorderTitle("Health"),
container.PlaceWidget(w.health),
container.BorderTitle("Moniker"),
container.PlaceWidget(w.moniker),
),
),
),
container.Right(
container.SplitVertical(
container.Left(
container.Border(linestyle.Light),
container.BorderTitle("System Time"),
container.PlaceWidget(w.time),
container.BorderTitle("Health"),
container.PlaceWidget(w.health),
),
container.Right(
container.Border(linestyle.Light),
container.BorderTitle("Connected Peers"),
container.PlaceWidget(w.peers),
container.BorderTitle("System Time"),
container.PlaceWidget(w.time),
),
),
),
Expand All @@ -57,8 +57,8 @@ func (w *Widget) drawView() (*container.Container, error) {
container.SplitVertical(
container.Left(
container.Border(linestyle.Light),
container.BorderTitle("Latest Block"),
container.PlaceWidget(w.height),
container.BorderTitle("Block Time"),
container.PlaceWidget(w.secondsPerBlock),
),
container.Right(
container.Border(linestyle.Light),
Expand All @@ -71,8 +71,8 @@ func (w *Widget) drawView() (*container.Container, error) {
container.SplitVertical(
container.Left(
container.Border(linestyle.Light),
container.BorderTitle("s Between Blocks"),
container.PlaceWidget(w.secondsPerBlock),
container.BorderTitle("Connected Peers"),
container.PlaceWidget(w.peers),
),
container.Right(
container.Border(linestyle.Light),
Expand Down
26 changes: 14 additions & 12 deletions pkg/widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/mum4k/termdash/widgets/text"
)

const loading = "⌛ loading..."

// Widget widget container holder.
type Widget struct {
terminal *termbox.Terminal
Expand All @@ -31,7 +33,7 @@ type Widget struct {
latestGas *text.Text
transactions *text.Text
blocks *text.Text
height *text.Text
moniker *text.Text
blockProgress *donut.Donut
}

Expand All @@ -46,15 +48,15 @@ func New() (*Widget, error) {
if widget.currentNetwork, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
return widget, err
}
if err := widget.currentNetwork.Write("⌛ loading"); err != nil {
if err := widget.currentNetwork.Write(loading); err != nil {
return widget, err
}

// Creates Health Widget.
if widget.health, err = text.New(); err != nil {
return widget, err
}
if err := widget.health.Write("⌛ loading"); err != nil {
if err := widget.health.Write(loading); err != nil {
return widget, err
}

Expand All @@ -63,7 +65,7 @@ func New() (*Widget, error) {
return widget, err
}
currentTime := time.Now()
if err := widget.time.Write(currentTime.Format("2006-01-02\n03:04:05 PM\n")); err != nil {
if err := widget.time.Write(currentTime.Format("2006-01-02\n22:04:05")); err != nil {
return widget, err
}

Expand Down Expand Up @@ -95,39 +97,39 @@ func New() (*Widget, error) {
if widget.validators, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
return widget, err
}
if err := widget.validators.Write("List available validators.\n\n"); err != nil {
if err := widget.validators.Write(loading); err != nil {
return widget, err
}

// Creates Validators widget.
if widget.gasMax, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
return widget, err
}
if err := widget.gasMax.Write("How much gas.\n\n"); err != nil {
if err := widget.gasMax.Write(loading); err != nil {
return widget, err
}

// Creates Gas per Average Block Widget.
if widget.gasAvgBlock, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
return widget, err
}
if err := widget.gasAvgBlock.Write("How much gas.\n\n"); err != nil {
if err := widget.gasAvgBlock.Write(loading); err != nil {
return widget, err
}

// Creates Gas per Average Transaction Widget.
if widget.gasAvgTransaction, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
return widget, err
}
if err := widget.gasAvgTransaction.Write("How much gas.\n\n"); err != nil {
if err := widget.gasAvgTransaction.Write(loading); err != nil {
return widget, err
}

// Creates Gas per Latest Transaction Widget.
if widget.latestGas, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
return widget, err
}
if err := widget.latestGas.Write("How much gas.\n\n"); err != nil {
if err := widget.latestGas.Write(loading); err != nil {
return widget, err
}

Expand All @@ -145,7 +147,7 @@ func New() (*Widget, error) {
if widget.transactions, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
return widget, err
}
if err := widget.transactions.Write("Transactions will appear as soon as they are confirmed in a height."); err != nil {
if err := widget.transactions.Write("Transactions will appear as soon as they are confirmed in a moniker."); err != nil {
return widget, err
}

Expand All @@ -155,10 +157,10 @@ func New() (*Widget, error) {
}

// Create Blocks parsing widget.
if widget.height, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
if widget.moniker, err = text.New(text.RollContent(), text.WrapAtWords()); err != nil {
return widget, err
}
if err := widget.height.Write("⌛ loading"); err != nil {
if err := widget.moniker.Write(loading); err != nil {
return widget, err
}

Expand Down
12 changes: 5 additions & 7 deletions pkg/widget/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/widgets/donut"
"github.com/mum4k/termdash/widgets/text"

"github.com/ignite/gex/pkg/number"
)

// SetCurrentNetwork reset the widget and set current network text.
Expand Down Expand Up @@ -78,15 +76,15 @@ func (w *Widget) SetLatestGas(txt string, opts ...text.WriteOption) error {
return w.latestGas.Write(txt, opts...)
}

// SetHeight resets the widget and sets block height text.
func (w *Widget) SetHeight(height int64, opts ...text.WriteOption) error {
w.height.Reset()
return w.height.Write(number.WithComma(height), opts...)
// SetMoniker resets the widget and sets moniker text.
func (w *Widget) SetMoniker(text string, opts ...text.WriteOption) error {
w.moniker.Reset()
return w.moniker.Write(text, opts...)
}

// AddTransaction adds a new transaction to the widget.
func (w *Widget) AddTransaction(txt string, opts ...text.WriteOption) error {
now := time.Now().Format("2006-01-02 03:04:05 PM")
now := time.Now().Format("2006-01-02 22:04:05")
if err := w.transactions.Write(
fmt.Sprintf("\n\nNew Transaction (%s)\n", now),
text.WriteCellOpts(cell.Bold(), cell.Inverse()),
Expand Down
80 changes: 48 additions & 32 deletions services/explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package explorer
import (
"context"
"fmt"
"strings"
"sync"
"time"

"github.com/cometbft/cometbft/libs/json"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cometbft/cometbft/types"
"golang.org/x/sync/errgroup"

"github.com/ignite/gex/pkg/client"
"github.com/ignite/gex/pkg/number"
Expand All @@ -20,6 +22,14 @@ const (
statusNotConnected = "✖️ not connected"
)

var (
RoundStepPropose = strings.ToUpper("RoundStepPropose")
RoundStepPreVote = strings.ToUpper("RoundStepPrevote")
RoundStepPreCommit = strings.ToUpper("RoundStepPrecommit")
RoundStepCommit = strings.ToUpper("RoundStepCommit")
RoundStepNewHeight = strings.ToUpper("RoundStepNewHeight")
)

// info holds all cross infos.
type info struct {
sync.RWMutex
Expand Down Expand Up @@ -49,9 +59,23 @@ func Run(ctx context.Context, host string) error {
info = &info{}
start = time.Now()
)

errGroup, _ := errgroup.WithContext(ctx)
errGroup.Go(func() error {
status, err := c.Status(ctx)
if err != nil {
return err
}

if err := w.SetCurrentNetwork(status.NodeInfo.Network); err != nil {
return err
}
return w.SetMoniker(status.NodeInfo.Moniker)
})

client.Callback(ctx, 1*time.Second, func() error {
now := time.Now()
if err := w.SetTime(now.Format("2006-01-02\n03:04:05 PM")); err != nil {
if err := w.SetTime(now.Format("2006-01-02\n22:04:05")); err != nil {
return err
}
secondsPassed := now.Sub(start).Seconds()
Expand Down Expand Up @@ -92,14 +116,6 @@ func Run(ctx context.Context, host string) error {
return w.SetGasAvgTransaction(number.WithComma(averageGasPerTx))
})

status, err := c.Status(ctx)
if err != nil {
return err
}
if err := w.SetCurrentNetwork(status.NodeInfo.Network); err != nil {
return err
}

c.ConsensusParams(ctx, func(params coretypes.ResultConsensusParams) error {
info.Lock()
info.maxGasWanted = params.ConsensusParams.Block.MaxGas
Expand All @@ -122,11 +138,27 @@ func Run(ctx context.Context, host string) error {
return w.SetValidators(validators.Total)
})

err = c.NewBlock(ctx, func(block types.EventDataNewBlock) error {
if err := w.SetHeight(block.Block.Height); err != nil {
return err
err = c.NewRoundStep(ctx, func(state types.EventDataRoundState) error {
progress := 0
switch strings.ToUpper(state.Step) {
case RoundStepPropose:
progress = 20
case RoundStepPreVote:
progress = 40
case RoundStepPreCommit:
progress = 60
case RoundStepCommit:
progress = 80
case RoundStepNewHeight:
progress = 100
}
return w.SetBlockProgress(progress)
})
if err != nil {
return err
}

err = c.NewBlock(ctx, func(block types.EventDataNewBlock) error {
info.Lock()
info.blocks++
info.Unlock()
Expand All @@ -144,26 +176,6 @@ func Run(ctx context.Context, host string) error {
return err
}

err = c.NewRoundStep(ctx, func(state types.EventDataRoundState) error {
progress := 0
switch state.Step {
case "RoundStepPropose":
progress = 20
case "RoundStepPrevote":
progress = 40
case "RoundStepPrecommit":
progress = 60
case "RoundStepCommit":
progress = 80
case "RoundStepNewHeight":
progress = 100
}
return w.SetBlockProgress(progress)
})
if err != nil {
return err
}

err = c.Tx(ctx, func(tx types.EventDataTx) error {
info.Lock()
info.transactions++
Expand All @@ -181,5 +193,9 @@ func Run(ctx context.Context, host string) error {
return err
}

if err := errGroup.Wait(); err != nil {
return err
}

return w.Run(ctx)
}

0 comments on commit c2b11ea

Please sign in to comment.