From 0bfdf836820c9cc06ecd954e9cd5948ce0ffa73a Mon Sep 17 00:00:00 2001 From: Edouard Paris Date: Wed, 3 Apr 2019 14:11:49 +0200 Subject: [PATCH] refac channels --- network/models/channel.go | 7 ++++- ui/controller.go | 2 +- ui/models/channels.go | 60 +++++++++++++++++++++++++++++++++++++-- ui/models/models.go | 15 ++++++++-- ui/views/channels.go | 14 +++++++-- ui/views/summary.go | 2 +- 6 files changed, 90 insertions(+), 10 deletions(-) diff --git a/network/models/channel.go b/network/models/channel.go index c06c272..01593c1 100644 --- a/network/models/channel.go +++ b/network/models/channel.go @@ -1,6 +1,10 @@ package models -import "github.com/edouardparis/lntop/logging" +import ( + "time" + + "github.com/edouardparis/lntop/logging" +) type ChannelsBalance struct { Balance int64 @@ -32,6 +36,7 @@ type Channel struct { CSVDelay uint32 Private bool PendingHTLC []*HTLC + LastUpdated *time.Time } func (m Channel) MarshalLogObject(enc logging.ObjectEncoder) error { diff --git a/ui/controller.go b/ui/controller.go index 6360e26..1c80838 100644 --- a/ui/controller.go +++ b/ui/controller.go @@ -145,7 +145,7 @@ func (c *controller) OnEnter(g *gocui.Gui, v *gocui.View) error { return err case views.CHANNEL: - err := g.DeleteView(view.Name()) + err := c.views.Channel.Delete(g) if err != nil { return err } diff --git a/ui/models/channels.go b/ui/models/channels.go index 38fe7a2..909f5d5 100644 --- a/ui/models/channels.go +++ b/ui/models/channels.go @@ -3,15 +3,69 @@ package models import "github.com/edouardparis/lntop/network/models" type Channels struct { - Items []*models.Channel + index map[uint64]*models.Channel + list []*models.Channel +} + +func (c Channels) List() []*models.Channel { + return c.list } func (c *Channels) Get(index int) *models.Channel { - if index < 0 || index > len(c.Items)-1 { + if index < 0 || index > len(c.list)-1 { return nil } - return c.Items[index] + return c.list[index] +} + +func (c *Channels) GetByID(id uint64) *models.Channel { + return c.index[id] +} + +func (c Channels) Contains(channel *models.Channel) bool { + _, ok := c.index[channel.ID] + return ok +} + +func (c *Channels) Add(channel *models.Channel) { + if c.Contains(channel) { + return + } + c.index[channel.ID] = channel + c.list = append(c.list, channel) +} + +func (c *Channels) Update(newChannel *models.Channel) { + oldChannel, ok := c.index[newChannel.ID] + if !ok { + c.Add(newChannel) + return + } + oldChannel.Active = newChannel.Active + oldChannel.LocalBalance = newChannel.LocalBalance + oldChannel.RemoteBalance = newChannel.RemoteBalance + oldChannel.CommitFee = newChannel.CommitFee + oldChannel.CommitWeight = newChannel.CommitWeight + oldChannel.FeePerKiloWeight = newChannel.FeePerKiloWeight + oldChannel.UnsettledBalance = newChannel.UnsettledBalance + oldChannel.TotalAmountSent = newChannel.TotalAmountSent + oldChannel.TotalAmountReceived = newChannel.TotalAmountReceived + oldChannel.UpdatesCount = newChannel.UpdatesCount + oldChannel.CSVDelay = newChannel.CSVDelay + oldChannel.Private = newChannel.Private + oldChannel.PendingHTLC = newChannel.PendingHTLC + + if newChannel.LastUpdated != nil { + oldChannel.LastUpdated = newChannel.LastUpdated + } +} + +func NewChannels() *Channels { + return &Channels{ + list: []*models.Channel{}, + index: make(map[uint64]*models.Channel), + } } type Channel struct { diff --git a/ui/models/models.go b/ui/models/models.go index e63ff5c..a2fdd8e 100644 --- a/ui/models/models.go +++ b/ui/models/models.go @@ -20,7 +20,7 @@ func New(app *app.App) *Models { return &Models{ App: app, Info: &Info{}, - Channels: &Channels{}, + Channels: NewChannels(), WalletBalance: &WalletBalance{}, ChannelsBalance: &ChannelsBalance{}, CurrentChannel: &Channel{}, @@ -45,7 +45,18 @@ func (m *Models) RefreshChannels(ctx context.Context) error { if err != nil { return err } - *m.Channels = Channels{Items: channels} + for i := range channels { + if !m.Channels.Contains(channels[i]) { + m.Channels.Add(channels[i]) + continue + } + channel := m.Channels.GetByID(channels[i].ID) + if channel != nil && + (channel.UpdatesCount < channels[i].UpdatesCount || + channel.LastUpdated == nil) { + m.Channels.Update(channels[i]) + } + } return nil } diff --git a/ui/views/channels.go b/ui/views/channels.go index b0b7143..385e8b0 100644 --- a/ui/views/channels.go +++ b/ui/views/channels.go @@ -13,6 +13,7 @@ import ( const ( CHANNEL = "channel" + CHANNEL_HEADER = "channel_header" CHANNELS = "channels" CHANNELS_COLUMNS = "channels_columns" ) @@ -58,6 +59,7 @@ func (c *Channels) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { } func displayChannelsColumns(v *gocui.View) { + v.Clear() fmt.Fprintln(v, fmt.Sprintf("%-9s %-26s %12s %12s %5s", "Status", "Gauge", @@ -69,7 +71,7 @@ func displayChannelsColumns(v *gocui.View) { func (c *Channels) display(v *gocui.View) { v.Clear() - for _, item := range c.channels.Items { + for _, item := range c.channels.List() { line := fmt.Sprintf("%s %s %s %12d %5d %500s", active(item), gauge(item), @@ -119,7 +121,7 @@ func (c Channel) Empty() bool { } func (c *Channel) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { - header, err := g.SetView(CHANNELS_COLUMNS, x0-1, y0, x1+2, y0+2) + header, err := g.SetView(CHANNEL_HEADER, x0-1, y0, x1+2, y0+2) if err != nil { if err != gocui.ErrUnknownView { return err @@ -142,6 +144,14 @@ func (c *Channel) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { return nil } +func (c Channel) Delete(g *gocui.Gui) error { + err := g.DeleteView(CHANNEL_HEADER) + if err != nil { + return err + } + return g.DeleteView(CHANNEL) +} + func (c *Channel) display(v *gocui.View) { v.Clear() channel := c.channel.Item diff --git a/ui/views/summary.go b/ui/views/summary.go index 4d7e896..c5a7dea 100644 --- a/ui/views/summary.go +++ b/ui/views/summary.go @@ -64,7 +64,7 @@ func (s *Summary) display() { )) fmt.Fprintln(s.left, fmt.Sprintf("%s %s", color.Cyan("gauge :"), - gaugeTotal(s.channelsBalance.Balance, s.channels.Items), + gaugeTotal(s.channelsBalance.Balance, s.channels.List()), )) s.right.Clear()