<commit_before>"""
Health check convention.

Reports service health and basic information from the "/api/health" endpoint,
using HTTP 200/503 status codes to indicate healthiness.

"""
from flask import jsonify

from microcosm_flask.naming import singleton_path_for
from microcosm_flask.operations import Operation


class Health(object):
    """
    Wrapper around service health state.

    May contain zero or more "checks" which are just named booleans.
    The overall health is OK if all checks are OK.

    """
    def __init__(self, name):
        self.name = name
        self.checks = {}

    def to_dict(self):
        """
        Encode the name, the status of all checks, and the current overall status.

        Note that:

            >>> all([])
            True

        So the default overall status (in the absence of any checks) will be OK.

        """
        checks = {
            # If desired, synchronous checks can be implemented by providing
            # an object that implements __len__ or __nonzero__; both will be
            # evaluted upon boolean evaluation.
            key: bool(value)
            for key, value in self.checks.items()
        }
        return dict(
            # return the service name helps for routing debugging
            name=self.name,
            ok=all(checks.values()),
            checks=checks,
        )


def configure_health(graph):
    """
    Configure the health endpoint.

    :returns: a handle to the `Health` object, allowing other components to
              manipulate health state.
    """

    health = Health(name=graph.metadata.name)

    @graph.route(singleton_path_for(Health), Operation.Retrieve, Health)
    def current_health():
        dct = health.to_dict()<commit_msg>Omit checks if missing<commit_after>"""
Health check convention.

Reports service health and basic information from the "/api/health" endpoint,
using HTTP 200/503 status codes to indicate healthiness.

"""
from flask import jsonify

from microcosm_flask.naming import singleton_path_for
from microcosm_flask.operations import Operation


class Health(object):
    """
    Wrapper around service health state.

    May contain zero or more "checks" which are just named booleans.
    The overall health is OK if all checks are OK.

    """
    def __init__(self, name):
        self.name = name
        self.checks = {}

    def to_dict(self):
        """
        Encode the name, the status of all checks, and the current overall status.

        Note that:

            >>> all([])
            True

        So the default overall status (in the absence of any checks) will be OK.

        """
        checks = {
            # If desired, synchronous checks can be implemented by providing
            # an object that implements __len__ or __nonzero__; both will be
            # evaluted upon boolean evaluation.
            key: bool(value)
            for key, value in self.checks.items()
        }
        dct = dict(
            # return the service name helps for routing debugging
            name=self.name,
            ok=all(checks.values()),
        )
        if checks:
            dct["checks"] = checks
        return dct


def configure_health(graph):
    """
    Configure the health endpoint.

    :returns: a handle to the `Health` object, allowing other components to
              manipulate health state.
    """

    health = Health(name=graph.metadata.name)

    @graph.route(singleton_path_for(Health), Operation.Retrieve, Health)
    def current_health():
        dct = health.to_dict()
{'fraction_non_alphanumeric': 0.07750419697817572, 'fraction_numerical': 0.003357582540570789, 'mean_word_length': 2.258887876025524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '6944166', 'n_tokens_mistral': 964, 'n_tokens_neox': 905, 'n_words': 355}