Class: MCollective::Aggregate::Summary

Inherits:
Base
  • Object
show all
Defined in:
lib/mcollective/aggregate/summary.rb

Instance Attribute Summary

Attributes inherited from Base

#action, #aggregate_format, #arguments, #name, #output_name, #result

Instance Method Summary collapse

Methods inherited from Base

#initialize, #result_class

Constructor Details

This class inherits a constructor from MCollective::Aggregate::Base

Instance Method Details

#add_value(value) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/mcollective/aggregate/summary.rb', line 25

def add_value(value)
  if @result[:value].keys.include?(value)
    @result[:value][value] += 1
  else
    @result[:value][value] = 1
  end
end

#process_result(value, reply) ⇒ Object

Increments the value field if value has been seen before Else create a new value field



15
16
17
18
19
20
21
22
23
# File 'lib/mcollective/aggregate/summary.rb', line 15

def process_result(value, reply)
  unless value.nil?
    if value.is_a? Array
      value.map {|val| add_value(val)}
    else
      add_value(value)
    end
  end
end

#startup_hookObject

Before function is run processing



5
6
7
8
9
10
11
# File 'lib/mcollective/aggregate/summary.rb', line 5

def startup_hook
  @result[:value] = {}
  @result[:type] = :collection

  # set default aggregate_format if it is undefined
  @aggregate_format ||= :calculate
end

#summarizeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mcollective/aggregate/summary.rb', line 33

def summarize
  if @aggregate_format == :calculate
    max_key_length = @result[:value].keys.map do |k|
      # Response values retain their types. Here we check
      # if the response is a string and turn it into a string
      # if it isn't one.
      if k.respond_to?(:length)
        k.length
      elsif k.respond_to?(:to_s)
        k.to_s.length
      end
    end.max
    @aggregate_format = "%#{max_key_length}s = %s"
  end

  super
end