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
|
|
|
|
|
2016-11-10 21:49:51 +01:00
|
|
|
from lbrynet.core.Platform import get_platform
|
|
|
|
from lbrynet.conf import settings
|
|
|
|
|
2016-10-01 17:47:37 +02:00
|
|
|
import constants
|
2016-11-10 21:49:51 +01:00
|
|
|
from api import Api
|
|
|
|
from events import Events, make_context
|
|
|
|
from track import Track
|
2016-10-01 17:47:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
2016-11-10 21:49:51 +01:00
|
|
|
self.is_started = False
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def new_instance(cls, api=None, events=None):
|
|
|
|
if api is None:
|
|
|
|
api = Api.new_instance()
|
|
|
|
if events is None:
|
|
|
|
events = Events(
|
|
|
|
make_context(get_platform(), settings.wallet),
|
|
|
|
'not loaded', 'not loaded'
|
|
|
|
)
|
|
|
|
return cls(api, events, Track())
|
|
|
|
|
|
|
|
def update_events_generator(self, events_generator):
|
|
|
|
self.events_generator = events_generator
|
|
|
|
|
|
|
|
def _get_looping_calls(self):
|
|
|
|
return [
|
|
|
|
('send_heartbeat', self._send_heartbeat, 60),
|
|
|
|
('update_tracked_metrics', self._update_tracked_metrics, 300),
|
|
|
|
]
|
2016-10-05 21:38:58 +02:00
|
|
|
|
|
|
|
def setup_looping_calls(self):
|
|
|
|
call_manager = looping_call_manager.LoopingCallManager()
|
2016-11-10 21:49:51 +01:00
|
|
|
for name, fn, _ in self._get_looping_calls():
|
2016-10-05 21:38:58 +02:00
|
|
|
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-11-10 21:49:51 +01:00
|
|
|
if not self.is_started:
|
|
|
|
for name, _, interval in self._get_looping_calls():
|
|
|
|
self.looping_call_manager.start(name, interval)
|
|
|
|
self.is_started = True
|
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
|
|
|
|
2016-11-10 21:49:51 +01:00
|
|
|
def send_server_startup(self):
|
|
|
|
event = self.events_generator.server_startup()
|
|
|
|
self.analytics_api.track(event)
|
|
|
|
|
|
|
|
def send_server_startup_success(self):
|
|
|
|
event = self.events_generator.server_startup_success()
|
|
|
|
self.analytics_api.track(event)
|
|
|
|
|
|
|
|
def send_server_startup_error(self, message):
|
|
|
|
event = self.events_generator.server_startup_error(message)
|
|
|
|
self.analytics_api.track(event)
|
|
|
|
|
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-11-10 21:49:51 +01:00
|
|
|
def send_error(self, message, sd_hash=None):
|
|
|
|
event = self.events_generator.error(message, sd_hash)
|
|
|
|
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-18 23:49:31 +02:00
|
|
|
should_send, value = self.track.summarize_and_reset(constants.BLOB_BYTES_UPLOADED)
|
2016-10-11 19:50:44 +02:00
|
|
|
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)
|