<commit_before>import json
from kafka import KeyedProducer
from kafka.common import KafkaUnavailableError, LeaderNotAvailableError
import time
from casexml.apps.case.models import CommCareCase
from corehq.apps.change_feed import data_sources
from corehq.apps.change_feed.connection import get_kafka_client
from corehq.apps.change_feed.topics import get_topic
from couchforms.models import all_known_formlike_doc_types
import logging
from pillowtop.checkpoints.manager import PillowCheckpoint, get_django_checkpoint_store
from pillowtop.couchdb import CachedCouchDB
from pillowtop.feed.interface import ChangeMeta
from pillowtop.listener import PythonPillow
from pillowtop.processor import PillowProcessor


class KafkaProcessor(PillowProcessor):
    """
    Processor that pushes changes to Kafka
    """
    def __init__(self, kafka):
        self._kafka = kafka
        self._producer = KeyedProducer(self._kafka)

    def process_change(self, pillow_instance, change, do_set_checkpoint=False):
        document_type = _get_document_type(change.document)
        if document_type:
            assert change.document is not None
            change_meta = ChangeMeta(
                document_id=change.id,
                data_source_type=data_sources.COUCH,
                data_source_name=pillow_instance.get_db_name(),
                document_type=document_type,
                document_subtype=_get_document_subtype(change.document),
                domain=change.document.get('domain', None),
                is_deletion=change.deleted,
            )
            try:
                self._producer.send_messages(
                    bytes(get_topic(document_type)),
                    bytes(change_meta.domain),
                    bytes(json.dumps(change_meta.to_json())),
                )
            except LeaderNotAvailableError:
                # kafka seems to be down. sleep a bit to avoid crazy amounts of error spam
                time.sleep(15)
                raise


class ChangeFeedPillow(PythonPillow):

    def __init__(self, couch_db, kafka, checkpoint):
        super(ChangeFeedPillow, self).__init__(couch_db=couch_db, checkpoint=checkpoint, chunk_size=10)
        self._processor = KafkaProcessor(kafka)

    def get_db_name(self):
        return self.get_couch_db().dbname

    def process_change(self, change, is_retry_attempt=False):
        self._processor.process_change(self, change)
<commit_msg>push data source info into the processor<commit_after>import json
from kafka import KeyedProducer
from kafka.common import KafkaUnavailableError, LeaderNotAvailableError
import time
from casexml.apps.case.models import CommCareCase
from corehq.apps.change_feed import data_sources
from corehq.apps.change_feed.connection import get_kafka_client
from corehq.apps.change_feed.topics import get_topic
from couchforms.models import all_known_formlike_doc_types
import logging
from pillowtop.checkpoints.manager import PillowCheckpoint, get_django_checkpoint_store
from pillowtop.couchdb import CachedCouchDB
from pillowtop.feed.interface import ChangeMeta
from pillowtop.listener import PythonPillow
from pillowtop.processor import PillowProcessor


class KafkaProcessor(PillowProcessor):
    """
    Processor that pushes changes to Kafka
    """
    def __init__(self, kafka, data_source_type, data_source_name):
        self._kafka = kafka
        self._producer = KeyedProducer(self._kafka)
        self._data_source_type = data_source_type
        self._data_source_name = data_source_name

    def process_change(self, pillow_instance, change, do_set_checkpoint=False):
        document_type = _get_document_type(change.document)
        if document_type:
            assert change.document is not None
            change_meta = ChangeMeta(
                document_id=change.id,
                data_source_type=self._data_source_type,
                data_source_name=self._data_source_name,
                document_type=document_type,
                document_subtype=_get_document_subtype(change.document),
                domain=change.document.get('domain', None),
                is_deletion=change.deleted,
            )
            try:
                self._producer.send_messages(
                    bytes(get_topic(document_type)),
                    bytes(change_meta.domain),
                    bytes(json.dumps(change_meta.to_json())),
                )
            except LeaderNotAvailableError:
                # kafka seems to be down. sleep a bit to avoid crazy amounts of error spam
                time.sleep(15)
                raise


class ChangeFeedPillow(PythonPillow):

    def __init__(self, couch_db, kafka, checkpoint):
        super(ChangeFeedPillow, self).__init__(couch_db=couch_db, checkpoint=checkpoint, chunk_size=10)
        self._processor = KafkaProcessor(
            kafka, data_source_type=data_sources.COUCH, data_source_name=self.get_db_name()
        )

    def get_db_name(self):
        return self.get_couch_db().dbname

    def process_change(self, change, is_retry_attempt=False):
        self._processor.process_change(self, change)

{'fraction_non_alphanumeric': 0.06554818571985954, 'fraction_numerical': 0.0015606710885680843, 'mean_word_length': 2.9898832684824903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '1665448', 'n_tokens_mistral': 1472, 'n_tokens_neox': 1409, 'n_words': 299}