<commit_before>import socket
import json
from time import time

from kombu import Connection, Exchange
from kombu.pools import producers

from flask import current_app


class EventQueue(object):
    def __init__(self, broker_uri, serializer='json', compression=None):
        self.exchange = Exchange('events', 'topic', durable=True)
        self.broker_uri = broker_uri
        self.serializer = serializer
        self.compression = compression

    def send(self, entity, action, context={}):
        with Connection(self.broker_uri) as conn:
            payload = {
                'entity': entity,
                'action': action,
                'context': context,
                'hostname': socket.gethostname(),
                'timestamp': time()
            }

            try:
                with producers[conn].acquire(block=True) as producer:
                    producer.publish(payload, serializer=self.serializer,
                                     compression=self.compression,
                                     exchange=self.exchange,
                                     declare=[self.exchange])

                    current_app.logger.info('Event sent: entity=%s, action=%s',
                                            entity, action)
            except conn.connection_errors as err:
                current_app.logger.warning(
                    'Broker connection failed with: %s', err)
                return


class SNSEventTopic(object):
    def __init__(self, name='Events'):
        import boto3

        self.sns = boto3.client('sns')

        resp = self.sns.create_topic(Name=name)
        self.arn = resp['TopicArn']

    def send(self, entity, action, context={}):
        payload = {
            'entity': entity,
            'action': action,
            'context': context,
            'hostname': socket.gethostname(),
            'timestamp': time()<commit_msg>event queue classes now require a user_id<commit_after>import socket
import json
from time import time

from kombu import Connection, Exchange
from kombu.pools import producers

from flask import current_app


class EventQueue(object):
    def __init__(self, broker_uri, serializer='json', compression=None):
        self.exchange = Exchange('events', 'topic', durable=True)
        self.broker_uri = broker_uri
        self.serializer = serializer
        self.compression = compression

    def send(self, entity, action, user_id, context={}):
        with Connection(self.broker_uri) as conn:
            payload = {
                'entity': entity,
                'action': action,
                'user_id': user_id,
                'context': context,
                'hostname': socket.gethostname(),
                'timestamp': time()
            }

            try:
                with producers[conn].acquire(block=True) as producer:
                    producer.publish(payload, serializer=self.serializer,
                                     compression=self.compression,
                                     exchange=self.exchange,
                                     declare=[self.exchange])

                    current_app.logger.info('Event sent: entity=%s, action=%s',
                                            entity, action)
            except conn.connection_errors as err:
                current_app.logger.warning(
                    'Broker connection failed with: %s', err)
                return


class SNSEventTopic(object):
    def __init__(self, name='Events'):
        import boto3

        self.sns = boto3.client('sns')

        resp = self.sns.create_topic(Name=name)
        self.arn = resp['TopicArn']

    def send(self, entity, action, user_id, context={}):
        payload = {
            'entity': entity,
            'action': action,
            'user_id': user_id,
            'context': context,
            'hostname': socket.gethostname(),
            'timestamp': time()
{'fraction_non_alphanumeric': 0.09847715736040609, 'fraction_numerical': 0.0010152284263959391, 'mean_word_length': 1.7011651816312543, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1738514', 'n_tokens_mistral': 1006, 'n_tokens_neox': 970, 'n_words': 241}