def _sanitize(rule, statistics): """Sanitize statistics.""" LOG.debug('sanitize stats %s', statistics) if rule.get('exclude_outliers'): key = operator.attrgetter('count') mean = utils.mean(statistics, key) stddev = utils.stddev(statistics, key, mean) lower = mean - 2 * stddev upper = mean + 2 * stddev inliers, outliers = utils.anomalies(statistics, key, lower, upper) if outliers: LOG.debug('excluded weak datapoints with sample counts %s', [s.count for s in outliers]) statistics = inliers else: LOG.debug('no excluded weak datapoints') # in practice statistics are always sorted by period start, not # strictly required by the API though statistics = statistics[-rule['evaluation_periods']:] result_statistics = [ getattr(stat, rule['statistic']) for stat in statistics ] LOG.debug('pruned statistics to %d', len(statistics)) return result_statistics
def _sanitize(alarm, statistics): """Sanitize statistics.""" LOG.debug('sanitize stats %s', statistics) if alarm.rule.get('exclude_outliers'): key = operator.attrgetter('count') mean = utils.mean(statistics, key) stddev = utils.stddev(statistics, key, mean) lower = mean - 2 * stddev upper = mean + 2 * stddev inliers, outliers = utils.anomalies(statistics, key, lower, upper) if outliers: LOG.debug('excluded weak datapoints with sample counts %s', [s.count for s in outliers]) statistics = inliers else: LOG.debug('no excluded weak datapoints') # in practice statistics are always sorted by period start, not # strictly required by the API though statistics = statistics[-alarm.rule['evaluation_periods']:] result_statistics = [getattr(stat, alarm.rule['statistic']) for stat in statistics] LOG.debug('pruned statistics to %d', len(statistics)) return result_statistics