Sha256: c7d5df8899cdba9718961b0803a85f76131b1c3d66dfdabe4f64a31c3ef6826d

Contents?: true

Size: 1.34 KB

Versions: 20

Compression:

Stored size: 1.34 KB

Contents

module MCollective
  class Aggregate
    class Summary < Base
      # Before function is run processing
      def startup_hook
        @result[:value] = {}
        @result[:type] = :collection

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

      # Increments the value field if value has been seen before
      # Else create a new value field
      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

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

      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
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
choria-mcorpc-support-2.26.5 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.26.4 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.26.3 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.26.2 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.26.1 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.26.0 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.25.3 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.25.2 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.25.1 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.25.0 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.24.4 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.24.3 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.24.2 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.24.1 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.24.0 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.23.3 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.23.2 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.23.1 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.23.0 lib/mcollective/aggregate/summary.rb
choria-mcorpc-support-2.23.0.pre lib/mcollective/aggregate/summary.rb