<commit_before>import aiohttp
from . import Agent


class ConsulAgent(Agent):

    @property
    def leader_url(self):
        return "http://{host}:{port}/v1/status/leader".format(host=self.host,
                                                              port=self.port)

    @property
    def services_url(self):
        return ("http://{host}:{port}/v1/catalog/services"
                .format(host=self.host,
                        port=self.port))

    @property
    def nodes_url(self):
        return ("http://{host}:{port}/v1/catalog/nodes"
                .format(host=self.host,
                        port=self.port))

    @property
    def health_url(self):
        return ("http://{host}:{port}/v1/health/service/"
                .format(host=self.host,
                        port=self.port))

    async def get_json(self, url):
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as resp:
                return await resp.json()

    @property
    async def services(self):
        return await self.get_json(self.services_url)

    @property
    async def nodes(self):
        return await self.get_json(self.nodes_url)

    @property
    def host(self):
        return self._data.get("host", "localhost")

    @property
    def port(self):
        return int(self._data.get("port", 8500))

    async def process(self, event_fn, logger):
        services = await self.services
        logger.debug("Got services: {0}".format(services))

        nodes = await self.nodes
        logger.debug("Got nodes: {0}".format(nodes))<commit_msg>Send consul node health checks<commit_after>import aiohttp

from functools import partial

from . import Agent


class ConsulAgent(Agent):

    @property
    def leader_url(self):
        return "http://{host}:{port}/v1/status/leader".format(host=self.host,
                                                              port=self.port)

    @property
    def services_url(self):
        return ("http://{host}:{port}/v1/catalog/services"
                .format(host=self.host,
                        port=self.port))

    @property
    def nodes_url(self):
        return ("http://{host}:{port}/v1/catalog/nodes"
                .format(host=self.host,
                        port=self.port))

    @property
    def health_url(self):
        def health(host, port, service_name):
            return ("http://{host}:{port}/v1/health/service/{service}"
                    .format(host=host,
                            port=port,
                            service=service_name))

        return partial(health, self.host, self.port)

    async def get_json(self, url):
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as resp:
                return await resp.json()

    @property
    async def services(self):
        return await self.get_json(self.services_url)

    @property
    async def nodes(self):
        return await self.get_json(self.nodes_url)

    async def health(self, service_name):
        return await self.get_json(self.health_url(service_name))

    @property
    def host(self):
        return self._data.get("host", "localhost")

    @property
    def port(self):
        return int(self._data.get("port", 8500))

    async def process(self, event_fn, logger):
        services = await self.services
        logger.debug("Got services: {0}".format(services))

        nodes = await self.nodes
        logger.debug("Got nodes: {0}".format(nodes))
        for service_name, _ in services.items():
            health_nodes = await self.health(service_name)
            for node in health_nodes:
                logger.debug("HealthInfo: {0}".format(node))
                ok = all([check["Status"] == "passing" for check in node["Checks"]])
                logger.debug("Node is Ok? {0}".format(ok))
                event_fn(metric_f=1.0 if ok else 0.0,
                         service=self.prefix + service_name,
                         host=node["Node"])
{'fraction_non_alphanumeric': 0.121745598809819, 'fraction_numerical': 0.006446813786263328, 'mean_word_length': 1.9168474331164136, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4013784', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1108, 'n_words': 243}