refac channels

pull/1/head
Edouard Paris 5 years ago
parent 1457c7772f
commit 0bfdf83682

@ -1,6 +1,10 @@
package models package models
import "github.com/edouardparis/lntop/logging" import (
"time"
"github.com/edouardparis/lntop/logging"
)
type ChannelsBalance struct { type ChannelsBalance struct {
Balance int64 Balance int64
@ -32,6 +36,7 @@ type Channel struct {
CSVDelay uint32 CSVDelay uint32
Private bool Private bool
PendingHTLC []*HTLC PendingHTLC []*HTLC
LastUpdated *time.Time
} }
func (m Channel) MarshalLogObject(enc logging.ObjectEncoder) error { func (m Channel) MarshalLogObject(enc logging.ObjectEncoder) error {

@ -145,7 +145,7 @@ func (c *controller) OnEnter(g *gocui.Gui, v *gocui.View) error {
return err return err
case views.CHANNEL: case views.CHANNEL:
err := g.DeleteView(view.Name()) err := c.views.Channel.Delete(g)
if err != nil { if err != nil {
return err return err
} }

@ -3,15 +3,69 @@ package models
import "github.com/edouardparis/lntop/network/models" import "github.com/edouardparis/lntop/network/models"
type Channels struct { 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 { 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 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 { type Channel struct {

@ -20,7 +20,7 @@ func New(app *app.App) *Models {
return &Models{ return &Models{
App: app, App: app,
Info: &Info{}, Info: &Info{},
Channels: &Channels{}, Channels: NewChannels(),
WalletBalance: &WalletBalance{}, WalletBalance: &WalletBalance{},
ChannelsBalance: &ChannelsBalance{}, ChannelsBalance: &ChannelsBalance{},
CurrentChannel: &Channel{}, CurrentChannel: &Channel{},
@ -45,7 +45,18 @@ func (m *Models) RefreshChannels(ctx context.Context) error {
if err != nil { if err != nil {
return err 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 return nil
} }

@ -13,6 +13,7 @@ import (
const ( const (
CHANNEL = "channel" CHANNEL = "channel"
CHANNEL_HEADER = "channel_header"
CHANNELS = "channels" CHANNELS = "channels"
CHANNELS_COLUMNS = "channels_columns" 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) { func displayChannelsColumns(v *gocui.View) {
v.Clear()
fmt.Fprintln(v, fmt.Sprintf("%-9s %-26s %12s %12s %5s", fmt.Fprintln(v, fmt.Sprintf("%-9s %-26s %12s %12s %5s",
"Status", "Status",
"Gauge", "Gauge",
@ -69,7 +71,7 @@ func displayChannelsColumns(v *gocui.View) {
func (c *Channels) display(v *gocui.View) { func (c *Channels) display(v *gocui.View) {
v.Clear() v.Clear()
for _, item := range c.channels.Items { for _, item := range c.channels.List() {
line := fmt.Sprintf("%s %s %s %12d %5d %500s", line := fmt.Sprintf("%s %s %s %12d %5d %500s",
active(item), active(item),
gauge(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 { 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 != nil {
if err != gocui.ErrUnknownView { if err != gocui.ErrUnknownView {
return err return err
@ -142,6 +144,14 @@ func (c *Channel) Set(g *gocui.Gui, x0, y0, x1, y1 int) error {
return nil 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) { func (c *Channel) display(v *gocui.View) {
v.Clear() v.Clear()
channel := c.channel.Item channel := c.channel.Item

@ -64,7 +64,7 @@ func (s *Summary) display() {
)) ))
fmt.Fprintln(s.left, fmt.Sprintf("%s %s", fmt.Fprintln(s.left, fmt.Sprintf("%s %s",
color.Cyan("gauge :"), color.Cyan("gauge :"),
gaugeTotal(s.channelsBalance.Balance, s.channels.Items), gaugeTotal(s.channelsBalance.Balance, s.channels.List()),
)) ))
s.right.Clear() s.right.Clear()

Loading…
Cancel
Save