File: printer.rb

package info (click to toggle)
ruby-ddmetrics 1.0.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 220 kB
  • sloc: ruby: 760; makefile: 3
file content (49 lines) | stat: -rw-r--r-- 1,363 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true

module DDMetrics
  class Printer
    def summary_to_s(summary)
      table_for_summary(summary).to_s
    end

    def counter_to_s(counter)
      table_for_counter(counter).to_s
    end

    private

    def table_for_summary(summary)
      header_labels = nil
      headers = ['count', 'min', '.50', '.90', '.95', 'max', 'tot']

      rows = summary.labels.map do |label|
        header_labels ||= label.to_a.sort.map(&:first).map(&:to_s)
        stats = summary.get(label)

        count = stats.count
        min   = stats.min
        p50   = stats.quantile(0.50)
        p90   = stats.quantile(0.90)
        p95   = stats.quantile(0.95)
        tot   = stats.sum
        max   = stats.max

        label.to_a.sort.map(&:last).map(&:to_s) + [count.to_s] + [min, p50, p90, p95, max, tot].map { |r| format('%4.2f', r) }
      end

      DDMetrics::Table.new([header_labels + headers] + rows, num_headers: header_labels.size)
    end

    def table_for_counter(counter)
      header_labels = nil
      headers = ['count']

      rows = counter.labels.map do |label|
        header_labels ||= label.to_a.sort.map(&:first).map(&:to_s)
        label.to_a.sort.map(&:last).map(&:to_s) + [counter.get(label).to_s]
      end

      DDMetrics::Table.new([header_labels + headers] + rows, num_headers: header_labels.size)
    end
  end
end