move analytics manager

This commit is contained in:
Job Evers-Meltzer 2016-10-01 10:47:37 -05:00
parent f5213fb77a
commit f9f07c47e2
5 changed files with 54 additions and 43 deletions

View file

@ -1,6 +1,6 @@
from constants import *
from events import * from events import *
from api import AnalyticsApi as Api from api import Api
from track import Track from track import Track
from manager import Manager
# Constants for metrics
BLOB_BYTES_UPLOADED = 'blob_bytes_uploaded'

View file

@ -28,7 +28,7 @@ def log_response(fn):
return wrapper return wrapper
class AnalyticsApi(object): class Api(object):
def __init__(self, session, url, write_key): def __init__(self, session, url, write_key):
self.session = session self.session = session
self.url = url self.url = url

View file

@ -0,0 +1,2 @@
# Constants for metrics
BLOB_BYTES_UPLOADED = 'blob_bytes_uploaded'

View file

@ -0,0 +1,44 @@
import base58
from twisted.internet import task
import api
import constants
import events
import track
class Manager(object):
def __init__(self):
self.analytics_api = None
self.events_generator = None
self.track = track.Track()
self.send_heartbeat = task.LoopingCall(self._send_heartbeat)
self.update_tracked_metrics = task.LoopingCall(self._update_tracked_metrics)
def start(self, platform, wallet_type, lbry_id, session_id):
context = events.make_context(platform, wallet_type)
self.events_generator = events.Events(context, base58.b58encode(lbry_id), session_id)
self.analytics_api = api.Api.load()
self.send_heartbeat.start(60)
self.update_tracked_metrics.start(300)
def shutdown(self):
if self.send_heartbeat.running:
self.send_heartbeat.stop()
if self.update_tracked_metrics.running:
self.update_tracked_metrics.stop()
def send_download_started(self, name, stream_info=None):
event = self.events_generator.download_started(name, stream_info)
self.analytics_api.track(event)
def _send_heartbeat(self):
heartbeat = self.events_generator.heartbeat()
self.analytics_api.track(heartbeat)
def _update_tracked_metrics(self):
value = self.track.summarize(constants.BLOB_BYTES_UPLOADED)
if value > 0:
event = self.events_generator.metric_observered(analytics.BLOB_BYTES_UPLOADED, value)
self.analytics_api.track(event)

View file

@ -203,40 +203,8 @@ class CheckRemoteVersions(object):
return defer.fail(None) return defer.fail(None)
class AnalyticsManager(object): def calculate_available_blob_sizes(blob_manager):
def __init__(self): return sum(blob.length for blob in blob_manager.get_all_verified_blobs())
self.analytics_api = None
self.events_generator = None
self.track = analytics.Track()
self.send_heartbeat = LoopingCall(self._send_heartbeat)
self.update_tracked_metrics = LoopingCall(self._update_tracked_metrics)
def start(self, platform, wallet_type, lbry_id, session_id):
context = analytics.make_context(platform, wallet_type)
self.events_generator = analytics.Events(context, base58.b58encode(lbry_id), session_id)
self.analytics_api = analytics.Api.load()
self.send_heartbeat.start(60)
self.update_tracked_metrics.start(300)
def shutdown(self):
if self.send_heartbeat.running:
self.send_heartbeat.stop()
if self.update_tracked_metrics.running:
self.update_tracked_metrics.stop()
def send_download_started(self, name, stream_info=None):
event = self.events_generator.download_started(name, stream_info)
self.analytics_api.track(event)
def _send_heartbeat(self):
heartbeat = self.events_generator.heartbeat()
self.analytics_api.track(heartbeat)
def _update_tracked_metrics(self):
value = self.track.summarize(analytics.BLOB_BYTES_UPLOADED)
if value > 0:
event = self.events_generator.metric_observered(analytics.BLOB_BYTES_UPLOADED, value)
self.analytics_api.track(event)
class Daemon(jsonrpc.JSONRPC): class Daemon(jsonrpc.JSONRPC):
@ -276,7 +244,7 @@ class Daemon(jsonrpc.JSONRPC):
self.first_run_after_update = False self.first_run_after_update = False
self.uploaded_temp_files = [] self.uploaded_temp_files = []
self._session_id = base58.b58encode(generate_id()) self._session_id = base58.b58encode(generate_id())
self.analytics_manager = AnalyticsManager() self.analytics_manager = analytics.Manager()
if os.name == "nt": if os.name == "nt":
from lbrynet.winhelpers.knownpaths import get_path, FOLDERID, UserHandle from lbrynet.winhelpers.knownpaths import get_path, FOLDERID, UserHandle
@ -453,7 +421,6 @@ class Daemon(jsonrpc.JSONRPC):
('version_checker', CheckRemoteVersions(self)), ('version_checker', CheckRemoteVersions(self)),
('connection_problem_checker', self._check_connection_problems), ('connection_problem_checker', self._check_connection_problems),
('pending_claim_checker', self._check_pending_claims), ('pending_claim_checker', self._check_pending_claims),
('send_tracked_metrics', self._send_tracked_metrics),
] ]
for name, fn in looping_calls: for name, fn in looping_calls:
self.looping_call_manager.register_looping_call(name, fn) self.looping_call_manager.register_looping_call(name, fn)
@ -645,7 +612,6 @@ class Daemon(jsonrpc.JSONRPC):
d.addCallback(lambda _: threads.deferToThread(self._setup_data_directory)) d.addCallback(lambda _: threads.deferToThread(self._setup_data_directory))
d.addCallback(lambda _: self._check_db_migration()) d.addCallback(lambda _: self._check_db_migration())
d.addCallback(lambda _: self._get_settings()) d.addCallback(lambda _: self._get_settings())
d.addCallback(lambda _: self._set_events())
d.addCallback(lambda _: self._get_session()) d.addCallback(lambda _: self._get_session())
d.addCallback(lambda _: add_lbry_file_to_sd_identifier(self.sd_identifier)) d.addCallback(lambda _: add_lbry_file_to_sd_identifier(self.sd_identifier))
d.addCallback(lambda _: self._setup_stream_identifier()) d.addCallback(lambda _: self._setup_stream_identifier())
@ -2876,7 +2842,6 @@ class _GetFileHelper(object):
key = self._get_key(lbry_file) key = self._get_key(lbry_file)
full_path = self._full_path(lbry_file) full_path = self._full_path(lbry_file)
mime_type = mimetypes.guess_type(full_path)[0] mime_type = mimetypes.guess_type(full_path)[0]
return { return {
'completed': lbry_file.completed, 'completed': lbry_file.completed,
'file_name': lbry_file.file_name, 'file_name': lbry_file.file_name,
@ -2899,7 +2864,7 @@ class _GetFileHelper(object):
'code': code, 'code': code,
'message': message 'message': message
} }
def _add_metadata(self, message, lbry_file): def _add_metadata(self, message, lbry_file):
def _add_to_dict(metadata): def _add_to_dict(metadata):
message['metadata'] = metadata message['metadata'] = metadata