Class: Wukong::Processor::Moments

Inherits:
Group show all
Defined in:
lib/wukong/widget/reducers/moments.rb

Constant Summary

Constants inherited from Wukong::Processor

SerializerError

Instance Attribute Summary collapse

Attributes inherited from Count

#size

Attributes inherited from Accumulator

#group, #key

Attributes included from Hanuman::StageInstanceMethods

#graph

Instance Method Summary collapse

Methods included from DynamicGet

#get, #get_nested, included

Methods inherited from Count

#setup

Methods inherited from Accumulator

#process, #setup

Methods inherited from Wukong::Processor

configure, description, #perform_action, #process, #receive_action, #setup, #stop

Methods included from Logging

included

Methods inherited from Hanuman::Stage

#clone

Methods included from Hanuman::StageClassMethods

#builder, #label, #register, #set_builder

Methods included from Hanuman::StageInstanceMethods

#add_link, #linkable_name, #root

Instance Attribute Details

#measurementsObject

Returns the value of attribute measurements.



9
10
11
# File 'lib/wukong/widget/reducers/moments.rb', line 9

def measurements
  @measurements
end

Instance Method Details

#accumulate(record) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/wukong/widget/reducers/moments.rb', line 36

def accumulate record
  super(record)
  self.of.each do |property|
    if raw = get(property, record)
      self.measurements[property] << (raw.to_f rescue next)
    end
  end
end

#finalize {|{:group => key, :count => size}.merge(:results => results)| ... } ⇒ Object

Yields:



64
65
66
# File 'lib/wukong/widget/reducers/moments.rb', line 64

def finalize
  yield({:group => key, :count => size}.merge(:results => results))
end

#get_key(record) ⇒ Object



14
15
16
17
# File 'lib/wukong/widget/reducers/moments.rb', line 14

def get_key record
  super(record) unless (self.group_by || self.by)
  get(self.group_by || self.by, record)
end

#receive_of(o) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/wukong/widget/reducers/moments.rb', line 19

def receive_of o
  @of = case o
  when String then o.split(',')
  when Array  then o
  else []
  end
end

#resultsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/wukong/widget/reducers/moments.rb', line 45

def results
  {}.tap do |r|
    measurements.each_pair do |property, values|
      r[property] = {}
      next if values.empty?
      count               = values.size.to_f
      r[property][:count] = count.to_i
      
      mean               = values.inject(0.0) { |sum, value| sum += value } / count
      r[property][:mean] = mean
      unless no_std_dev
        variance    = values.inject(0.0) { |sum, value| diff = (value - mean) ; sum += diff * diff } / count
        std         = Math.sqrt(variance)
        r[property][:std_dev] = std
      end
    end
  end
end

#start(record) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/wukong/widget/reducers/moments.rb', line 27

def start record
  super(record)
  @measurements = {}.tap do |m|
    self.of.each do |property|
      m[property] = []
    end
  end
end