refac channels

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

@ -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 {

@ -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
}

@ -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 {

@ -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
}

@ -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

@ -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()

Loading…
Cancel
Save