Class: Trifle::Stats::Transponder::Average

Inherits:
Object
  • Object
show all
Includes:
Mixins::Packer
Defined in:
lib/trifle/stats/transponder/average.rb

Instance Method Summary collapse

Methods included from Mixins::Packer

included

Instance Method Details

#transpond(series:, path:, key: 'average', sum: 'sum', count: 'count') ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/trifle/stats/transponder/average.rb', line 10

def transpond(series:, path:, key: 'average', sum: 'sum', count: 'count') # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  keys = path.to_s.split('.')
  sum = sum.to_s.split('.')
  count = count.to_s.split('.')
  key = path.to_s.empty? ? key : [path, key].join('.')
  series[:values] = series[:values].map do |data|
    dsum = data.dig(*keys, *sum)
    dcount = data.dig(*keys, *count)
    next data unless dsum && dcount

    dres = (dsum / dcount)
    signal = {
      key => dres.nan? ? BigDecimal(0) : dres
    }
    self.class.deep_merge(data, self.class.unpack(hash: signal))
  end
  series
end