Skip to content

Commit

Permalink
Merge pull request #23 from algorandfoundation/feat/disable-ui-for-sync
Browse files Browse the repository at this point in the history
feat: disable UI for sync
  • Loading branch information
PhearZero authored Nov 6, 2024
2 parents 0017855 + ff0348b commit 5b53ea3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 36 deletions.
25 changes: 15 additions & 10 deletions internal/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,23 @@ func AccountsFromState(state *StateModel, t Time, client *api.ClientWithResponse
for _, key := range *state.ParticipationKeys {
val, ok := values[key.Address]
if !ok {

account, err := GetAccount(client, key.Address)

// TODO: handle error
if err != nil {
// TODO: Logging
panic(err)
var account = api.Account{
Address: key.Address,
Status: "Unknown",
Amount: 0,
}

var expires = t.Now()
if state.Status.State != "SYNCING" {
var err error
account, err = GetAccount(client, key.Address)
// TODO: handle error
if err != nil {
// TODO: Logging
panic(err)
}
}
now := t.Now()
var expires = now.Add(-(time.Hour * 24 * 365 * 100))
if key.EffectiveLastValid != nil {
now := t.Now()
roundDiff := max(0, *key.EffectiveLastValid-int(state.Status.LastRound))
distance := int(state.Metrics.RoundTime) * roundDiff
expires = now.Add(time.Duration(distance))
Expand Down
5 changes: 5 additions & 0 deletions internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
// Fetch Keys
s.UpdateKeys(ctx, client)

if s.Status.State == "SYNCING" {
lastRound = s.Status.LastRound
cb(s, nil)
continue
}
// Run Round Averages and RX/TX every 5 rounds
if s.Status.LastRound%5 == 0 {
bm, err := GetBlockMetrics(ctx, client, s.Status.LastRound, s.Metrics.Window)
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/accounts/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {

switch msg := msg.(type) {
case internal.StateModel:
m.Data = msg.Accounts
m.Data = &msg
m.table.SetRows(*m.makeRows())
case tea.KeyMsg:
switch msg.String() {
Expand Down
26 changes: 17 additions & 9 deletions ui/pages/accounts/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/algorandfoundation/hack-tui/ui/style"
"sort"
"strconv"
"time"

"github.com/algorandfoundation/hack-tui/internal"
"github.com/charmbracelet/bubbles/table"
Expand All @@ -13,7 +14,7 @@ import (
type ViewModel struct {
Width int
Height int
Data map[string]internal.Account
Data *internal.StateModel

table table.Model
navigation string
Expand All @@ -24,7 +25,7 @@ func New(state *internal.StateModel) ViewModel {
m := ViewModel{
Width: 0,
Height: 0,
Data: state.Accounts,
Data: state,
controls: "( (g)enerate )",
navigation: "| " + style.Green.Render("(a)ccounts") + " | (k)eys | (t)xn |",
}
Expand Down Expand Up @@ -52,7 +53,7 @@ func (m ViewModel) SelectedAccount() internal.Account {
var account internal.Account
var selectedRow = m.table.SelectedRow()
if selectedRow != nil {
account = m.Data[selectedRow[0]]
account = m.Data.Accounts[selectedRow[0]]
}
return account
}
Expand All @@ -70,13 +71,20 @@ func (m ViewModel) makeColumns(width int) []table.Column {
func (m ViewModel) makeRows() *[]table.Row {
rows := make([]table.Row, 0)

for key := range m.Data {
for key := range m.Data.Accounts {
expires := m.Data.Accounts[key].Expires.String()
if m.Data.Status.State == "SYNCING" {
expires = "SYNCING"
}
if !m.Data.Accounts[key].Expires.After(time.Now().Add(-(time.Hour * 24 * 365 * 50))) {
expires = "NA"
}
rows = append(rows, table.Row{
m.Data[key].Address,
strconv.Itoa(m.Data[key].Keys),
m.Data[key].Status,
m.Data[key].Expires.String(),
strconv.Itoa(m.Data[key].Balance),
m.Data.Accounts[key].Address,
strconv.Itoa(m.Data.Accounts[key].Keys),
m.Data.Accounts[key].Status,
expires,
strconv.Itoa(m.Data.Accounts[key].Balance),
})
}
sort.SliceStable(rows, func(i, j int) bool {
Expand Down
12 changes: 10 additions & 2 deletions ui/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,21 @@ func (m StatusViewModel) View() string {
// Last Round
row1 := lipgloss.JoinHorizontal(lipgloss.Left, beginning, middle, end)

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

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

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

Expand Down
31 changes: 17 additions & 14 deletions ui/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
if m.page == KeysPage {
selKey := m.keysPage.SelectedKey()
if selKey != nil {
if selKey != nil && m.Data.Status.State != "SYNCING" {
m.page = TransactionPage
return m, keys.EmitKeySelected(selKey)
}
Expand All @@ -143,21 +143,24 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
return m, nil
case "t":
if m.page == AccountsPage {
acct := m.accountsPage.SelectedAccount()
data := *m.Data.ParticipationKeys
for i, key := range data {
if key.Address == acct.Address {
m.page = TransactionPage
return m, keys.EmitKeySelected(&data[i])
if m.Data.Status.State != "SYNCING" {

if m.page == AccountsPage {
acct := m.accountsPage.SelectedAccount()
data := *m.Data.ParticipationKeys
for i, key := range data {
if key.Address == acct.Address {
m.page = TransactionPage
return m, keys.EmitKeySelected(&data[i])
}
}
}
}
if m.page == KeysPage {
selKey := m.keysPage.SelectedKey()
if selKey != nil {
m.page = TransactionPage
return m, keys.EmitKeySelected(selKey)
if m.page == KeysPage {
selKey := m.keysPage.SelectedKey()
if selKey != nil {
m.page = TransactionPage
return m, keys.EmitKeySelected(selKey)
}
}
}
return m, nil
Expand Down

0 comments on commit 5b53ea3

Please sign in to comment.