You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lntop/ui/models/models.go

139 lines
3.1 KiB
Go

5 years ago
package models
5 years ago
import (
"context"
"github.com/edouardparis/lntop/app"
"github.com/edouardparis/lntop/logging"
"github.com/edouardparis/lntop/network"
5 years ago
"github.com/edouardparis/lntop/network/models"
"github.com/edouardparis/lntop/network/options"
5 years ago
)
5 years ago
type Models struct {
logger logging.Logger
network *network.Network
5 years ago
Info *Info
Channels *Channels
WalletBalance *WalletBalance
ChannelsBalance *ChannelsBalance
Transactions *Transactions
3 years ago
RoutingLog *RoutingLog
5 years ago
}
func New(app *app.App) *Models {
5 years ago
return &Models{
logger: app.Logger.With(logging.String("logger", "models")),
network: app.Network,
5 years ago
Info: &Info{},
5 years ago
Channels: NewChannels(),
5 years ago
WalletBalance: &WalletBalance{},
ChannelsBalance: &ChannelsBalance{},
Transactions: &Transactions{},
3 years ago
RoutingLog: &RoutingLog{},
5 years ago
}
}
5 years ago
type Info struct {
*models.Info
}
5 years ago
func (m *Models) RefreshInfo(ctx context.Context) error {
info, err := m.network.Info(ctx)
5 years ago
if err != nil {
return err
}
*m.Info = Info{info}
return nil
}
func (m *Models) RefreshChannels(ctx context.Context) error {
channels, err := m.network.ListChannels(ctx, options.WithChannelPending)
if err != nil {
return err
}
5 years ago
for i := range channels {
if !m.Channels.Contains(channels[i]) {
m.Channels.Add(channels[i])
}
channel := m.Channels.GetByChanPoint(channels[i].ChannelPoint)
5 years ago
if channel != nil &&
(channel.UpdatesCount < channels[i].UpdatesCount ||
channel.LastUpdate == nil) {
err := m.network.GetChannelInfo(ctx, channels[i])
if err != nil {
return err
}
if channels[i].Node == nil {
channels[i].Node, err = m.network.GetNode(ctx,
channels[i].RemotePubKey)
if err != nil {
m.logger.Debug("refreshChannels: cannot find Node",
logging.String("pubkey", channels[i].RemotePubKey))
}
}
5 years ago
}
m.Channels.Update(channels[i])
5 years ago
}
5 years ago
return nil
}
5 years ago
type WalletBalance struct {
*models.WalletBalance
5 years ago
}
5 years ago
func (m *Models) RefreshWalletBalance(ctx context.Context) error {
balance, err := m.network.GetWalletBalance(ctx)
5 years ago
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.network.GetChannelsBalance(ctx)
5 years ago
if err != nil {
return err
}
*m.ChannelsBalance = ChannelsBalance{balance}
return nil
}
3 years ago
type RoutingLog struct {
Log []*models.RoutingEvent
}
const MaxRoutingEvents = 512 // 8K monitor @ 8px per line = 540
func (m *Models) RefreshRouting(update interface{}) func(context.Context) error {
return (func(ctx context.Context) error {
hu, ok := update.(*models.RoutingEvent)
if ok {
found := false
for _, hlu := range m.RoutingLog.Log {
if hlu.Equals(hu) {
hlu.Update(hu)
found = true
break
}
}
if !found {
if len(m.RoutingLog.Log) == MaxRoutingEvents {
m.RoutingLog.Log = m.RoutingLog.Log[1:]
}
m.RoutingLog.Log = append(m.RoutingLog.Log, hu)
}
} else {
m.logger.Error("refreshRouting: invalid event data")
}
return nil
})
}