Skip to content

Commit

Permalink
plugin: cleaning code and update docs
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 1, 2022
1 parent 29de1df commit e2cd456
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
15 changes: 15 additions & 0 deletions internal/cache/cache_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type cacheManager struct {
cache map[string]*string
}

// GetInstance return the instance of the cache manager that it is
// a singleton.
func GetInstance() *cacheManager {
return &cacheManager{
prefix: "cache",
Expand Down Expand Up @@ -74,6 +76,9 @@ func (instance *cacheManager) addToCache(key string) error {
return nil
}

// IsInCache check if the key is inside the cache and return the result
// this not include side effect, so if any side effect happens, the function
// always return false.
func (instance *cacheManager) IsInCache(key string) bool {
if instance.cache != nil {
_, ok := instance.cache[key]
Expand Down Expand Up @@ -113,6 +118,16 @@ func (instance *cacheManager) PutToCache(key string, value interface{}) error {
return instance.addToCache(key)
}

// PurgeFromCache delete the value from cache with the specified key
// otherwise return an error.
func (instance *cacheManager) PurgeFromCache(key string) error {
key = instance.buildID(key)
if err := db.GetInstance().DeleteValue(key); err != nil {
return err
}
return nil
}

// CleanCache clean the index from the database
func (instance *cacheManager) CleanCache() error {
for key := range instance.cache {
Expand Down
35 changes: 15 additions & 20 deletions internal/plugin/metric_interface.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package plugin implement all the necessary building blocks to implement
// an open source metrics.
package plugin

import (
Expand All @@ -6,7 +8,7 @@ import (
"github.com/vincenzopalazzo/glightning/glightning"
)

// mapping the internal id with the name of the metrics.
// MetricsSupported mapping the internal id with the name of the metrics.
// the id is passed by the plugin RPC name.
var MetricsSupported map[int]string

Expand All @@ -24,40 +26,33 @@ func init() {
ChannelDirections[1] = "INCOOMING"
}

// All the metrics need to respect this interface
// Metric All the metrics need to respect this interface
type Metric interface {
// return the name of the metric
// MetricName return the name of the metric
MetricName() *string

// call this to initialized the metric with node
// information if any error occurs, a not nil value is
// returned
// OnInit initialize the method with node information
OnInit(lightning *glightning.Lightning) error

// Call this method when the close rpc method is called
OnClose(msg *Msg, lightning *glightning.Lightning) error
// OnStop commit the actual information before exit
OnStop(msg *Msg, lightning *glightning.Lightning) error

// Call this method to make the status of the metrics persistent
// MakePersistent make the metric persistent
MakePersistent() error

// Method to store the run a callback to upload the content on the server.
// TODO: Use an interface to generalize the client, it can be also a rest api
// move accept some interface later.
// UploadOnRepo Commit the metric on remote server
UploadOnRepo(client *graphql.Client, lightning *glightning.Lightning) error

// Method to store the run a callback to init the content on the server
// the first time that the plugin in ran.
// InitOnRepo Init metric on the remote server.
InitOnRepo(client *graphql.Client, lightning *glightning.Lightning) error

// Call this method when you want update all the metrics without
// some particular event throw from c-lightning
// Update the metric with the last information of the node
Update(lightning *glightning.Lightning) error

// Class this method when you want catch some event from
// c-lightning and make some operation on the metrics data.
// UpdateWithMsg update the metric with the last information fo the node with some msg info
UpdateWithMsg(message *Msg, lightning *glightning.Lightning) error

// convert the object into a json
// ToJSON convert the object into a json
ToJSON() (string, error)

// Migrate to a new version of the metrics, some new version of the plugin
Expand All @@ -66,7 +61,7 @@ type Metric interface {
Migrate(payload map[string]interface{}) error
}

// Message struct to pass from the plugin to the metric
// Msg Message struct to pass from the plugin to the metric
type Msg struct {
// The message is from a command? if not it is nil
cmd string
Expand Down
12 changes: 6 additions & 6 deletions internal/plugin/metrics_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,7 @@ func (instance *MetricOne) MakePersistent() error {
return instance.Storage.StoreMetricOneSnapshot(instance.lastCheck, &instanceJson)
}

// FIXME: the message is not useful, but we keep it only for future evolution
// or we will remove it from here.
func (instance *MetricOne) OnClose(msg *Msg, lightning *glightning.Lightning) error {
func (instance *MetricOne) OnStop(msg *Msg, lightning *glightning.Lightning) error {
log.GetInstance().Debug("On close event on metrics called")
//TODO: Check if the values are empty, if yes, try a solution
// to avoid to push empty payload.
Expand All @@ -493,7 +491,10 @@ func (instance *MetricOne) OnClose(msg *Msg, lightning *glightning.Lightning) er
}
instance.UpTime = append(instance.UpTime, statusItem)
instance.lastCheck = now
return instance.MakePersistent()
if err := instance.MakePersistent(); err != nil {
return err
}
return nil
}

// ToJSON Convert the MetricOne structure to a JSON string.
Expand Down Expand Up @@ -589,7 +590,6 @@ func (instance *MetricOne) checkChannelInCache(lightning *glightning.Lightning,
}

if nodeInfo == nil {
// FIXME: we need some method to update the cache and prefilled at the startup.
node, err := lightning.GetNode(channelID)
if err != nil {
log.GetInstance().Error(fmt.Sprintf("Error in command listNodes in makeChannelsSummary: %s", err))
Expand Down Expand Up @@ -935,7 +935,7 @@ func (instance *MetricOne) getChannelInfo(lightning *glightning.Lightning,

switch forward.Status {
case "settled", "offered", "failed":
// do nothings
// do nothing
continue
case "local_failed":
// store the information about the failure
Expand Down

0 comments on commit e2cd456

Please sign in to comment.