<commit_before># Copyright (c) 2017 Quilt Data, Inc. All rights reserved.

"""
Analytics code: Mixpanel, async consumer.
"""

import atexit
from concurrent.futures import ThreadPoolExecutor

from mixpanel import Consumer, Mixpanel

from . import app

MIXPANEL_EVENT = 'SERVER'


class AsyncConsumer(object):
    """
    Wrapper around mixpanel.Consumer that sends messages in a background thread.
    """
    def __init__(self):
        self._consumer = Consumer()
        self._executor = ThreadPoolExecutor(max_workers=1)

    def send(self, endpoint, json_message):
        """
        Queues the message to be sent.
        """
        self._executor.submit(self._consumer.send, endpoint, json_message)

    def shutdown(self):
        """
        Shuts down the background thread.
        """
        self._executor.shutdown(wait=True)

class NoopConsumer(object):
    def send(self, endpoint, json_message):
        pass

mp_token = app.config.get('MIXPANEL_PROJECT_TOKEN', 'fd<PASSWORD>')
if mp_token:
    mp_consumer = AsyncConsumer()
    atexit.register(mp_consumer.shutdown)
else:
    # For testing, dev, etc.
    mp_consumer = NoopConsumer()

mp = Mixpanel(mp_token, mp_consumer)<commit_msg>Fix MixPanel logging in teams setups (#338)<commit_after># Copyright (c) 2017 Quilt Data, Inc. All rights reserved.

"""
Analytics code: Mixpanel, async consumer.
"""

import atexit
from concurrent.futures import ThreadPoolExecutor

from mixpanel import Consumer, Mixpanel

from . import app

MIXPANEL_EVENT = 'SERVER'


class AsyncConsumer(object):
    """
    Wrapper around mixpanel.Consumer that sends messages in a background thread.
    """
    def __init__(self):
        self._consumer = Consumer()
        self._executor = ThreadPoolExecutor(max_workers=1)

    def send(self, endpoint, json_message):
        """
        Queues the message to be sent.
        """
        self._executor.submit(self._consumer.send, endpoint, json_message)

    def shutdown(self):
        """
        Shuts down the background thread.
        """
        self._executor.shutdown(wait=True)

class NoopConsumer(object):
    def send(self, endpoint, json_message):
        pass

# By default, MixPanel is enabled - but can be turned off by setting the token to an empty string.
# `None` means, use the default token.
mp_token = app.config.get('MIXPANEL_PROJECT_TOKEN')
if mp_token is None:
    mp_token = 'fd<PASSWORD>'

if mp_token:
    mp_consumer = AsyncConsumer()
    atexit.register(mp_consumer.shutdown)
else:
    # For testing, dev, etc.
    mp_consumer = NoopConsumer()

mp = Mixpanel(mp_token, mp_consumer)
{'fraction_non_alphanumeric': 0.09854294478527607, 'fraction_numerical': 0.004984662576687116, 'mean_word_length': 3.333887043189369, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4804339', 'n_tokens_mistral': 816, 'n_tokens_neox': 768, 'n_words': 224}