From e2cd4560409e538715498e381ee59d98f029d617 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sat, 2 Apr 2022 00:20:10 +0200 Subject: [PATCH] plugin: cleaning code and update docs Signed-off-by: Vincenzo Palazzo --- internal/cache/cache_manager.go | 15 +++++++++++++ internal/plugin/metric_interface.go | 35 +++++++++++++---------------- internal/plugin/metrics_one.go | 12 +++++----- 3 files changed, 36 insertions(+), 26 deletions(-) diff --git a/internal/cache/cache_manager.go b/internal/cache/cache_manager.go index aa5354e..a4f167d 100644 --- a/internal/cache/cache_manager.go +++ b/internal/cache/cache_manager.go @@ -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", @@ -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] @@ -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 { diff --git a/internal/plugin/metric_interface.go b/internal/plugin/metric_interface.go index 5da03a5..6ed3c24 100644 --- a/internal/plugin/metric_interface.go +++ b/internal/plugin/metric_interface.go @@ -1,3 +1,5 @@ +// Package plugin implement all the necessary building blocks to implement +// an open source metrics. package plugin import ( @@ -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 @@ -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 @@ -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 diff --git a/internal/plugin/metrics_one.go b/internal/plugin/metrics_one.go index 55f1698..6a47796 100644 --- a/internal/plugin/metrics_one.go +++ b/internal/plugin/metrics_one.go @@ -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. @@ -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. @@ -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)) @@ -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