2
0
mirror of https://github.com/edouardparis/lntop synced 2024-11-16 00:12:44 +00:00
lntop/ui/models/channels.go

126 lines
2.6 KiB
Go
Raw Normal View History

2019-04-02 12:27:51 +00:00
package models
2019-04-08 12:35:51 +00:00
import (
"sort"
2019-04-08 12:35:51 +00:00
"sync"
"github.com/edouardparis/lntop/network/models"
)
2019-04-02 12:27:51 +00:00
type ChannelsSort func(*models.Channel, *models.Channel) bool
2019-04-02 12:27:51 +00:00
type Channels struct {
current *models.Channel
index map[string]*models.Channel
list []*models.Channel
sort ChannelsSort
mu sync.RWMutex
2019-04-03 12:11:49 +00:00
}
2019-04-08 12:35:51 +00:00
func (c *Channels) List() []*models.Channel {
2019-04-03 12:11:49 +00:00
return c.list
2019-04-02 12:27:51 +00:00
}
func (c *Channels) Len() int {
return len(c.list)
}
func (c *Channels) Swap(i, j int) {
c.list[i], c.list[j] = c.list[j], c.list[i]
}
func (c *Channels) Less(i, j int) bool {
return c.sort(c.list[i], c.list[j])
}
func (c *Channels) Sort(s ChannelsSort) {
if s == nil {
return
}
c.sort = s
sort.Sort(c)
}
func (c *Channels) Current() *models.Channel {
return c.current
}
func (c *Channels) SetCurrent(index int) {
c.current = c.Get(index)
}
2019-04-02 12:27:51 +00:00
func (c *Channels) Get(index int) *models.Channel {
2019-04-03 12:11:49 +00:00
if index < 0 || index > len(c.list)-1 {
2019-04-02 12:27:51 +00:00
return nil
}
2019-04-03 12:11:49 +00:00
return c.list[index]
}
2019-04-10 13:02:09 +00:00
func (c *Channels) GetByChanPoint(chanPoint string) *models.Channel {
return c.index[chanPoint]
2019-04-03 12:11:49 +00:00
}
2019-04-08 12:35:51 +00:00
func (c *Channels) Contains(channel *models.Channel) bool {
2019-04-10 13:02:09 +00:00
_, ok := c.index[channel.ChannelPoint]
2019-04-03 12:11:49 +00:00
return ok
}
func (c *Channels) Add(channel *models.Channel) {
2019-04-08 12:35:51 +00:00
c.mu.Lock()
defer c.mu.Unlock()
2019-04-03 12:11:49 +00:00
if c.Contains(channel) {
return
}
2019-04-10 13:02:09 +00:00
c.index[channel.ChannelPoint] = channel
2019-04-03 12:11:49 +00:00
c.list = append(c.list, channel)
}
func (c *Channels) Update(newChannel *models.Channel) {
2019-04-08 12:35:51 +00:00
c.mu.Lock()
defer c.mu.Unlock()
2019-04-10 13:02:09 +00:00
oldChannel, ok := c.index[newChannel.ChannelPoint]
2019-04-03 12:11:49 +00:00
if !ok {
c.Add(newChannel)
if c.sort != nil {
sort.Sort(c)
}
2019-04-03 12:11:49 +00:00
return
}
2019-04-11 07:57:00 +00:00
oldChannel.ID = newChannel.ID
2019-04-08 11:38:21 +00:00
oldChannel.Status = newChannel.Status
2019-04-03 12:11:49 +00:00
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
2019-04-04 08:10:21 +00:00
if newChannel.LastUpdate != nil {
oldChannel.LastUpdate = newChannel.LastUpdate
2019-04-03 12:11:49 +00:00
}
2019-04-05 15:28:36 +00:00
if newChannel.Policy1 != nil {
oldChannel.Policy1 = newChannel.Policy1
}
if newChannel.Policy2 != nil {
oldChannel.Policy2 = newChannel.Policy2
}
2019-04-03 12:11:49 +00:00
}
func NewChannels() *Channels {
return &Channels{
list: []*models.Channel{},
2019-04-10 13:02:09 +00:00
index: make(map[string]*models.Channel),
2019-04-03 12:11:49 +00:00
}
2019-04-02 12:27:51 +00:00
}