Class: EventMeter::Rollup

Inherits:
Object
  • Object
show all
Defined in:
lib/event_meter/rollup.rb

Constant Summary collapse

MIN_FIELDS =
%w[duration_ms_min interval_ms_min started_at_ms_min].freeze
MAX_FIELDS =
%w[duration_ms_max interval_ms_max started_at_ms_max].freeze
MAX_RENDERABLE_TIMESTAMP_MS =

9999-12-31T23:59:59.999Z

253_402_300_799_999

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = {}) ⇒ Rollup

Returns a new instance of Rollup.



29
30
31
32
# File 'lib/event_meter/rollup.rb', line 29

def initialize(fields = {})
  @fields = {}
  fields.each { |key, value| @fields[key.to_s] = numeric_value(value) }
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



9
10
11
# File 'lib/event_meter/rollup.rb', line 9

def fields
  @fields
end

Class Method Details

.combine(raw_rollups) ⇒ Object



15
16
17
18
19
# File 'lib/event_meter/rollup.rb', line 15

def self.combine(raw_rollups)
  raw_rollups.reduce(new) do |combined, raw|
    combined.merge!(from_hash(raw))
  end
end

.from_hash(hash) ⇒ Object



11
12
13
# File 'lib/event_meter/rollup.rb', line 11

def self.from_hash(hash)
  new(hash || {})
end

.max_field?(field) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/event_meter/rollup.rb', line 25

def self.max_field?(field)
  MAX_FIELDS.include?(field.to_s)
end

.min_field?(field) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/event_meter/rollup.rb', line 21

def self.min_field?(field)
  MIN_FIELDS.include?(field.to_s)
end

Instance Method Details

#add_duration(duration_ms) ⇒ Object



38
39
40
41
42
# File 'lib/event_meter/rollup.rb', line 38

def add_duration(duration_ms)
  return if duration_ms.nil?

  add_metric("duration_ms", duration_ms)
end

#add_interval(interval_ms) ⇒ Object



44
45
46
47
48
# File 'lib/event_meter/rollup.rb', line 44

def add_interval(interval_ms)
  return if interval_ms.nil?

  add_metric("interval_ms", interval_ms)
end

#add_started_at(started_ms) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/event_meter/rollup.rb', line 50

def add_started_at(started_ms)
  return if started_ms.nil?

  value = numeric_value(started_ms)
  fields["started_at_ms_min"] = [fields["started_at_ms_min"], value].compact.min
  fields["started_at_ms_max"] = [fields["started_at_ms_max"], value].compact.max
end

#increment(field, value = 1) ⇒ Object



34
35
36
# File 'lib/event_meter/rollup.rb', line 34

def increment(field, value = 1)
  fields[field.to_s] = fields.fetch(field.to_s, 0) + numeric_value(value)
end

#merge!(other) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/event_meter/rollup.rb', line 58

def merge!(other)
  other.fields.each do |field, value|
    if self.class.min_field?(field)
      fields[field] = [fields[field], value].compact.min
    elsif self.class.max_field?(field)
      fields[field] = [fields[field], value].compact.max
    else
      increment(field, value)
    end
  end

  self
end

#to_h(seconds: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/event_meter/rollup.rb', line 72

def to_h(seconds: nil)
  count = fields.fetch("count", 0)
  duration_count = fields.fetch("duration_ms_count", 0)
  interval_count = fields.fetch("interval_ms_count", 0)
  started_at_min_ms = fields["started_at_ms_min"]
  started_at_max_ms = fields["started_at_ms_max"]
  rate_window_seconds = seconds || inferred_rate_window_seconds(started_at_min_ms, started_at_max_ms)

  {
    count: count,
    success_count: fields.fetch("success_count", 0),
    failure_count: fields.fetch("failure_count", 0),
    skipped_count: fields.fetch("skipped_count", 0),
    started_at_min: iso_time(started_at_min_ms),
    started_at_max: iso_time(started_at_max_ms),
    rate_window_seconds: rate_window_seconds,
    per_second: rate(count, rate_window_seconds),
    per_minute: rate(count * 60, rate_window_seconds),
    duration_ms_count: duration_count,
    duration_ms_sum: fields.fetch("duration_ms_sum", 0),
    duration_ms_avg: average(fields.fetch("duration_ms_sum", 0), duration_count),
    duration_ms_min: fields["duration_ms_min"],
    duration_ms_max: fields["duration_ms_max"],
    interval_ms_count: interval_count,
    interval_ms_sum: fields.fetch("interval_ms_sum", 0),
    interval_ms_avg: average(fields.fetch("interval_ms_sum", 0), interval_count),
    interval_ms_min: fields["interval_ms_min"],
    interval_ms_max: fields["interval_ms_max"]
  }.delete_if { |_key, value| value.nil? }
end