]> arthur.barton.de Git - netdata.git/commitdiff
More IPFS stats in less code
authorPaul Jimenez <pj@place.org>
Mon, 9 Jan 2017 06:12:29 +0000 (01:12 -0500)
committerPaul Jimenez <pj@place.org>
Mon, 9 Jan 2017 06:12:29 +0000 (01:12 -0500)
python.d/ipfs.chart.py

index b0b2a9659cf95ca11e8bc55a9b92da1e8717973a..89c7fbf6e040f66cbe1a75fe1a50343fb4948de8 100644 (file)
@@ -19,7 +19,7 @@ retries = 60
 # }}
 
 # charts order (can be overridden if you want less charts, or different order)
-ORDER = ['bandwidth', 'peers']
+ORDER = ['bandwidth', 'peers', 'repo_size', 'repo_objects']
 
 CHARTS = {
     'bandwidth': {
@@ -32,10 +32,21 @@ CHARTS = {
         'options': [None, 'IPFS Peers', 'peers', 'Peers', 'ipfs.peers', 'line'],
         'lines': [
             ["peers", None, 'absolute']
-        ]}
+        ]},
+    'repo_size': {
+        'options': [None, 'IPFS Repo Size', 'MB', 'Size', 'ipfs.repo_size', 'line'],
+        'lines': [
+            ["size", None, "absolute", 1, 1000000]
+        ]},
+    'repo_objects': {
+        'options': [None, 'IPFS Repo Objects', 'objects', 'Objects', 'ipfs.repo_objects', 'line'],
+        'lines': [
+            ["objects", None, "absolute", 1, 1],
+            ["pinned", None, "absolute", 1, 1],
+            ["recursive_pins", None, "absolute", 1, 1]
+        ]},
 }
 
-
 class Service(UrlService):
     def __init__(self, configuration=None, name=None):
         UrlService.__init__(self, configuration=configuration, name=name)
@@ -46,62 +57,44 @@ class Service(UrlService):
         self.order = ORDER
         self.definitions = CHARTS
 
-    def _get_bandwidth(self):
-        """
-        Format data received from http request
-        :return: int, int
+    def _get_json(self, suburl):
         """
-        self.url = self.baseurl + "/api/v0/stats/bw"
+        :return: json decoding of the specified url
+        """ 
+        self.url = self.baseurl + suburl
         try:
-            raw = self._get_raw_data()
-        except AttributeError:
-            return None
-
-        try:
-            parsed = json.loads(raw)
-            bw_in = int(parsed['RateIn'])
-            bw_out = int(parsed['RateOut'])
+            return json.loads(self._get_raw_data()) 
         except:
-            return None
-
-        return bw_in, bw_out
+            return {}
 
-    def _get_peers(self):
-        """
-        Format data received from http request
-        :return: int
-        """
-        self.url = self.baseurl + "/api/v0/swarm/peers"
-        try:
-            raw = self._get_raw_data()
-        except AttributeError:
-            return None
-
-        try:
-            parsed = json.loads(raw)
-            peers = len(parsed['Strings'])
-        except:
-            return None
+    def _recursive_pins(self, keys):
+        return len([k for k in keys if keys[k]["Type"] == b"recursive"])
 
-        return peers
 
     def _get_data(self):
         """
         Get data from API
         :return: dict
         """
-        try:
-            peers = self._get_peers()
-            bandwidth_in, bandwidth_out = self._get_bandwidth()
-        except:
-            return None
-        data = {}
-        if peers is not None:
-            data['peers'] = peers
-        if bandwidth_in is not None and bandwidth_out is not None:
-            data['in'] = bandwidth_in
-            data['out'] = bandwidth_out
+        cfg = {# suburl : List of (result-key, original-key, transform-func)
+               '/api/v0/stats/bw'   :[('in', 'RateIn', int ),
+                                      ('out', 'RateOut', int )],
+               '/api/v0/swarm/peers':[('peers', 'Strings', len )],
+               '/api/v0/stats/repo' :[('size', 'RepoSize', int),
+                                      ('objects', 'NumObjects', int)],
+               '/api/v0/pin/ls': [('pinned', 'Keys', len),
+                                  ('recursive_pins', 'Keys', self._recursive_pins)
+                                 ]
+        }
+        r = {}
+        for suburl in cfg:
+            json = self._get_json(suburl)
+            for newkey, origkey, xmute in cfg[suburl]:
+                try:
+                    r[newkey] = xmute(json[origkey])
+                except: pass
+        return r or None
+
+
 
-        if len(data) == 0:
-            return None
-        return data