Skip to content

Commit 8bfa525

Browse files
authored
Merge pull request #11 from algorandfoundation/fix/catchup-and-zero-tc-blocks
fix: catchup and zero tc blocks
2 parents 8e695f7 + 4cf2f7c commit 8bfa525

File tree

6 files changed

+86
-9
lines changed

6 files changed

+86
-9
lines changed

cmd/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ var (
7676
)
7777
go func() {
7878
state.Watch(func(status *internal.StateModel, err error) {
79-
cobra.CheckErr(err)
80-
p.Send(state)
79+
if err == nil {
80+
p.Send(state)
81+
}
82+
if err != nil {
83+
p.Send(err)
84+
}
8185
}, context.Background(), client)
8286
}()
8387
_, err = p.Run()

internal/block.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ type BlockMetrics struct {
3030
}
3131

3232
func GetBlockMetrics(ctx context.Context, client *api.ClientWithResponses, round uint64, window int) (*BlockMetrics, error) {
33-
var avgs BlockMetrics
33+
var avgs = BlockMetrics{
34+
AvgTime: 0,
35+
TPS: 0,
36+
}
3437
if round < uint64(window) {
35-
avgs.AvgTime = 0
36-
avgs.TPS = 0
3738
return &avgs, nil
3839
}
3940
var format api.GetBlockParamsFormat = "json"
@@ -52,12 +53,14 @@ func GetBlockMetrics(ctx context.Context, client *api.ClientWithResponses, round
5253
// Push to the transactions count list
5354
aTimestamp := time.Duration(a.JSON200.Block["ts"].(float64)) * time.Second
5455
bTimestamp := time.Duration(b.JSON200.Block["ts"].(float64)) * time.Second
55-
//
56-
aTransactions := a.JSON200.Block["tc"].(float64)
57-
bTransactions := b.JSON200.Block["tc"].(float64)
56+
// Transaction Counter
57+
aTransactions := a.JSON200.Block["tc"]
58+
bTransactions := b.JSON200.Block["tc"]
5859

5960
avgs.AvgTime = time.Duration((int(aTimestamp - bTimestamp)) / window)
60-
avgs.TPS = (aTransactions - bTransactions) / (float64(window) * avgs.AvgTime.Seconds())
61+
if aTransactions != nil && bTransactions != nil {
62+
avgs.TPS = (aTransactions.(float64) - bTransactions.(float64)) / (float64(window) * avgs.AvgTime.Seconds())
63+
}
6164

6265
return &avgs, nil
6366
}

internal/state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
4141
}
4242
if status.StatusCode() != 200 {
4343
cb(nil, errors.New(status.Status()))
44+
return
4445
}
4546

4647
// Update Status

ui/error.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ui
2+
3+
import (
4+
"github.com/algorandfoundation/hack-tui/ui/controls"
5+
tea "github.com/charmbracelet/bubbletea"
6+
"github.com/charmbracelet/lipgloss"
7+
"strings"
8+
)
9+
10+
type ErrorViewModel struct {
11+
Height int
12+
Width int
13+
controls controls.Model
14+
Message string
15+
}
16+
17+
func NewErrorViewModel(message string) ErrorViewModel {
18+
return ErrorViewModel{
19+
Height: 0,
20+
Width: 0,
21+
controls: controls.New(" Error "),
22+
}
23+
}
24+
25+
func (m ErrorViewModel) Init() tea.Cmd {
26+
return nil
27+
}
28+
29+
func (m ErrorViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
30+
return m.HandleMessage(msg)
31+
}
32+
33+
func (m ErrorViewModel) HandleMessage(msg tea.Msg) (ErrorViewModel, tea.Cmd) {
34+
var cmd tea.Cmd
35+
switch msg := msg.(type) {
36+
37+
case tea.WindowSizeMsg:
38+
m.Width = msg.Width
39+
m.Height = msg.Height - 2
40+
}
41+
m.controls, cmd = m.controls.HandleMessage(msg)
42+
return m, cmd
43+
}
44+
45+
func (m ErrorViewModel) View() string {
46+
pad := strings.Repeat("\n", max(0, m.Height/2-1))
47+
return lipgloss.JoinVertical(lipgloss.Center, pad, red.Render(m.Message), pad, m.controls.View())
48+
}

ui/style.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var (
2323
cyan = lipgloss.NewStyle().Foreground(lipgloss.Color("14"))
2424
yellow = lipgloss.NewStyle().Foreground(lipgloss.Color("11"))
2525
green = lipgloss.NewStyle().Foreground(lipgloss.Color("10"))
26+
red = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
2627
Magenta = lipgloss.NewStyle().
2728
Foreground(lipgloss.Color("5")).
2829
Render

ui/viewport.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const (
2121
KeysPage ViewportPage = "keys"
2222
GeneratePage ViewportPage = "generate"
2323
TransactionPage ViewportPage = "transaction"
24+
ErrorPage ViewportPage = "error"
2425
)
2526

2627
type ViewportViewModel struct {
@@ -41,6 +42,10 @@ type ViewportViewModel struct {
4142

4243
page ViewportPage
4344
client *api.ClientWithResponses
45+
46+
// Error Handler
47+
errorMsg *string
48+
errorPage ErrorViewModel
4449
}
4550

4651
type DeleteFinished string
@@ -73,6 +78,9 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7378
cmds = append(cmds, cmd)
7479

7580
switch msg := msg.(type) {
81+
case error:
82+
strMsg := msg.Error()
83+
m.errorMsg = &strMsg
7684
// When the state updates
7785
case internal.StateModel:
7886
m.Data = &msg
@@ -170,6 +178,7 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
170178
m.transactionPage, cmd = m.transactionPage.HandleMessage(pageMsg)
171179
cmds = append(cmds, cmd)
172180
//}
181+
m.errorPage, cmd = m.errorPage.HandleMessage(pageMsg)
173182
cmds = append(cmds, cmd)
174183
// Avoid triggering commands again
175184
return m, tea.Batch(cmds...)
@@ -192,6 +201,13 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
192201

193202
// View renders the viewport.Model
194203
func (m ViewportViewModel) View() string {
204+
errMsg := m.errorMsg
205+
206+
if errMsg != nil {
207+
m.errorPage.Message = *errMsg
208+
m.page = ErrorPage
209+
}
210+
195211
// Handle Page render
196212
var page tea.Model
197213
switch m.page {
@@ -203,6 +219,8 @@ func (m ViewportViewModel) View() string {
203219
page = m.keysPage
204220
case TransactionPage:
205221
page = m.transactionPage
222+
case ErrorPage:
223+
page = m.errorPage
206224
}
207225

208226
if page == nil {
@@ -247,6 +265,8 @@ func MakeViewportViewModel(state *internal.StateModel, client *api.ClientWithRes
247265
page: AccountsPage,
248266
// RPC client
249267
client: client,
268+
269+
errorPage: NewErrorViewModel(""),
250270
}
251271

252272
return &m, nil

0 commit comments

Comments
 (0)