<commit_before>from twisted.internet import task

import constants


class Manager(object):
    def __init__(self, analytics_api, events_generator, track):
        self.analytics_api = analytics_api
        self.events_generator = events_generator
        self.track = track
        self.send_heartbeat = task.LoopingCall(self._send_heartbeat)
        self.update_tracked_metrics = task.LoopingCall(self._update_tracked_metrics)

    def start(self):
        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(constants.BLOB_BYTES_UPLOADED, value)
            self.analytics_api.track(event)
<commit_msg>Add looping call manager to analytics manager<commit_after>from lbrynet.core import looping_call_manager

from twisted.internet import task

import constants


class Manager(object):
    def __init__(self, analytics_api, events_generator, track):
        self.analytics_api = analytics_api
        self.events_generator = events_generator
        self.track = track
        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

    def start(self):
        self.looping_call_manager.start('send_heartbeat', 60)
        self.looping_call_manager.start('update_tracked_metrics', 300)

    def shutdown(self):
        self.looping_call_manager.shutdown()

    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(constants.BLOB_BYTES_UPLOADED, value)
            self.analytics_api.track(event)

{'fraction_non_alphanumeric': 0.08141891891891892, 'fraction_numerical': 0.004054054054054054, 'mean_word_length': 3.3608247422680413, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7221009', 'n_tokens_mistral': 908, 'n_tokens_neox': 890, 'n_words': 141}