<commit_before>""" source definition for rancher source """
import time

import prometheus_client.core
from cattleprod import poke
from dotmap import DotMap

from rancon.service import Service
from rancon.tools import getLogger
from . import SourceBase

class RancherSource(SourceBase):
    """ describes a source for rancher """

    required_opts = ('url',)
    additional_opts = ('accesskey', 'secretkey', 'default_name_scheme')

    def __init__(self, url, accesskey=None, secretkey=None,
                 default_name_scheme='%NAME%'):
        self.log = getLogger(__name__)
        self.log.error("INIT: url={}".format(url))
        self.log.error("INIT: accesskey={}".format(str(accesskey)))
        self.log.error("INIT: secretkey={}".format('<SET>' if secretkey
                                                   else None))
        self.log.error("INIT: default_name_scheme={}".format(
            default_name_scheme))
        self.url = url
        self.accesskey = accesskey
        self.secretkey = secretkey
        self.default_name_scheme = default_name_scheme
        self.cache = DotMap()
        self.get_services_summary = prometheus_client.core.Summary('get_services_seconds', 'Number of seconds get_services takes', ())

    def is_rancon(self, service):
        """ Checks if service has a rancon label """
        labels = service.launchConfig.labels
        self.log.debug("EVAL: service '{}' (labels: '{}'".format(
            service.name, ",".join(labels.keys())))
        for label in labels:
            if label == 'rancon' or label.startswith("rancon."):
                self.log.info("EVAL: found matching service '{}' "
                              "by label '{}'"
                              .format(service.name, label))
                return True
        return False

    def get_services(self, **_):
        start = time.time()
        starting_point = self._poke(self.url)
        routable_services = []

        # get all services with rancon(\..+) labels
        services = [spoint for spoint in starting_point.get_services if self.is_rancon(spoint)]

        # create service instances
        self.log.debug("EVAL: found {} potential services by tag, "
                       "checking for endpoints"
                       .format(len(services)))

        for service in services:
            endpoints = service.publicEndpoints or []
            labels = service.launchConfig.labels
            for endpoint in endpoints:
                meta = {k.split(".", 1)[1].replace(".", "_"): v
                        for k, v in labels.items()
                        if k.startswith('rancon.')}
                meta['host'] = endpoint['ipAddress']
                meta['port'] = endpoint['port']
                meta['service'] = service.name
                meta['stack'] = self._get_name_for(service.links.environment)
                meta['environment'] = self._get_name_for(service.links.account)
                if 'name' not in meta:<commit_msg>fix service iteration<commit_after>""" source definition for rancher source """
import time

import prometheus_client.core
from cattleprod import poke
from dotmap import DotMap

from rancon.service import Service
from rancon.tools import getLogger
from . import SourceBase

class RancherSource(SourceBase):
    """ describes a source for rancher """

    required_opts = ('url',)
    additional_opts = ('accesskey', 'secretkey', 'default_name_scheme')

    def __init__(self, url, accesskey=None, secretkey=None,
                 default_name_scheme='%NAME%'):
        self.log = getLogger(__name__)
        self.log.error("INIT: url={}".format(url))
        self.log.error("INIT: accesskey={}".format(str(accesskey)))
        self.log.error("INIT: secretkey={}".format('<SET>' if secretkey
                                                   else None))
        self.log.error("INIT: default_name_scheme={}".format(
            default_name_scheme))
        self.url = url
        self.accesskey = accesskey
        self.secretkey = secretkey
        self.default_name_scheme = default_name_scheme
        self.cache = DotMap()
        self.get_services_summary = prometheus_client.core.Summary('get_services_seconds', 'Number of seconds get_services takes', ())

    def is_rancon(self, service):
        """ Checks if service has a rancon label """
        labels = service.launchConfig.labels
        self.log.debug("EVAL: service '{}' (labels: '{}'".format(
            service.name, ",".join(labels.keys())))
        for label in labels:
            if label == 'rancon' or label.startswith("rancon."):
                self.log.info("EVAL: found matching service '{}' "
                              "by label '{}'"
                              .format(service.name, label))
                return True
        return False

    def get_services(self, **_):
        start = time.time()
        starting_point = self._poke(self.url)
        routable_services = []

        # get all services with rancon(\..+) labels
        services = [spoint for spoint in starting_point.get_services() if self.is_rancon(spoint)]

        # create service instances
        self.log.debug("EVAL: found {} potential services by tag, "
                       "checking for endpoints"
                       .format(len(services)))

        for service in services:
            endpoints = service.publicEndpoints or []
            labels = service.launchConfig.labels
            for endpoint in endpoints:
                meta = {k.split(".", 1)[1].replace(".", "_"): v
                        for k, v in labels.items()
                        if k.startswith('rancon.')}
                meta['host'] = endpoint['ipAddress']
                meta['port'] = endpoint['port']
                meta['service'] = service.name
                meta['stack'] = self._get_name_for(service.links.environment)
                meta['environment'] = self._get_name_for(service.links.account)
                if 'name' not in meta:
{'fraction_non_alphanumeric': 0.11929356881039653, 'fraction_numerical': 0.0006664445184938354, 'mean_word_length': 2.1087519419989644, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2851926', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1561, 'n_words': 385}