From 68ec948558931429074186cb98c0446c7b313a73 Mon Sep 17 00:00:00 2001 From: Edouard Paris Date: Fri, 29 Mar 2019 12:38:09 +0100 Subject: [PATCH] refac summary --- network/backend/backend.go | 2 +- network/backend/lnd/lnd.go | 4 +-- network/backend/lnd/proto.go | 4 +-- network/backend/mock/mock.go | 4 +-- network/models/channel.go | 4 +-- ui/controller.go | 17 +++++++----- ui/models/models.go | 50 ++++++++++++++++++++++++++++-------- ui/ui.go | 8 +++--- ui/views/summary.go | 38 ++++++++++++++++++++------- ui/views/views.go | 2 +- 10 files changed, 94 insertions(+), 39 deletions(-) diff --git a/network/backend/backend.go b/network/backend/backend.go index 4b09173..95d3be4 100644 --- a/network/backend/backend.go +++ b/network/backend/backend.go @@ -18,7 +18,7 @@ type Backend interface { GetWalletBalance(context.Context) (*models.WalletBalance, error) - GetChannelBalance(context.Context) (*models.ChannelBalance, error) + GetChannelsBalance(context.Context) (*models.ChannelsBalance, error) ListChannels(context.Context, ...options.Channel) ([]*models.Channel, error) diff --git a/network/backend/lnd/lnd.go b/network/backend/lnd/lnd.go index 252743f..ab7a5c8 100644 --- a/network/backend/lnd/lnd.go +++ b/network/backend/lnd/lnd.go @@ -135,7 +135,7 @@ func (l Backend) GetWalletBalance(ctx context.Context) (*models.WalletBalance, e return balance, nil } -func (l Backend) GetChannelBalance(ctx context.Context) (*models.ChannelBalance, error) { +func (l Backend) GetChannelsBalance(ctx context.Context) (*models.ChannelsBalance, error) { l.logger.Debug("Retrieve channel balance...") clt, err := l.Client(ctx) @@ -150,7 +150,7 @@ func (l Backend) GetChannelBalance(ctx context.Context) (*models.ChannelBalance, return nil, errors.WithStack(err) } - balance := protoToChannelBalance(resp) + balance := protoToChannelsBalance(resp) l.logger.Debug("Channel balance retrieved", logging.Object("balance", balance)) diff --git a/network/backend/lnd/proto.go b/network/backend/lnd/proto.go index 66505eb..405dd5e 100644 --- a/network/backend/lnd/proto.go +++ b/network/backend/lnd/proto.go @@ -14,8 +14,8 @@ func protoToWalletBalance(w *lnrpc.WalletBalanceResponse) *models.WalletBalance } } -func protoToChannelBalance(w *lnrpc.ChannelBalanceResponse) *models.ChannelBalance { - return &models.ChannelBalance{ +func protoToChannelsBalance(w *lnrpc.ChannelBalanceResponse) *models.ChannelsBalance { + return &models.ChannelsBalance{ PendingOpenBalance: w.GetPendingOpenBalance(), Balance: w.GetBalance(), } diff --git a/network/backend/mock/mock.go b/network/backend/mock/mock.go index 3a708ab..ca2b78b 100644 --- a/network/backend/mock/mock.go +++ b/network/backend/mock/mock.go @@ -46,8 +46,8 @@ func (b *Backend) GetWalletBalance(ctx context.Context) (*models.WalletBalance, return &models.WalletBalance{}, nil } -func (b *Backend) GetChannelBalance(ctx context.Context) (*models.ChannelBalance, error) { - return &models.ChannelBalance{}, nil +func (b *Backend) GetChannelsBalance(ctx context.Context) (*models.ChannelsBalance, error) { + return &models.ChannelsBalance{}, nil } func (b *Backend) ListChannels(ctx context.Context, opt ...options.Channel) ([]*models.Channel, error) { diff --git a/network/models/channel.go b/network/models/channel.go index 6e98e36..c06c272 100644 --- a/network/models/channel.go +++ b/network/models/channel.go @@ -2,12 +2,12 @@ package models import "github.com/edouardparis/lntop/logging" -type ChannelBalance struct { +type ChannelsBalance struct { Balance int64 PendingOpenBalance int64 } -func (m ChannelBalance) MarshalLogObject(enc logging.ObjectEncoder) error { +func (m ChannelsBalance) MarshalLogObject(enc logging.ObjectEncoder) error { enc.AddInt64("balance", m.Balance) enc.AddInt64("pending_open_balance", m.PendingOpenBalance) diff --git a/ui/controller.go b/ui/controller.go index a88d0d1..074fe96 100644 --- a/ui/controller.go +++ b/ui/controller.go @@ -46,17 +46,22 @@ func cursorUp(g *gocui.Gui, v *gocui.View) error { return nil } -func (c *controller) Update(ctx context.Context) error { +func (c *controller) SetModels(ctx context.Context) error { err := c.models.RefreshInfo(ctx) if err != nil { return err } - // c.views.Summary.UpdateChannelsStats( - // info.NumPendingChannels, - // info.NumActiveChannels, - // info.NumInactiveChannels, - //) + err = c.models.RefreshWalletBalance(ctx) + if err != nil { + return err + } + + err = c.models.RefreshChannelsBalance(ctx) + if err != nil { + return err + } + return c.models.RefreshChannels(ctx) } diff --git a/ui/models/models.go b/ui/models/models.go index 9207313..3ef8864 100644 --- a/ui/models/models.go +++ b/ui/models/models.go @@ -8,19 +8,27 @@ import ( ) type Models struct { - App *app.App - Info *Info - Channels *Channels + App *app.App + Info *Info + Channels *Channels + WalletBalance *WalletBalance + ChannelsBalance *ChannelsBalance } func New(app *app.App) *Models { return &Models{ - App: app, - Info: &Info{}, - Channels: &Channels{}, + App: app, + Info: &Info{}, + Channels: &Channels{}, + WalletBalance: &WalletBalance{}, + ChannelsBalance: &ChannelsBalance{}, } } +type Info struct { + *models.Info +} + func (m *Models) RefreshInfo(ctx context.Context) error { info, err := m.App.Network.Info(ctx) if err != nil { @@ -30,6 +38,10 @@ func (m *Models) RefreshInfo(ctx context.Context) error { return nil } +type Channels struct { + Items []*models.Channel +} + func (m *Models) RefreshChannels(ctx context.Context) error { channels, err := m.App.Network.ListChannels(ctx) if err != nil { @@ -39,10 +51,28 @@ func (m *Models) RefreshChannels(ctx context.Context) error { return nil } -type Info struct { - *models.Info +type WalletBalance struct { + *models.WalletBalance } -type Channels struct { - Items []*models.Channel +func (m *Models) RefreshWalletBalance(ctx context.Context) error { + balance, err := m.App.Network.GetWalletBalance(ctx) + if err != nil { + return err + } + *m.WalletBalance = WalletBalance{balance} + return nil +} + +type ChannelsBalance struct { + *models.ChannelsBalance +} + +func (m *Models) RefreshChannelsBalance(ctx context.Context) error { + balance, err := m.App.Network.GetChannelsBalance(ctx) + if err != nil { + return err + } + *m.ChannelsBalance = ChannelsBalance{balance} + return nil } diff --git a/ui/ui.go b/ui/ui.go index 287a484..6a2e960 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -17,14 +17,14 @@ func Run(ctx context.Context, app *app.App) error { g.Cursor = true ctrl := newController(app) - g.SetManagerFunc(ctrl.layout) - - err = ctrl.setKeyBinding(g) + err = ctrl.SetModels(ctx) if err != nil { return err } - err = ctrl.Update(ctx) + g.SetManagerFunc(ctrl.layout) + + err = ctrl.setKeyBinding(g) if err != nil { return err } diff --git a/ui/views/summary.go b/ui/views/summary.go index 9eac896..1eda0be 100644 --- a/ui/views/summary.go +++ b/ui/views/summary.go @@ -14,14 +14,16 @@ const ( ) type Summary struct { - left *gocui.View - right *gocui.View - info *models.Info + left *gocui.View + right *gocui.View + info *models.Info + channelsBalance *models.ChannelsBalance + walletBalance *models.WalletBalance } func (s *Summary) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { var err error - s.left, err = g.SetView(SUMMARY_LEFT, x0, y0, x0+40, y1) + s.left, err = g.SetView(SUMMARY_LEFT, x0, y0, x1/2, y1) if err != nil { if err != gocui.ErrUnknownView { return err @@ -29,7 +31,7 @@ func (s *Summary) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { } s.left.Frame = false - s.right, err = g.SetView(SUMMARY_RIGHT, x0+40, y0, x1, y1) + s.right, err = g.SetView(SUMMARY_RIGHT, x1/2, y0, x1, y1) if err != nil { if err != gocui.ErrUnknownView { return err @@ -43,15 +45,33 @@ func (s *Summary) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { func (s *Summary) display() { s.left.Clear() fmt.Fprintln(s.left, color.Green("[ Channels ]")) - fmt.Fprintln(s.left, fmt.Sprintf("%d %s %d %s %d %s", - s.info.NumPendingChannels, color.Yellow("pending"), + fmt.Fprintln(s.left, fmt.Sprintf("%s %d (%s|%s)", + color.Cyan("balance:"), + s.channelsBalance.Balance+s.channelsBalance.PendingOpenBalance, + color.Green(fmt.Sprintf("%d", s.channelsBalance.Balance)), + color.Yellow(fmt.Sprintf("%d", s.channelsBalance.PendingOpenBalance)), + )) + fmt.Fprintln(s.left, fmt.Sprintf("%s %d %s %d %s %d %s", + color.Cyan("state :"), s.info.NumActiveChannels, color.Green("active"), + s.info.NumPendingChannels, color.Yellow("pending"), s.info.NumInactiveChannels, color.Red("inactive"), )) s.right.Clear() + fmt.Fprintln(s.right, color.Green("[ Wallet ]")) + fmt.Fprintln(s.right, fmt.Sprintf("%s %d (%s|%s)", + color.Cyan("balance:"), + s.walletBalance.TotalBalance, + color.Green(fmt.Sprintf("%d", s.walletBalance.ConfirmedBalance)), + color.Yellow(fmt.Sprintf("%d", s.walletBalance.UnconfirmedBalance)), + )) } -func NewSummary(info *models.Info) *Summary { - return &Summary{info: info} +func NewSummary(info *models.Info, channelsBalance *models.ChannelsBalance, walletBalance *models.WalletBalance) *Summary { + return &Summary{ + info: info, + channelsBalance: channelsBalance, + walletBalance: walletBalance, + } } diff --git a/ui/views/views.go b/ui/views/views.go index 72f35f9..0d94447 100644 --- a/ui/views/views.go +++ b/ui/views/views.go @@ -35,7 +35,7 @@ func New(m *models.Models) *Views { return &Views{ Header: NewHeader(m.Info), Footer: NewFooter(), - Summary: NewSummary(m.Info), + Summary: NewSummary(m.Info, m.ChannelsBalance, m.WalletBalance), Channels: NewChannels(m.Channels), } }