<commit_before>import json
from kafka import KeyedProducer
from kafka.common import KafkaUnavailableError
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


class ChangeFeedPillow(PythonPillow):

    def __init__(self, couch_db, kafka, checkpoint):
        super(ChangeFeedPillow, self).__init__(couch_db=couch_db, checkpoint=checkpoint, chunk_size=10)
        self._kafka = kafka
        self._producer = KeyedProducer(self._kafka)

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

    def process_change(self, change, is_retry_attempt=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=self.get_db_name(),
                document_type=document_type,
                document_subtype=_get_document_subtype(change.document),
                domain=change.document.get('domain', None),
                is_deletion=change.deleted,
            )
            self._producer.send_messages(
                bytes(get_topic(document_type)),
                bytes(change_meta.domain),
                bytes(json.dumps(change_meta.to_json())),
            )


def get_default_couch_db_change_feed_pillow():
    default_couch_db = CachedCouchDB(CommCareCase.get_db().uri, readonly=False)
    try:
        kafka_client = get_kafka_client()
    except KafkaUnavailableError:
        logging.warning('Ignoring missing kafka client during unit testing')
        kafka_client = None

    return ChangeFeedPillow(
        couch_db=default_couch_db,
        kafka=kafka_client,
        checkpoint=PillowCheckpoint(get_django_checkpoint_store(), 'default-couch-change-feed')
    )


def _get_document_type(document_or_none):
    return document_or_none.get('doc_type', None) if document_or_none else None
<commit_msg>sleep for a bit when leader is unavailable<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


class ChangeFeedPillow(PythonPillow):

    def __init__(self, couch_db, kafka, checkpoint):
        super(ChangeFeedPillow, self).__init__(couch_db=couch_db, checkpoint=checkpoint, chunk_size=10)
        self._kafka = kafka
        self._producer = KeyedProducer(self._kafka)

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

    def process_change(self, change, is_retry_attempt=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=self.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


def get_default_couch_db_change_feed_pillow():
    default_couch_db = CachedCouchDB(CommCareCase.get_db().uri, readonly=False)
    try:
        kafka_client = get_kafka_client()
    except KafkaUnavailableError:
        logging.warning('Ignoring missing kafka client during unit testing')
        kafka_client = None

    return ChangeFeedPillow(
        couch_db=default_couch_db,
        kafka=kafka_client,
        checkpoint=PillowCheckpoint(get_django_checkpoint_store(), 'default-couch-change-feed')
    )


def _get_document_type(document_or_none):
    return document_or_none.get('doc_type', None) if document_or_none else None

{'fraction_non_alphanumeric': 0.06848520023215322, 'fraction_numerical': 0.0011607661056297156, 'mean_word_length': 3.237704918032787, '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': '4871180', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1496, 'n_words': 288}