Skip to content

Commit 4e34d48

Browse files
authored
Merge pull request #23 from algorandfoundation/feat/disable-ui-for-sync
feat: disable UI for sync
2 parents 6a05bf9 + eea8be8 commit 4e34d48

File tree

6 files changed

+65
-36
lines changed

6 files changed

+65
-36
lines changed

internal/accounts.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,23 @@ func AccountsFromState(state *StateModel, t Time, client *api.ClientWithResponse
121121
for _, key := range *state.ParticipationKeys {
122122
val, ok := values[key.Address]
123123
if !ok {
124-
125-
account, err := GetAccount(client, key.Address)
126-
127-
// TODO: handle error
128-
if err != nil {
129-
// TODO: Logging
130-
panic(err)
124+
var account = api.Account{
125+
Address: key.Address,
126+
Status: "Unknown",
127+
Amount: 0,
131128
}
132-
133-
var expires = t.Now()
129+
if state.Status.State != "SYNCING" {
130+
var err error
131+
account, err = GetAccount(client, key.Address)
132+
// TODO: handle error
133+
if err != nil {
134+
// TODO: Logging
135+
panic(err)
136+
}
137+
}
138+
now := t.Now()
139+
var expires = now.Add(-(time.Hour * 24 * 365 * 100))
134140
if key.EffectiveLastValid != nil {
135-
now := t.Now()
136141
roundDiff := max(0, *key.EffectiveLastValid-int(state.Status.LastRound))
137142
distance := int(state.Metrics.RoundTime) * roundDiff
138143
expires = now.Add(time.Duration(distance))

internal/state.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
6262
// Fetch Keys
6363
s.UpdateKeys(ctx, client)
6464

65+
if s.Status.State == "SYNCING" {
66+
lastRound = s.Status.LastRound
67+
cb(s, nil)
68+
continue
69+
}
6570
// Run Round Averages and RX/TX every 5 rounds
6671
if s.Status.LastRound%5 == 0 {
6772
bm, err := GetBlockMetrics(ctx, client, s.Status.LastRound, s.Metrics.Window)

ui/pages/accounts/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
2121

2222
switch msg := msg.(type) {
2323
case internal.StateModel:
24-
m.Data = msg.Accounts
24+
m.Data = &msg
2525
m.table.SetRows(*m.makeRows())
2626
case tea.KeyMsg:
2727
switch msg.String() {

ui/pages/accounts/model.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/algorandfoundation/hack-tui/ui/style"
55
"sort"
66
"strconv"
7+
"time"
78

89
"github.com/algorandfoundation/hack-tui/internal"
910
"github.com/charmbracelet/bubbles/table"
@@ -13,7 +14,7 @@ import (
1314
type ViewModel struct {
1415
Width int
1516
Height int
16-
Data map[string]internal.Account
17+
Data *internal.StateModel
1718

1819
table table.Model
1920
navigation string
@@ -24,7 +25,7 @@ func New(state *internal.StateModel) ViewModel {
2425
m := ViewModel{
2526
Width: 0,
2627
Height: 0,
27-
Data: state.Accounts,
28+
Data: state,
2829
controls: "( (g)enerate )",
2930
navigation: "| " + style.Green.Render("(a)ccounts") + " | (k)eys | (t)xn |",
3031
}
@@ -52,7 +53,7 @@ func (m ViewModel) SelectedAccount() internal.Account {
5253
var account internal.Account
5354
var selectedRow = m.table.SelectedRow()
5455
if selectedRow != nil {
55-
account = m.Data[selectedRow[0]]
56+
account = m.Data.Accounts[selectedRow[0]]
5657
}
5758
return account
5859
}
@@ -70,13 +71,20 @@ func (m ViewModel) makeColumns(width int) []table.Column {
7071
func (m ViewModel) makeRows() *[]table.Row {
7172
rows := make([]table.Row, 0)
7273

73-
for key := range m.Data {
74+
for key := range m.Data.Accounts {
75+
expires := m.Data.Accounts[key].Expires.String()
76+
if m.Data.Status.State == "SYNCING" {
77+
expires = "SYNCING"
78+
}
79+
if !m.Data.Accounts[key].Expires.After(time.Now().Add(-(time.Hour * 24 * 365 * 50))) {
80+
expires = "NA"
81+
}
7482
rows = append(rows, table.Row{
75-
m.Data[key].Address,
76-
strconv.Itoa(m.Data[key].Keys),
77-
m.Data[key].Status,
78-
m.Data[key].Expires.String(),
79-
strconv.Itoa(m.Data[key].Balance),
83+
m.Data.Accounts[key].Address,
84+
strconv.Itoa(m.Data.Accounts[key].Keys),
85+
m.Data.Accounts[key].Status,
86+
expires,
87+
strconv.Itoa(m.Data.Accounts[key].Balance),
8088
})
8189
}
8290
sort.SliceStable(rows, func(i, j int) bool {

ui/status.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,21 @@ func (m StatusViewModel) View() string {
8585
// Last Round
8686
row1 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end)
8787

88-
beginning = style.Blue.Render(" Round time: ") + fmt.Sprintf("%.2fs", float64(m.Data.Metrics.RoundTime)/float64(time.Second))
88+
roundTime := fmt.Sprintf("%.2fs", float64(m.Data.Metrics.RoundTime)/float64(time.Second))
89+
if m.Data.Status.State == "SYNCING" {
90+
roundTime = "--"
91+
}
92+
beginning = style.Blue.Render(" Round time: ") + roundTime
8993
end = getBitRate(m.Data.Metrics.TX) + style.Green.Render("TX ")
9094
middle = strings.Repeat(" ", max(0, size-(lipgloss.Width(beginning)+lipgloss.Width(end)+2)))
9195

9296
row2 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end)
9397

94-
beginning = style.Blue.Render(" TPS: ") + fmt.Sprintf("%.2f", m.Data.Metrics.TPS)
98+
tps := fmt.Sprintf("%.2f", m.Data.Metrics.TPS)
99+
if m.Data.Status.State == "SYNCING" {
100+
tps = "--"
101+
}
102+
beginning = style.Blue.Render(" TPS: ") + tps
95103
end = getBitRate(m.Data.Metrics.RX) + style.Green.Render("RX ")
96104
middle = strings.Repeat(" ", max(0, size-(lipgloss.Width(beginning)+lipgloss.Width(end)+2)))
97105

ui/viewport.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
123123
}
124124
if m.page == KeysPage {
125125
selKey := m.keysPage.SelectedKey()
126-
if selKey != nil {
126+
if selKey != nil && m.Data.Status.State != "SYNCING" {
127127
m.page = TransactionPage
128128
return m, keys.EmitKeySelected(selKey)
129129
}
@@ -143,21 +143,24 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
143143
}
144144
return m, nil
145145
case "t":
146-
if m.page == AccountsPage {
147-
acct := m.accountsPage.SelectedAccount()
148-
data := *m.Data.ParticipationKeys
149-
for i, key := range data {
150-
if key.Address == acct.Address {
151-
m.page = TransactionPage
152-
return m, keys.EmitKeySelected(&data[i])
146+
if m.Data.Status.State != "SYNCING" {
147+
148+
if m.page == AccountsPage {
149+
acct := m.accountsPage.SelectedAccount()
150+
data := *m.Data.ParticipationKeys
151+
for i, key := range data {
152+
if key.Address == acct.Address {
153+
m.page = TransactionPage
154+
return m, keys.EmitKeySelected(&data[i])
155+
}
153156
}
154157
}
155-
}
156-
if m.page == KeysPage {
157-
selKey := m.keysPage.SelectedKey()
158-
if selKey != nil {
159-
m.page = TransactionPage
160-
return m, keys.EmitKeySelected(selKey)
158+
if m.page == KeysPage {
159+
selKey := m.keysPage.SelectedKey()
160+
if selKey != nil {
161+
m.page = TransactionPage
162+
return m, keys.EmitKeySelected(selKey)
163+
}
161164
}
162165
}
163166
return m, nil

0 commit comments

Comments
 (0)