Skip to content

Commit

Permalink
fix: use the encoder inside the matric one
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Dec 13, 2022
1 parent ae34813 commit 3e1674b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
18 changes: 12 additions & 6 deletions internal/plugin/metric_one_model.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package plugin

import (
"encoding/json"
"strings"

"github.com/vincenzopalazzo/cln4go/comm/encoder"

"github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/db"
"github.com/LNOpenMetrics/go-lnmetrics.reporter/pkg/json"
"github.com/LNOpenMetrics/go-lnmetrics.reporter/pkg/model"
)

Expand Down Expand Up @@ -188,7 +190,6 @@ type MetricOne struct {
UpTime []*status `json:"up_time"`

// map of informaton of channel information
// TODO: managing the dualfunding channels
ChannelsInfo map[string]*statusChannel `json:"-"`

// Last check of the plugin, useful to store the data
Expand All @@ -199,6 +200,8 @@ type MetricOne struct {
Storage db.PluginDatabase `json:"-"`

PeerSnapshot map[string]*model.ListPeersPeer `json:"-"`

Encoder encoder.JSONEncoder `json:"-"`
}

func (instance MetricOne) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -230,24 +233,27 @@ func (instance MetricOne) MarshalJSON() ([]byte, error) {
// Pass in an instance of the new type T to json.Marshal.
// For the embedded M field use a converted instance of the receiver.
// For the ChannelsInfo field use the channels slice.
return json.Marshal(T{
return instance.Encoder.EncodeToByte(T{
M: M(instance),
ChannelsInfo: channels,
})
}

// UnmarshalJSON Same as MarshalJSON but in reverse.
func (instance *MetricOne) UnmarshalJSON(data []byte) error {
if instance.Encoder == nil {
instance.Encoder = &json.FastJSON{}
}
var jsonMap map[string]any
if err := json.Unmarshal(data, &jsonMap); err != nil {
if err := instance.Encoder.DecodeFromBytes(data, &jsonMap); err != nil {
return err
}

if err := instance.Migrate(jsonMap); err != nil {
return err
}

data, err := json.Marshal(jsonMap)
data, err := instance.Encoder.EncodeToByte(jsonMap)
if err != nil {
return err
}
Expand All @@ -257,7 +263,7 @@ func (instance *MetricOne) UnmarshalJSON(data []byte) error {
ChannelsInfo []*statusChannel `json:"channels_info"`
}
t := T{M: (*M)(instance)}
if err := json.Unmarshal(data, &t); err != nil {
if err := instance.Encoder.DecodeFromBytes(data, &t); err != nil {
return err
}

Expand Down
13 changes: 8 additions & 5 deletions internal/plugin/metrics_one.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package plugin

import (
"encoding/json"
"errors"
"fmt"
"github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/cache"
"reflect"
"strconv"
"strings"
"time"

"github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/cache"

"github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/db"
"github.com/LNOpenMetrics/go-lnmetrics.reporter/pkg/graphql"
"github.com/LNOpenMetrics/go-lnmetrics.reporter/pkg/json"
"github.com/LNOpenMetrics/go-lnmetrics.reporter/pkg/ln"
"github.com/LNOpenMetrics/go-lnmetrics.reporter/pkg/model"

Expand Down Expand Up @@ -48,6 +49,7 @@ func NewMetricOne(nodeId string, sysInfo sysinfo.HostInfo, storage db.PluginData
Color: "",
Storage: storage,
PeerSnapshot: make(map[string]*model.ListPeersPeer),
Encoder: &json.FastJSON{},
}
}

Expand Down Expand Up @@ -272,7 +274,7 @@ func (instance *MetricOne) OnStop(msg *Msg, lightning cln4go.Client) error {
if err != nil {
return err
}
if err := json.Unmarshal([]byte(*jsonLast), &lastMetric); err != nil {
if err := instance.Encoder.DecodeFromBytes([]byte(*jsonLast), &lastMetric); err != nil {
return err
}
now := time.Now().Unix()
Expand All @@ -295,7 +297,7 @@ func (instance *MetricOne) OnStop(msg *Msg, lightning cln4go.Client) error {

// ToJSON Convert the MetricOne structure to a JSON string.
func (instance *MetricOne) ToJSON() (string, error) {
json, err := json.Marshal(&instance)
json, err := instance.Encoder.EncodeToByte(&instance)
if err != nil {
log.GetInstance().Error(err)
return "", err
Expand Down Expand Up @@ -382,7 +384,7 @@ func (instance *MetricOne) checkChannelInCache(lightning cln4go.Client, channelI
return nil, err
}
// FIXME: use the plugin encoder
if err := json.Unmarshal(bytes, &nodeInfo); err != nil {
if err := instance.Encoder.DecodeFromBytes(bytes, &nodeInfo); err != nil {
log.GetInstance().Errorf("Error %s", err)
return nil, err
}
Expand Down Expand Up @@ -586,6 +588,7 @@ func (instance *MetricOne) collectInfoChannel(lightning cln4go.Client,
info, infoFound := infoMap[direction]
if !infoFound {
log.GetInstance().Errorf("Error: channel not exist for direction %s", direction)
log.GetInstance().Errorf("info error: key channel info: %s", key)
// this should never happen, because we fill the channel with the same
// method that we derive the directions
return fmt.Errorf("error: channel not exist for direction %s", direction)
Expand Down

0 comments on commit 3e1674b

Please sign in to comment.