def histogram_stats()

in src/kudu/scripts/parse_metrics_log.py [0:0]


def histogram_stats(aggregated_prev, aggregated_cur, m):
  """
  Compute percentile stats for the metric 'm' in the window between two
  metric snapshots.
  """
  if m not in aggregated_prev or m not in aggregated_cur or not isinstance(aggregated_cur, dict):
    return UNKNOWN_PERCENTILES

  prev = aggregated_prev[m]
  cur = aggregated_cur[m]

  # Determine the total count we should expect between the current and previous
  # snapshots.
  delta_total = sum([val for _, val in cur.iteritems()]) - \
      sum([val for _, val in prev.iteritems()])

  if delta_total == 0:
    return UNKNOWN_PERCENTILES
  res = dict()
  cum_count = 0


  # Iterate over all of the buckets for the current and previous snapshots,
  # summing them up, and assigning percentiles to the bucket as appropriate.
  for cur_val, cur_count in sorted(aggregated_cur[m].iteritems()):
    prev_count = prev.get(cur_val, 0)
    delta_count = cur_count - prev_count
    cum_count += delta_count
    percentile = float(cum_count) / delta_total

    # Determine which percentiles this bucket belongs to.
    percentile = float(cum_count) / delta_total
    if 'p50' not in res and percentile > 0.50:
      res['p50'] = cur_val
    if 'p95' not in res and percentile > 0.95:
      res['p95'] = cur_val
    if 'p99' not in res and percentile > 0.99:
      res['p99'] = cur_val
    if 'p999' not in res and percentile > 0.999:
      res['p999'] = cur_val
    if cum_count == delta_total:
      res['max'] = cur_val
  return res