Class: TabsTabs::Metrics::Value::Stats

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Helpers
Defined in:
lib/tabs_tabs/metrics/value/stats.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#normalize_period, #timestamp_range, #to_numeric

Constructor Details

#initialize(period, resolution, values) ⇒ Stats

Returns a new instance of Stats.



11
12
13
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 11

def initialize(period, resolution, values)
  @period, @resolution, @values = period, resolution, values
end

Instance Attribute Details

#periodObject (readonly)

Returns the value of attribute period.



9
10
11
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 9

def period
  @period
end

#resolutionObject (readonly)

Returns the value of attribute resolution.



9
10
11
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 9

def resolution
  @resolution
end

#valuesObject (readonly)

Returns the value of attribute values.



9
10
11
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 9

def values
  @values
end

Instance Method Details

#avgObject



39
40
41
42
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 39

def avg
  return 0 if count.zero?
  (self.sum.to_f / self.count.to_f).round(Config.decimal_precision)
end

#countObject



23
24
25
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 23

def count
  @count ||= values.map { |v| v["count"] }.sum
end

#each(&block) ⇒ Object



44
45
46
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 44

def each(&block)
  values.each(&block)
end

#firstObject



15
16
17
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 15

def first
  values.first
end

#lastObject



19
20
21
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 19

def last
  values.last
end

#maxObject



35
36
37
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 35

def max
  @max ||= values.max_by { |v| v["max"] }["max"]
end

#minObject



31
32
33
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 31

def min
  @min ||= values.min_by { |v| v["min"] }["min"]
end

#sumObject



27
28
29
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 27

def sum
  @sum ||= values.map { |v| v["sum"] }.sum
end

#to_aObject



48
49
50
# File 'lib/tabs_tabs/metrics/value/stats.rb', line 48

def to_a
  values
end