2016-10-05 21:38:58 +02:00
|
|
|
from lbrynet.core import looping_call_manager
|
|
|
|
|
2016-10-11 19:50:44 +02:00
|
|
|
from twisted.internet import defer
|
2016-10-01 17:47:37 +02:00
|
|
|
from twisted.internet import task
|
|
|
|
|
|
|
|
import constants
|
|
|
|
|
|
|
|
|
|
|
|
class Manager(object):
|
2016-10-05 20:51:26 +02:00
|
|
|
def __init__(self, analytics_api, events_generator, track):
|
|
|
|
self.analytics_api = analytics_api
|
|
|
|
self.events_generator = events_generator
|
|
|
|
self.track = track
|
2016-10-05 21:38:58 +02:00
|
|
|
self.looping_call_manager = self.setup_looping_calls()
|
|
|
|
|
|
|
|
def setup_looping_calls(self):
|
|
|
|
call_manager = looping_call_manager.LoopingCallManager()
|
|
|
|
looping_calls = [
|
|
|
|
('send_heartbeat', self._send_heartbeat),
|
|
|
|
('update_tracked_metrics', self._update_tracked_metrics),
|
|
|
|
]
|
|
|
|
for name, fn in looping_calls:
|
|
|
|
call_manager.register_looping_call(name, task.LoopingCall(fn))
|
|
|
|
return call_manager
|
2016-10-01 17:47:37 +02:00
|
|
|
|
2016-10-05 20:51:26 +02:00
|
|
|
def start(self):
|
2016-10-05 21:38:58 +02:00
|
|
|
self.looping_call_manager.start('send_heartbeat', 60)
|
|
|
|
self.looping_call_manager.start('update_tracked_metrics', 300)
|
2016-10-01 17:47:37 +02:00
|
|
|
|
|
|
|
def shutdown(self):
|
2016-10-05 21:38:58 +02:00
|
|
|
self.looping_call_manager.shutdown()
|
2016-10-01 17:47:37 +02:00
|
|
|
|
|
|
|
def send_download_started(self, name, stream_info=None):
|
|
|
|
event = self.events_generator.download_started(name, stream_info)
|
|
|
|
self.analytics_api.track(event)
|
|
|
|
|
2016-10-05 21:45:17 +02:00
|
|
|
def register_repeating_metric(self, event_name, value_generator, frequency=300):
|
|
|
|
lcall = task.LoopingCall(self._send_repeating_metric, event_name, value_generator)
|
|
|
|
self.looping_call_manager.register_looping_call(event_name, lcall)
|
|
|
|
lcall.start(frequency)
|
|
|
|
|
2016-10-01 17:47:37 +02:00
|
|
|
def _send_heartbeat(self):
|
|
|
|
heartbeat = self.events_generator.heartbeat()
|
|
|
|
self.analytics_api.track(heartbeat)
|
|
|
|
|
|
|
|
def _update_tracked_metrics(self):
|
2016-10-11 19:50:44 +02:00
|
|
|
should_send, value = self.track.summarize(constants.BLOB_BYTES_UPLOADED)
|
|
|
|
if should_send:
|
|
|
|
event = self.events_generator.metric_observed(constants.BLOB_BYTES_UPLOADED, value)
|
2016-10-01 17:47:37 +02:00
|
|
|
self.analytics_api.track(event)
|
2016-10-05 21:45:17 +02:00
|
|
|
|
|
|
|
def _send_repeating_metric(self, event_name, value_generator):
|
2016-10-11 19:50:44 +02:00
|
|
|
result = value_generator()
|
|
|
|
if_deferred(result, self._send_repeating_metric_value, event_name)
|
|
|
|
|
|
|
|
def _send_repeating_metric_value(self, result, event_name):
|
|
|
|
should_send, value = result
|
2016-10-05 21:45:17 +02:00
|
|
|
if should_send:
|
2016-10-11 19:50:44 +02:00
|
|
|
event = self.events_generator.metric_observed(event_name, value)
|
2016-10-05 21:45:17 +02:00
|
|
|
self.analytics_api.track(event)
|
2016-10-11 19:50:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def if_deferred(maybe_deferred, callback, *args, **kwargs):
|
|
|
|
if isinstance(maybe_deferred, defer.Deferred):
|
|
|
|
maybe_deferred.addCallback(callback, *args, **kwargs)
|
|
|
|
else:
|
2016-10-11 22:14:35 +02:00
|
|
|
callback(maybe_deferred, *args, **kwargs)
|