5e56d2e105e94af6e17fdfe8aa95fa96f434fefb
[packages/precise/mcollective.git] / plugins / mcollective / aggregate / average.rb
1 module MCollective
2   class Aggregate
3     class Average<Base
4       # Before function is run processing
5       def startup_hook
6         @result[:value] = 0
7         @result[:type] = :numeric
8
9         @count = 0
10
11         # Set default aggregate_function if it is undefined
12         @aggregate_format = "Average of #{@result[:output]}: %f" unless @aggregate_format
13       end
14
15       # Determines the average of a set of numerical values
16       def process_result(value, reply)
17         @result[:value] += value
18         @count += 1
19       end
20
21       # Stops execution of the function and returns a ResultObject
22       def summarize
23         @result[:value] /= @count
24
25         result_class(:numeric).new(@result, @aggregate_format, @action)
26       end
27     end
28   end
29 end