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

115 lines
2.5 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
5 years ago
CurrentChannel *Channel
5 years ago
WalletBalance *WalletBalance
ChannelsBalance *ChannelsBalance
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{},
5 years ago
CurrentChannel: &Channel{},
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.Error("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
}
func (m *Models) SetCurrentChannel(ctx context.Context, index int) error {
channel := m.Channels.Get(index)
if channel == nil {
return nil
}
*m.CurrentChannel = Channel{Item: channel}
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
}