<commit_before>import stats_buffer
import util_cli as util

class SyndromeDetector:
    def run(self, accessor, scale, threshold=None):
        result = {}
        thresholdval = accessor["threshold"]
        if threshold.has_key(accessor["name"]):
            thresholdval = threshold[accessor["name"]]

        main_counter = accessor["counter"][0]

        for bucket, stats_info in stats_buffer.buckets.iteritems():
            values = {}
            for counter in accessor["counter"]:
                if not stats_info[scale].has_key(counter):
                    return result
                values[counter] = stats_info[scale][counter]

            #First one is the main counter we run against
            timestamps = values[main_counter]["timestamp"]
            nodeStats = values[main_counter]["nodeStats"]
            #samplesCount = values[main_counter]["samplesCount"]

            trend = []
            num_warn = []
            for node, vals in nodeStats.iteritems():
                vals = {}
                for counter in accessor["counter"]:
                    vals[counter] = values[counter]["nodeStats"][node]

                node_avg = {}
                #if samplesCount > 0:
                #    node_avg_curr = sum(curr_vals) / samplesCount
                #else:
                #    node_avg_curr = 0

                # Fine grained analysis
                abnormal_segs = util.abnormal_extract(vals[main_counter], thresholdval[main_counter][1], thresholdval[main_counter][0])
                abnormal_vals = []
                for seg in abnormal_segs:
                    begin_index = seg[0]
                    seg_total = seg[1]
                    if seg_total < thresholdval["recurrence"]:
                        continue
                    end_index = begin_index + seg_total

                    seg_avg = {}
                    b = True
                    seg_tuple = ()
                    for counter in accessor["counter"]:
                        seg_avg[counter] = sum(vals[counter][begin_index : end_index]) / seg_total
                        seg_tuple += (util.pretty_float(seg_avg[counter]), )
                        b &= util.evalfunc(thresholdval[counter][1], seg_avg[counter], thresholdval[counter][0])
                        if not b:
                            break

                    if b:
                        seg_tuple = (util.pretty_datetime(timestamps[begin_index]),) + seg_tuple
                        seg_tuple = (util.pretty_datetime(timestamps[end_index-1], True),) + seg_tuple
                        symptom = accessor["symptom"] % seg_tuple

                        num_warn.append({"node":node, "value":symptom})
                        abnormal_vals.append(seg_avg[main_counter])
                if len(abnormal_vals) > 0:
                    trend.append((node, {"value" : util.pretty_float(sum(abnormal_vals)/len(abnormal_vals)) + "%",
                                         "raw" : abnormal_vals}
                                    ))
            if len(num_warn) > 0:
                trend.append(("warn", num_warn))
            result[bucket] = trend<commit_msg>Fix a bug which block report generating<commit_after>import stats_buffer
import util_cli as util

class SyndromeDetector:
    def run(self, accessor, scale, threshold=None):
        result = {}
        thresholdval = accessor["threshold"]
        if threshold.has_key(accessor["name"]):
            thresholdval = threshold[accessor["name"]]

        main_counter = accessor["counter"][0]

        for bucket, stats_info in stats_buffer.buckets.iteritems():
            values = {}
            for counter in accessor["counter"]:
                if not stats_info[scale].has_key(counter):
                    return result
                values[counter] = stats_info[scale][counter]

            #First one is the main counter we run against
            timestamps = values[main_counter]["timestamp"]
            nodeStats = values[main_counter]["nodeStats"]
            #samplesCount = values[main_counter]["samplesCount"]

            trend = []
            num_warn = []
            for node, vals in nodeStats.iteritems():
                vals = {}
                for counter in accessor["counter"]:
                    vals[counter] = values[counter]["nodeStats"][node]

                node_avg = {}
                #if samplesCount > 0:
                #    node_avg_curr = sum(curr_vals) / samplesCount
                #else:
                #    node_avg_curr = 0

                # Fine grained analysis
                abnormal_segs = util.abnormal_extract(vals[main_counter], thresholdval[main_counter][1], thresholdval[main_counter][0])
                abnormal_vals = []
                for seg in abnormal_segs:
                    begin_index = seg[0]
                    seg_total = seg[1]
                    if seg_total < thresholdval["recurrence"]:
                        continue
                    end_index = begin_index + seg_total

                    seg_avg = {}
                    b = False
                    seg_tuple = ()
                    for counter in accessor["counter"]:
                        seg_avg[counter] = sum(vals[counter][begin_index : end_index]) / seg_total
                        seg_tuple += (util.pretty_float(seg_avg[counter]), )
                        #b &= util.evalfunc(thresholdval[counter][1], seg_avg[counter], thresholdval[counter][0])
                        if not b:
                            break

                    if b:
                        seg_tuple = (util.pretty_datetime(timestamps[begin_index]),) + seg_tuple
                        seg_tuple = (util.pretty_datetime(timestamps[end_index-1], True),) + seg_tuple
                        symptom = accessor["symptom"] % seg_tuple

                        num_warn.append({"node":node, "value":symptom})
                        abnormal_vals.append(seg_avg[main_counter])
                if len(abnormal_vals) > 0:
                    trend.append((node, {"value" : util.pretty_float(sum(abnormal_vals)/len(abnormal_vals)) + "%",
                                         "raw" : abnormal_vals}
                                    ))
            if len(num_warn) > 0:
                trend.append(("warn", num_warn))
            result[bucket] = trend
{'fraction_non_alphanumeric': 0.09914880201765448, 'fraction_numerical': 0.0037831021437578815, 'mean_word_length': 1.5108824693312228, 'pattern_counts': {'":': 4, '<': 5, '<?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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4844997', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1571, 'n_words': 311}