Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] ci-test #18

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package cmd
import (
"context"
"encoding/json"
"io"
"os"
"strings"

"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/internal"
"github.com/algorandfoundation/hack-tui/ui"
Expand All @@ -15,6 +11,9 @@ import (
"github.com/oapi-codegen/oapi-codegen/v2/pkg/securityprovider"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io"
"os"
"strings"
)

const BANNER = `
Expand Down Expand Up @@ -43,6 +42,7 @@ var (
cobra.CheckErr(err)

partkeys, err := internal.GetPartKeys(context.Background(), client)
cobra.CheckErr(err)

state := internal.StateModel{
Status: internal.StatusModel{
Expand Down Expand Up @@ -73,13 +73,15 @@ var (
p := tea.NewProgram(
m,
tea.WithAltScreen(),
tea.WithFPS(120),
)
go func() {
state.Watch(func(status *internal.StateModel, err error) {
if err == nil {
p.Send(state)
}
if err != nil {
p.Send(state)
p.Send(err)
}
}, context.Background(), client)
Expand Down
3 changes: 2 additions & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (

// Test the stub root command
func Test_ExecuteRootCommand(t *testing.T) {
viper.Set("server", "https://mainnet-api.4160.nodely.dev:443")
viper.Set("token", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
viper.Set("server", "http://localhost:8080")

// Execute
err := rootCmd.Execute()
Expand Down
28 changes: 19 additions & 9 deletions internal/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type Account struct {
LastModified int
}

// Get Online Status of Account
func getAccountOnlineStatus(client *api.ClientWithResponses, address string) (string, error) {
// GetAccount get online status of Account
func GetAccount(client *api.ClientWithResponses, address string) (api.Account, error) {
var format api.AccountInformationParamsFormat = "json"
r, err := client.AccountInformationWithResponse(
context.Background(),
Expand All @@ -37,15 +37,16 @@ func getAccountOnlineStatus(client *api.ClientWithResponses, address string) (st
Format: &format,
})

var accountInfo api.Account
if err != nil {
return "N/A", err
return accountInfo, err
}

if r.StatusCode() != 200 {
return "N/A", errors.New(fmt.Sprintf("Failed to get account information. Received error code: %d", r.StatusCode()))
return accountInfo, errors.New(fmt.Sprintf("Failed to get account information. Received error code: %d", r.StatusCode()))
}

return r.JSON200.Status, nil
return *r.JSON200, nil
}

// AccountsFromParticipationKeys maps an array of api.ParticipationKey to a keyed map of Account
Expand All @@ -58,18 +59,27 @@ func AccountsFromState(state *StateModel, client *api.ClientWithResponses) map[s
val, ok := values[key.Address]
if !ok {

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

// TODO: handle error
if err != nil {
// TODO: Logging
panic(err)
}

var expires = time.Now()
if key.EffectiveLastValid != nil {
now := time.Now()
roundDiff := max(0, *key.EffectiveLastValid-int(state.Status.LastRound))
distance := int(state.Metrics.RoundTime) * roundDiff
expires = now.Add(time.Duration(distance))
}

values[key.Address] = Account{
Address: key.Address,
Status: statusOnline,
Balance: 0,
Expires: time.Unix(0, 0),
Status: account.Status,
Balance: account.Amount / 1000000,
Expires: expires,
Keys: 1,
}
} else {
Expand Down
17 changes: 15 additions & 2 deletions internal/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,28 @@ func GetBlockMetrics(ctx context.Context, client *api.ClientWithResponses, round
if err != nil {
return nil, err
}
if a.StatusCode() != 200 {
return nil, errors.New("invalid status code")
}
b, err := client.GetBlockWithResponse(ctx, int(round)-window, &api.GetBlockParams{
Format: &format,
})
if a.StatusCode() != 200 {
return nil, errors.New("invalid status code")
}
if err != nil {
return nil, err
}

// Push to the transactions count list
aTimestamp := time.Duration(a.JSON200.Block["ts"].(float64)) * time.Second
bTimestamp := time.Duration(b.JSON200.Block["ts"].(float64)) * time.Second
aTimestampRes := a.JSON200.Block["ts"]
bTimestampRes := b.JSON200.Block["ts"]
if aTimestampRes == nil || bTimestampRes == nil {
return &avgs, nil
}
aTimestamp := time.Duration(aTimestampRes.(float64)) * time.Second
bTimestamp := time.Duration(bTimestampRes.(float64)) * time.Second

// Transaction Counter
aTransactions := a.JSON200.Block["tc"]
bTransactions := b.JSON200.Block["tc"]
Expand Down
21 changes: 17 additions & 4 deletions internal/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"context"
"errors"
"time"

"github.com/algorandfoundation/hack-tui/api"
)
Expand All @@ -17,6 +18,14 @@ type StateModel struct {
Watching bool
}

func (s *StateModel) waitAfterError(err error, cb func(model *StateModel, err error)) {
if err != nil {
s.Status.State = "DOWN"
cb(nil, err)
time.Sleep(time.Second * 3)
}
}

// TODO: allow context to handle loop
func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Context, client *api.ClientWithResponses) {
s.Watching = true
Expand All @@ -36,14 +45,17 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
break
}
status, err := client.WaitForBlockWithResponse(ctx, int(lastRound))
s.waitAfterError(err, cb)
if err != nil {
cb(nil, err)
continue
}
if status.StatusCode() != 200 {
cb(nil, errors.New(status.Status()))
return
s.waitAfterError(errors.New(status.Status()), cb)
continue
}

s.Status.State = "Unknown"

// Update Status
s.Status.Update(status.JSON200.LastRound, status.JSON200.CatchupTime, status.JSON200.UpgradeNodeVote)

Expand All @@ -53,8 +65,9 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
// 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)
s.waitAfterError(err, cb)
if err != nil {
cb(nil, err)
continue
}
s.Metrics.RoundTime = bm.AvgTime
s.Metrics.TPS = bm.TPS
Expand Down
3 changes: 2 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

func Test_Main(t *testing.T) {
viper.Set("server", "https://mainnet-api.4160.nodely.dev")
viper.Set("token", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
viper.Set("server", "http://localhost:8080")
main()
}
2 changes: 1 addition & 1 deletion ui/controls/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (m Model) View() string {
return ""
}
render := controlStyle.Render(m.Content)
difference := m.Width - lipgloss.Width(render)
difference := m.Width - lipgloss.Width(render) - 2
line := strings.Repeat("─", max(0, difference/2))
return lipgloss.JoinHorizontal(lipgloss.Center, line, render, line)
}
10 changes: 7 additions & 3 deletions ui/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ func (m ErrorViewModel) HandleMessage(msg tea.Msg) (ErrorViewModel, tea.Cmd) {

case tea.WindowSizeMsg:
m.Width = msg.Width
m.Height = msg.Height - 2
m.Height = msg.Height
}
m.controls, cmd = m.controls.HandleMessage(msg)
return m, cmd
}

func (m ErrorViewModel) View() string {
pad := strings.Repeat("\n", max(0, m.Height/2-1))
return lipgloss.JoinVertical(lipgloss.Center, pad, red.Render(m.Message), pad, m.controls.View())
ctls := m.controls.View()
controlHeight := lipgloss.Height(ctls)
msg := red.Render(m.Message)
msgHeight := lipgloss.Height(msg)
pad := strings.Repeat("\n", max(0, (m.Height/2)-controlHeight-msgHeight))
return lipgloss.JoinVertical(lipgloss.Center, pad, msg, pad, ctls)
}
10 changes: 7 additions & 3 deletions ui/pages/accounts/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
return m, tea.Quit
}
case tea.WindowSizeMsg:
m.table.SetWidth(msg.Width - lipgloss.Width(pages.Padding1("")) - 4)
m.table.SetHeight(msg.Height - lipgloss.Height(pages.Padding1("")) - lipgloss.Height(m.controls.View()))
m.table.SetColumns(m.makeColumns(msg.Width - lipgloss.Width(pages.Padding1("")) - 14))
m.Width = msg.Width
m.Height = msg.Height

borderRender := pages.Border.Render("")
m.table.SetWidth(max(0, msg.Width-lipgloss.Width(borderRender)))
m.table.SetHeight(m.Height - lipgloss.Height(borderRender) - lipgloss.Height(m.controls.View()))
m.table.SetColumns(m.makeColumns(msg.Width - lipgloss.Width(borderRender)))
}

m.table, cmd = m.table.Update(msg)
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/accounts/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

func (m ViewModel) View() string {
return lipgloss.JoinVertical(lipgloss.Center, pages.Padding1(m.table.View()), m.controls.View())
return pages.WithTitle("Accounts", lipgloss.JoinVertical(lipgloss.Center, pages.PageBorder(m.Width-3).Render(m.table.View()), m.controls.View()))
}
13 changes: 2 additions & 11 deletions ui/pages/generate/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/internal"
"github.com/algorandfoundation/hack-tui/ui/pages/accounts"
"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
Expand All @@ -25,18 +24,10 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
//var cmd tea.Cmd

switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.Width = msg.Width
case tea.KeyMsg:
switch msg.String() {
case "ctrl+r":
m.cursorMode++
if m.cursorMode > cursor.CursorHide {
m.cursorMode = cursor.CursorBlink
}
cmds := make([]tea.Cmd, len(m.Inputs))
for i := range m.Inputs {
cmds[i] = m.Inputs[i].Cursor.SetMode(m.cursorMode)
}
return m, tea.Batch(cmds...)
case "tab", "shift+tab", "up", "down":
s := msg.String()

Expand Down
1 change: 1 addition & 0 deletions ui/pages/generate/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type ViewModel struct {
Width int
Address string
Inputs []textinput.Model
client *api.ClientWithResponses
Expand Down
10 changes: 4 additions & 6 deletions ui/pages/generate/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import (
)

var (
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
blurredStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
cursorStyle = focusedStyle
noStyle = lipgloss.NewStyle()
helpStyle = blurredStyle
cursorModeHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244"))
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
blurredStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
cursorStyle = focusedStyle
noStyle = lipgloss.NewStyle()

focusedButton = focusedStyle.Render("[ Submit ]")
blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit"))
Expand Down
7 changes: 2 additions & 5 deletions ui/pages/generate/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generate

import (
"fmt"
"github.com/algorandfoundation/hack-tui/ui/pages"
"strings"
)

Expand All @@ -21,9 +22,5 @@ func (m ViewModel) View() string {
}
fmt.Fprintf(&b, "\n\n%s\n\n", *button)

b.WriteString(helpStyle.Render("cursor mode is "))
b.WriteString(cursorModeHelpStyle.Render(m.cursorMode.String()))
b.WriteString(helpStyle.Render(" (ctrl+r to change style)"))

return b.String()
return pages.WithTitle("Generate", pages.PageBorder(m.Width-3).Render(b.String()))
}
11 changes: 8 additions & 3 deletions ui/pages/keys/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
}

case tea.WindowSizeMsg:
m.table.SetWidth(msg.Width - lipgloss.Width(pages.Padding1("")) - 4)
m.table.SetHeight(msg.Height - lipgloss.Height(pages.Padding1("")) - lipgloss.Height(m.controls.View()))
m.table.SetColumns(m.makeColumns(msg.Width - lipgloss.Width(pages.Padding1("")) - 14))
m.Width = msg.Width
m.Height = msg.Height + 1

borderRender := pages.Border.Render("")
tableWidth := max(0, msg.Width-lipgloss.Width(borderRender)-20)
m.table.SetWidth(tableWidth)
m.table.SetHeight(m.Height - lipgloss.Height(borderRender) - lipgloss.Height(m.controls.View()))
m.table.SetColumns(m.makeColumns(tableWidth))
}

var cmds []tea.Cmd
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/keys/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (
)

func (m ViewModel) View() string {
return lipgloss.JoinVertical(lipgloss.Center, pages.Padding1(m.table.View()), m.controls.View())
return pages.WithTitle("Keys", lipgloss.JoinVertical(lipgloss.Center, pages.PageBorder(m.Width-3).Render(m.table.View()), m.controls.View()))
}
31 changes: 29 additions & 2 deletions ui/pages/style.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package pages

import "github.com/charmbracelet/lipgloss"
import (
"github.com/charmbracelet/lipgloss"
"strings"
)

var Padding1 = lipgloss.NewStyle().Padding(1).Render
var (
Padding1 = lipgloss.NewStyle().Padding(1).Render
Border = func() lipgloss.Style {
return lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder())
}()
PageBorder = func(width int) lipgloss.Style {
return Border.
Width(width).
Padding(0).
Margin(0).
BorderForeground(lipgloss.Color("8"))
}
)

func WithTitle(title string, view string) string {
r := []rune(view)
if lipgloss.Width(view) >= len(title)+4 {
b, _, _, _, _ := Border.GetBorder()
id := strings.IndexRune(view, []rune(b.Top)[0])
start := string(r[0:id])
return start + title + string(r[len(title)+id:])
}
return view
}
Loading