<commit_before>import json

from klein import Klein
from twisted.python import log
from twisted.web.http import OK, NOT_IMPLEMENTED, SERVICE_UNAVAILABLE


def write_request_json(request, json_obj):
    request.setHeader('Content-Type', 'application/json')
    request.write(json.dumps(json_obj).encode('utf-8'))


class HealthServer(object):

    app = Klein()
    health_handler = None

    def set_health_handler(self, health_handler):
        """
        Set the handler for the health endpoint.

        :param health_handler:
            The handler for health status requests. This must be a callable
            that returns a Health object.
        """
        self.health_handler = health_handler

    def run(self, host, port, log_file=None):
        """
        Run the server, i.e. start listening for requests on the given host and
        port.

        :param host:
            The address to the interface to listen on.
        :param port:
            The port to bind to.
        :param log_file:
            The file to write logs to.
        """
        self.app.run(host, port, log_file)

    @app.route('/health', methods=['GET'])
    def health(self, request):
        """
        Listens to incoming pings from Marathon on ``/health``.

        :param klein.app.KleinRequest request:
            The Klein HTTP request
        """
        if self.health_handler is None:
            return self._no_health_handler(request)

        health = self.health_handler()
        response_code = OK if health.healthy else SERVICE_UNAVAILABLE
        request.setResponseCode(response_code)
        write_request_json(request, health.json_message)

    def _no_health_handler(self, request):
        log.msg('Request to /health made but no handler is set')
        request.setResponseCode(NOT_IMPLEMENTED)<commit_msg>Switch server over to twisted.logger<commit_after>import json

from klein import Klein
from twisted.logger import Logger
from twisted.web.http import OK, NOT_IMPLEMENTED, SERVICE_UNAVAILABLE


def write_request_json(request, json_obj):
    request.setHeader('Content-Type', 'application/json')
    request.write(json.dumps(json_obj).encode('utf-8'))


class HealthServer(object):

    app = Klein()
    health_handler = None
    log = Logger()

    def set_health_handler(self, health_handler):
        """
        Set the handler for the health endpoint.

        :param health_handler:
            The handler for health status requests. This must be a callable
            that returns a Health object.
        """
        self.health_handler = health_handler

    def run(self, host, port, log_file=None):
        """
        Run the server, i.e. start listening for requests on the given host and
        port.

        :param host:
            The address to the interface to listen on.
        :param port:
            The port to bind to.
        :param log_file:
            The file to write logs to.
        """
        self.app.run(host, port, log_file)

    @app.route('/health', methods=['GET'])
    def health(self, request):
        """
        Listens to incoming pings from Marathon on ``/health``.

        :param klein.app.KleinRequest request:
            The Klein HTTP request
        """
        if self.health_handler is None:
            return self._no_health_handler(request)

        health = self.health_handler()
        response_code = OK if health.healthy else SERVICE_UNAVAILABLE
        request.setResponseCode(response_code)
        write_request_json(request, health.json_message)

    def _no_health_handler(self, request):
        self.log.warn('Request to /health made but no handler is set')
        request.setResponseCode(NOT_IMPLEMENTED)
{'fraction_non_alphanumeric': 0.08434384263001886, 'fraction_numerical': 0.0005389382915656157, 'mean_word_length': 2.4691588785046727, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3792500', 'n_tokens_mistral': 1031, 'n_tokens_neox': 1009, 'n_words': 315}