Skip to content

Commit

Permalink
fix: allow the rpc method to query only old score
Browse files Browse the repository at this point in the history
We can query only old score, because we do not know
if the new one is calculated by the time event based.

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Jan 31, 2023
1 parent 77b31c5 commit 0c98b7e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions internal/db/leveldb_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (instance *LevelDB) StoreMetricOneSnapshot(timestamp int64, payload *string
if err := instance.PutValue(key, payload); err != nil {
return err
}

timestampStr := fmt.Sprint(timestamp)
keyLastUpt := strings.Join([]string{"metric_one", "last"}, "/")
if err := instance.PutValue(keyLastUpt, &timestampStr); err != nil {
Expand Down
12 changes: 10 additions & 2 deletions internal/metrics/metrics_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func (instance *RawLocalScore) Migrate(payload map[string]any) error {
return nil
}

// FIXME: this is bad because we can not cache it forever, but it is needed
// just to speed up some cln performance, so we allow outdata peer data like
// address, alias, features.
func (instance *RawLocalScore) snapshotListPeers(lightning cln4go.Client) error {
instance.PeerSnapshot = make(map[string]*model.ListPeersPeer)
listPeers, err := ln.ListPeers(lightning, nil)
Expand Down Expand Up @@ -501,6 +504,10 @@ func (instance *RawLocalScore) makePaymentsSummary(lightning cln4go.Client, forw

// private method of the module
func (instance *RawLocalScore) collectInfoChannels(lightning cln4go.Client, channels []*model.ListFundsChannel, event string) error {
if len(channels) == 0 {
log.GetInstance().Debug("we are without channels so we exist now")
return nil
}
cache := make(map[string]bool)
cachePing := make(map[string]int64)
for _, channel := range channels {
Expand All @@ -509,11 +516,12 @@ func (instance *RawLocalScore) collectInfoChannels(lightning cln4go.Client, chan
// we skip this type of state
case "CHANNELD_AWAITING_LOCKIN", "DUALOPEND_OPEN_INIT",
"DUALOPEND_AWAITING_LOCKIN":
log.GetInstance().Debugf("node (`%s`) with a channel in a state %s", channel.PeerId, channel.State)
continue
default:
if err := instance.collectInfoChannel(lightning, channel, event, cachePing); err != nil {
// void returning error here? We can continue to make the analysis over the channels
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
log.GetInstance().Errorf("Error: %s", err)
return err
}
if channel.ShortChannelId == nil {
Expand Down Expand Up @@ -655,7 +663,7 @@ func (instance *RawLocalScore) collectInfoChannel(lightning cln4go.Client,
func (instance *RawLocalScore) peerConnected(lightning cln4go.Client, nodeId string) bool {
peer, found := instance.PeerSnapshot[nodeId]
if !found {
log.GetInstance().Infof("peer with node id %s not found", nodeId)
log.GetInstance().Infof("peer with node id %s not found in the snapshot", nodeId)
return false
}
log.GetInstance().Infof("peer `%s` is connected `%v`", nodeId, peer.Connected)
Expand Down
12 changes: 9 additions & 3 deletions internal/plugin/diagnostic.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plugin

import (
"encoding/json"
"fmt"

"github.com/LNOpenMetrics/go-lnmetrics.reporter/internal/metrics"
Expand All @@ -14,11 +15,16 @@ func NewRawLocalScoreRPC[T MetricsPluginState]() *RawLocalScoreRPC[T] {
}

func (instance *RawLocalScoreRPC[T]) Call(plugin *cln4go.Plugin[T], payload map[string]any) (map[string]any, error) {
metric, found := plugin.GetState().GetMetrics()[metrics.RawLocalScoreID]
if !found {
metricStr, err := plugin.GetState().GetStorage().LoadLastMetricOne()
if err != nil {
return nil, fmt.Errorf("Metric with id %d not found", 1)
}
return metric.ToMap()
// FIXME get the encoder from a plugin with GetEncoder and decode the string into a map
var metric map[string]any
if err := json.Unmarshal([]byte(*metricStr), &metric); err != nil {
return nil, err
}
return metric, nil
}

// ForceUpdateRPC enable the force update command
Expand Down
21 changes: 11 additions & 10 deletions pkg/graphql/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type GraphQLResponse struct {
}

type Client struct {
// The graph ql can contains a list of server where
// The graphql can contains a list of server where
// make the request.
BaseUrl []string
// Token to autenticate to the server
Expand Down Expand Up @@ -84,29 +84,29 @@ func isOnionUrl(url string) bool {
func (instance *Client) MakeRequest(query map[string]string) ([]*GraphQLResponse, error) {
jsonValue, err := json.Marshal(query)
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
log.GetInstance().Errorf("Error: %s", err)
return nil, err
}

failure := 0
responses := make([]*GraphQLResponse, 0)
for _, url := range instance.BaseUrl {
log.GetInstance().Info(fmt.Sprintf("Request to URL %s", url))
log.GetInstance().Infof("Request to URL %s", url)
if !instance.WithProxy && isOnionUrl(url) {
log.GetInstance().Debug(fmt.Sprintf("Skipped request to url %s because the proxy it is not configured in the plugin", url))
log.GetInstance().Debugf("Skipped request to url %s because the proxy it is not configured in the plugin", url)
continue
}
request, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
if err != nil {
failure++
log.GetInstance().Error(fmt.Sprintf("Error with the message \"%s\" during the request to endpoint %s", err, url))
log.GetInstance().Errorf("Error with the message \"%s\" during the request to endpoint %s", err, url)
continue
}
request.Header.Set("Content-Type", "application/json")
response, err := instance.Client.Do(request)
if err != nil {
failure++
log.GetInstance().Error(fmt.Sprintf("error with the message \"%s\" during the request to endpoint %s", err, url))
log.GetInstance().Errorf("error with the message \"%s\" during the request to endpoint %s", err, url)
continue
}

Expand All @@ -118,24 +118,25 @@ func (instance *Client) MakeRequest(query map[string]string) ([]*GraphQLResponse

defer func() {
if err := response.Body.Close(); err != nil {
log.GetInstance().Error(fmt.Sprintf("Error: %s", err))
log.GetInstance().Errorf("Error: %s", err)
}
}()

result, err := io.ReadAll(response.Body)
if err != nil {
failure++
log.GetInstance().Error(fmt.Sprintf("error with the message \"%s\" during the request to endpoint %s", err, url))
log.GetInstance().Errorf("error with the message \"%s\" during the request to endpoint %s", err, url)
continue
}
var respModel GraphQLResponse
if err := json.Unmarshal([]byte(result), &respModel); err != nil {
failure++
log.GetInstance().Infof("Raw server response: %s", result)
log.GetInstance().Error(fmt.Sprintf("Error during graphql response: %s", err))
log.GetInstance().Errorf("Error during graphql response: %s", err)
continue
}
responses = append(responses, &respModel)
log.GetInstance().Debug(fmt.Sprintf("Result from server %s", result))
log.GetInstance().Debugf("Result from server %s", result)
}

if failure == len(instance.BaseUrl) {
Expand Down

0 comments on commit 0c98b7e

Please sign in to comment.