<commit_before>import subprocess, socket, math

import diamond.collector


def parse_line(line):
    metric_name, rhs = line.strip().split(':', 1)
    rhs = rhs.strip()
    if ' ' in rhs:
        str_value, units = rhs.split(' ', 1)
        if units not in ('ms', 'ms.'):
            raise ValueError("Cannot parse " + repr(line))
    else:
        str_value = rhs

    try:
        value = float(str_value)
    except:
        value = str_value
    return metric_name, value


class Keyspace(object):
    def __init__(self, name, stats, tables):
        self.name = name
        self.stats = stats
        self.tables = tables

class Table(object):
    def __init__(self, name, stats):
        self.name = name
        self.stats = stats

def clean_key(key):
    return key.replace(' ', '_')

def cfstats():
    output = subprocess.check_output(['nodetool', 'cfstats'])
    lines = [line for line in output.splitlines()
             if line and (line != '----------------')]

    # cfstats output is structured in a very specific way: all lines are
    # key: value pairs prefixed by tabs. everything indented belongs to the 

    keyspaces = []
    for line in lines:
        tab_count = len(line) - len(line.lstrip('\t'))
        if tab_count == 0:
            key, value = parse_line(line)
            assert key == 'Keyspace'<commit_msg>SONIC-2123: Clean up bad metric names that confuse graphite<commit_after>import subprocess, socket, math

import diamond.collector


def parse_line(line):
    metric_name, rhs = line.strip().split(':', 1)
    rhs = rhs.strip()
    if ' ' in rhs:
        str_value, units = rhs.split(' ', 1)
        if units not in ('ms', 'ms.'):
            raise ValueError("Cannot parse " + repr(line))
    else:
        str_value = rhs

    try:
        value = float(str_value)
    except:
        value = str_value
    return metric_name, value


class Keyspace(object):
    def __init__(self, name, stats, tables):
        self.name = name
        self.stats = stats
        self.tables = tables

class Table(object):
    def __init__(self, name, stats):
        self.name = name
        self.stats = stats

def clean_key(key):
    return key.replace(' ', '_').replace(',', '_').replace('(', '').replace(')', '')

def cfstats():
    output = subprocess.check_output(['nodetool', 'cfstats'])
    lines = [line for line in output.splitlines()
             if line and (line != '----------------')]

    # cfstats output is structured in a very specific way: all lines are
    # key: value pairs prefixed by tabs. everything indented belongs to the 

    keyspaces = []
    for line in lines:
        tab_count = len(line) - len(line.lstrip('\t'))
        if tab_count == 0:
            key, value = parse_line(line)
            assert key == 'Keyspace'
{'fraction_non_alphanumeric': 0.13479511143062545, 'fraction_numerical': 0.0035945363048166786, 'mean_word_length': 2.3289473684210527, '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': '6601576', 'n_tokens_mistral': 844, 'n_tokens_neox': 821, 'n_words': 260}