<s> """<STR_LIT>""" <EOL> import json <EOL> from twisted . internet import defer , reactor <EOL> from twisted . web . client import Agent <EOL> from twisted . web . http_headers import Headers <EOL> from zope . interface import implements <EOL> from tensor . interfaces import ITensorSource <EOL> from tensor . objects import Source <EOL> from tensor . utils import BodyReceiver <EOL> class RiakStats ( Source ) : <EOL> """<STR_LIT>""" <EOL> implements ( ITensorSource ) <EOL> @ defer . inlineCallbacks <EOL> def _get_stats_from_node ( self ) : <EOL> agent = Agent ( reactor ) <EOL> url = self . config . get ( '<STR_LIT:url>' , '<STR_LIT>' % self . hostname ) <EOL> ua = self . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> headers = Headers ( { '<STR_LIT>' : [ ua ] } ) <EOL> request = yield agent . request ( '<STR_LIT:GET>' , url , headers ) <EOL> if request . length : <EOL> d = defer . Deferred ( ) <EOL> request . deliverBody ( BodyReceiver ( d ) ) <EOL> b = yield d <EOL> body = b . read ( ) <EOL> else : <EOL> body = "<STR_LIT>" <EOL> defer . returnValue ( json . loads ( body ) ) <EOL> @ defer . inlineCallbacks <EOL> def get ( self ) : <EOL> stats = yield self . _get_stats_from_node ( ) <EOL> get_rate = stats [ '<STR_LIT>' ] / <NUM_LIT> <EOL> put_rate = stats [ '<STR_LIT>' ] / <NUM_LIT> <EOL> defer . returnValue ( [ <EOL> self . createEvent ( <EOL> '<STR_LIT>' , '<STR_LIT>' , get_rate , <EOL> prefix = "<STR_LIT>" ) , <EOL> self . createEvent ( <EOL> '<STR_LIT>' , '<STR_LIT>' , put_rate , <EOL> prefix = "<STR_LIT>" ) , <EOL> ] ) </s>
