@cryptotaxi247 / netdata-1 / commits / d88f0c967

More IPFS stats in less code

Paul Jimenez committed Jan 9, 2017 at 01:12 UTC d88f0c9670a30d12b6698e05b0b56451aa0247e0
1 file changed +43 -50
python.d/ipfs.chart.py
+43 -50
@@ -19,7 +19,7 @@ retries = 60
19 # }}
20
21 # charts order (can be overridden if you want less charts, or different order)
22 -ORDER = ['bandwidth', 'peers']
22 +ORDER = ['bandwidth', 'peers', 'repo_size', 'repo_objects']
23
24 CHARTS = {
25 'bandwidth': {
@@ -32,10 +32,21 @@ CHARTS = {
32 'options': [None, 'IPFS Peers', 'peers', 'Peers', 'ipfs.peers', 'line'],
33 'lines': [
34 ["peers", None, 'absolute']
35 - ]}
35 + ]},
36 + 'repo_size': {
37 + 'options': [None, 'IPFS Repo Size', 'MB', 'Size', 'ipfs.repo_size', 'line'],
38 + 'lines': [
39 + ["size", None, "absolute", 1, 1000000]
40 + ]},
41 + 'repo_objects': {
42 + 'options': [None, 'IPFS Repo Objects', 'objects', 'Objects', 'ipfs.repo_objects', 'line'],
43 + 'lines': [
44 + ["objects", None, "absolute", 1, 1],
45 + ["pinned", None, "absolute", 1, 1],
46 + ["recursive_pins", None, "absolute", 1, 1]
47 + ]},
48 }
49
38 -
50 class Service(UrlService):
51 def __init__(self, configuration=None, name=None):
52 UrlService.__init__(self, configuration=configuration, name=name)
@@ -46,62 +57,44 @@ class Service(UrlService):
57 self.order = ORDER
58 self.definitions = CHARTS
59
49 - def _get_bandwidth(self):
50 - """
51 - Format data received from http request
52 - :return: int, int
60 + def _get_json(self, suburl):
61 """
54 - self.url = self.baseurl + "/api/v0/stats/bw"
62 + :return: json decoding of the specified url
63 + """
64 + self.url = self.baseurl + suburl
65 try:
56 - raw = self._get_raw_data()
57 - except AttributeError:
58 - return None
59 -
60 - try:
61 - parsed = json.loads(raw)
62 - bw_in = int(parsed['RateIn'])
63 - bw_out = int(parsed['RateOut'])
66 + return json.loads(self._get_raw_data())
67 except:
65 - return None
66 -
67 - return bw_in, bw_out
68 + return {}
69
69 - def _get_peers(self):
70 - """
71 - Format data received from http request
72 - :return: int
73 - """
74 - self.url = self.baseurl + "/api/v0/swarm/peers"
75 - try:
76 - raw = self._get_raw_data()
77 - except AttributeError:
78 - return None
79 -
80 - try:
81 - parsed = json.loads(raw)
82 - peers = len(parsed['Strings'])
83 - except:
84 - return None
70 + def _recursive_pins(self, keys):
71 + return len([k for k in keys if keys[k]["Type"] == b"recursive"])
72
86 - return peers
73
74 def _get_data(self):
75 """
76 Get data from API
77 :return: dict
78 """
93 - try:
94 - peers = self._get_peers()
95 - bandwidth_in, bandwidth_out = self._get_bandwidth()
96 - except:
97 - return None
98 - data = {}
99 - if peers is not None:
100 - data['peers'] = peers
101 - if bandwidth_in is not None and bandwidth_out is not None:
102 - data['in'] = bandwidth_in
103 - data['out'] = bandwidth_out
79 + cfg = {# suburl : List of (result-key, original-key, transform-func)
80 + '/api/v0/stats/bw' :[('in', 'RateIn', int ),
81 + ('out', 'RateOut', int )],
82 + '/api/v0/swarm/peers':[('peers', 'Strings', len )],
83 + '/api/v0/stats/repo' :[('size', 'RepoSize', int),
84 + ('objects', 'NumObjects', int)],
85 + '/api/v0/pin/ls': [('pinned', 'Keys', len),
86 + ('recursive_pins', 'Keys', self._recursive_pins)
87 + ]
88 + }
89 + r = {}
90 + for suburl in cfg:
91 + json = self._get_json(suburl)
92 + for newkey, origkey, xmute in cfg[suburl]:
93 + try:
94 + r[newkey] = xmute(json[origkey])
95 + except: pass
96 + return r or None
97 +
98 +
99 +
100
105 - if len(data) == 0:
106 - return None
107 - return data