<commit_before>__cmdname__ = 'compare_hosts'
__cmdopts__ = ''

class Module(object):
    def __init__(self, api, account):
        self.api = api

    def command(self, opts, host1, host2):
        """Compare the metrics of two hosts

        """
        rv = self.api.list_checks()
        ips = [host1, host2]

        # Fetch checks first
        checks = [{}, {}]
        for check in rv:
            for i in range(0, len(ips)):
                if check['target'] == ips[i]:
                    checks[i][check['check_id']] = check
                    continue

        # Then metrics
        metrics = [{}, {}]
        for i in range(0, len(ips)):
            for check in checks[i]:
                # TODO - agents
                rv = self.api.list_metrics(check_id=check)
                for m in rv:
                    metrics[i][m['name']] = m

        s = []
        for i in range(0, len(ips)):
            s.append(set([j for j in metrics[i] if metrics[i][j]['enabled']]))

        missing = [ s[0] - s[1], s[1] - s[0]]

        if missing[0]:
            print "Metrics present on %s but not on %s:" % (host1, host2)
            for i in missing[0]:
                print "    %s (%s)" % (
                    i, checks[0][metrics[0][i]['check_id']]['name'])

        if missing[1]:
            print "Metrics present on %s but not on %s:" % (host2, host1)
            for i in missing[1]:
                print "    %s (%s)" % (
                    i, checks[1][metrics[1][i]['check_id']]['name'])<commit_msg>Verify type and agent when comparing metrics<commit_after>import collections

__cmdname__ = 'compare_hosts'
__cmdopts__ = ''


class recursivedefaultdict(collections.defaultdict):
    def __init__(self):
        self.default_factory = type(self)

class Module(object):
    def __init__(self, api, account):
        self.api = api

    def command(self, opts, host1, host2):
        """Compare the metrics of two hosts

        """
        rv = self.api.list_checks()
        ips = [host1, host2]

        # Fetch checks first
        checks = [{}, {}]
        for check in rv:
            for i in range(0, len(ips)):
                if check['target'] == ips[i]:
                    checks[i][check['check_id']] = check
                    continue

        # Then metrics
        metrics = [{}, {}]
        for i in range(0, len(ips)):
            for check in checks[i]:
                # TODO - agents
                rv = self.api.list_metrics(check_id=check)
                for m in rv:
                    metrics[i]["%s`%s" % (m['check_id'], m['name'])] = m

        s = [] # Sets to compare
        # Mapping from metric name/type/agent to check name
        check_names = recursivedefaultdict()
        for i in range(0, len(ips)):
            tmp = []
            for metric_id, m in metrics[i].items():
                if m['enabled']:
                    c = checks[i][m['check_id']]
                    # The set will compare all of the criteria below
                    tmp.append((m['name'], c['type'], c['agent']))
                    check_names[c['agent']][c['type']][m['name']] = c['name']
            s.append(set(tmp))

        missing = [ s[0] - s[1], s[1] - s[0]]

        if missing[0]:
            print "Metrics present on %s but not on %s:" % (host1, host2)
            for i in missing[0]:
                print "    %s (%s/%s/%s)" % (
                    i[0], i[1], i[2], check_names[i[2]][i[1]][i[0]])

        if missing[1]:
            print "Metrics present on %s but not on %s:" % (host2, host1)
            for i in missing[1]:
                print "    %s (%s/%s/%s)" % (
                    i[0], i[1], i[2], check_names[i[2]][i[1]][i[0]])
{'fraction_non_alphanumeric': 0.15268411114108443, 'fraction_numerical': 0.014567035338548692, 'mean_word_length': 1.593006993006993, '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': '3271927', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1119, 'n_words': 309}