5 from twisted.web import resource
10 class Resource(resource.Resource):
12 resource.Resource.__init__(self)
14 self.putChild('', self)
16 def render_GET(self, request):
17 if not request.path.endswith('/'):
18 request.redirect(request.path + '/')
20 request.setHeader('Content-Type', 'text/html')
21 return '<html><head><title>P2Pool Graphs</title></head><body><p>Install python-rrdtool!</p></body></html>'
23 class Grapher(object):
24 def __init__(self, *args): pass
25 def get_resource(self): return Resource()
26 def __getattr__(self, name): return lambda *args, **kwargs: None
28 class Renderer(resource.Resource):
29 def __init__(self, arg_func):
30 self.arg_func = arg_func
32 def render_GET(self, request):
33 handle, filename = tempfile.mkstemp()
36 rrdtool.graph(filename, '--imgformat', 'PNG', *self.arg_func())
38 request.setHeader('Content-Type', 'image/png')
39 return open(filename, 'rb').read()
41 class Resource(resource.Resource):
42 def __init__(self, grapher):
43 resource.Resource.__init__(self)
44 self.grapher = grapher
46 self.putChild('', self)
48 self.putChild('poolrate_day', Renderer(lambda: ['--lower-limit', '0', '-M', '-E', '--start', '-1d',
49 'DEF:A=%s.poolrate:poolrate:AVERAGE' % (self.grapher.path,), 'LINE1:A#0000FF:Total (last day)',
50 'DEF:B=%s.localrate:localrate:AVERAGE' % (self.grapher.path,), 'LINE1:B#0000FF:Local (last day)']))
51 self.putChild('poolrate_week', Renderer(lambda: ['--lower-limit', '0', '-M', '-E', '--start', '-1w',
52 'DEF:A=%s.poolrate:poolrate:AVERAGE' % (self.grapher.path,), 'LINE1:A#0000FF:Total (last week)',
53 'DEF:B=%s.localrate:localrate:AVERAGE' % (self.grapher.path,), 'LINE1:B#0000FF:Local (last week)']))
54 self.putChild('poolrate_month', Renderer(lambda: ['--lower-limit', '0', '-M', '-E', '--start', '-1m',
55 'DEF:A=%s.poolrate:poolrate:AVERAGE' % (self.grapher.path,), 'LINE1:A#0000FF:Total (last month)',
56 'DEF:B=%s.localrate:localrate:AVERAGE' % (self.grapher.path,), 'LINE1:B#0000FF:Local (last month)']))
60 for i, x in enumerate(os.listdir(os.path.dirname(self.grapher.path))):
61 x2 = os.path.join(os.path.dirname(self.grapher.path), x)
62 if not x2.startswith(self.grapher.path + '.') or not x2.endswith('.localminer'):
64 name = x2[len(self.grapher.path + '.'):-len('.localminer')].decode('hex')
66 'DEF:%i=%s:localminer:AVERAGE' % (i, x2),
67 'AREA:%i#%s:%s%s' % (i, hashlib.sha256(name).hexdigest()[:6], name, ':STACK' if i != 0 else ''),
71 self.putChild('localrate_day', Renderer(lambda: ['--lower-limit', '0', '-M', '-E', '--start', '-1d'] + get_lines() + [
72 'DEF:A=%s.localrate:localrate:AVERAGE' % (self.grapher.path,), 'LINE1:A#0000FF:Total (last day)',
73 'DEF:B=%s.localdeadrate:localdeadrate:AVERAGE' % (self.grapher.path,), 'LINE1:B#FF0000:Dead (last day)']))
74 self.putChild('localrate_week', Renderer(lambda: ['--lower-limit', '0', '-M', '-E', '--start', '-1w'] + get_lines() + [
75 'DEF:A=%s.localrate:localrate:AVERAGE' % (self.grapher.path,), 'LINE1:A#0000FF:Total (last week)',
76 'DEF:B=%s.localdeadrate:localdeadrate:AVERAGE' % (self.grapher.path,), 'LINE1:B#FF0000:Dead (last week)']))
77 self.putChild('localrate_month', Renderer(lambda: ['--lower-limit', '0', '-M', '-E', '--start', '-1m'] + get_lines() + [
78 'DEF:A=%s.localrate:localrate:AVERAGE' % (self.grapher.path,), 'LINE1:A#0000FF:Total (last month)',
79 'DEF:B=%s.localdeadrate:localdeadrate:AVERAGE' % (self.grapher.path,), 'LINE1:B#FF0000:Dead (last month)']))
81 def render_GET(self, request):
82 if not request.path.endswith('/'):
83 request.redirect(request.path + '/')
85 request.setHeader('Content-Type', 'text/html')
86 return '''<html><head><title>P2Pool Graphs</title></head><body><h1>P2Pool Graphs</h1>
87 <h2>Pool hash rate:</h2>
88 <p><img style="display:inline" src="poolrate_day"/> <img style="display:inline" src="poolrate_week"/> <img style="display:inline" src="poolrate_month"/></p>
89 <h2>Local hash rate:</h2>
90 <p><img style="display:inline" src="localrate_day"/> <img style="display:inline" src="localrate_week"/> <img style="display:inline" src="localrate_month"/></p>
93 class Grapher(object):
94 def __init__(self, path):
97 if not os.path.exists(self.path + '.poolrate'):
98 rrdtool.create(self.path + '.poolrate', '--step', '300',
99 'DS:poolrate:GAUGE:600:U:U',
100 'RRA:AVERAGE:0.5:1:288', # last day
101 'RRA:AVERAGE:0.5:7:288', # last week
102 'RRA:AVERAGE:0.5:30:288', # last month
104 if not os.path.exists(self.path + '.localrate'):
105 rrdtool.create(self.path + '.localrate', '--step', '300',
106 'DS:localrate:ABSOLUTE:43200:U:U',
107 'RRA:AVERAGE:0.5:1:288', # last day
108 'RRA:AVERAGE:0.5:7:288', # last week
109 'RRA:AVERAGE:0.5:30:288', # last month
111 if not os.path.exists(self.path + '.localdeadrate'):
112 rrdtool.create(self.path + '.localdeadrate', '--step', '300',
113 'DS:localdeadrate:ABSOLUTE:43200:U:U',
114 'RRA:AVERAGE:0.5:1:288', # last day
115 'RRA:AVERAGE:0.5:7:288', # last week
116 'RRA:AVERAGE:0.5:30:288', # last month
119 def add_poolrate_point(self, poolrate):
120 rrdtool.update(self.path + '.poolrate', '-t', 'poolrate', 'N:%f' % (poolrate,))
122 def add_localrate_point(self, hashes, dead):
123 rrdtool.update(self.path + '.localrate', '-t', 'localrate', 'N:%f' % (hashes,))
124 rrdtool.update(self.path + '.localdeadrate', '-t', 'localdeadrate', 'N:%f' % (hashes if dead else 0,))
126 def add_localminer_point(self, name, hashes, dead):
127 path = self.path + '.' + name.encode('hex')
128 if not os.path.exists(path + '.localminer'):
129 rrdtool.create(path + '.localminer', '--step', '300',
130 'DS:localminer:ABSOLUTE:43200:U:U',
131 'RRA:AVERAGE:0.5:1:288', # last day
132 'RRA:AVERAGE:0.5:7:288', # last week
133 'RRA:AVERAGE:0.5:30:288', # last month
135 if not os.path.exists(path + '.localdeadminer'):
136 rrdtool.create(path + '.localdeadminer', '--step', '300',
137 'DS:localdeadminer:ABSOLUTE:43200:U:U',
138 'RRA:AVERAGE:0.5:1:288', # last day
139 'RRA:AVERAGE:0.5:7:288', # last week
140 'RRA:AVERAGE:0.5:30:288', # last month
142 rrdtool.update(path + '.localminer', '-t', 'localminer', 'N:%f' % (hashes,))
143 rrdtool.update(path + '.localdeadminer', '-t', 'localdeadminer', 'N:%f' % (hashes if dead else 0,))
145 def get_resource(self):
146 return Resource(self)