Class: LogfileInterval::AggregatorSet

Inherits:
Object
  • Object
show all
Defined in:
lib/logfile_interval/aggregator_set.rb

Instance Method Summary collapse

Constructor Details

#initialize(parser_columns) ⇒ AggregatorSet

Returns a new instance of AggregatorSet.



3
4
5
6
7
8
9
10
# File 'lib/logfile_interval/aggregator_set.rb', line 3

def initialize(parser_columns)
  @parser_columns = parser_columns
  @aggregators = {}
  parser_columns.each do |name, options|
    next unless klass = options[:aggregator_class]
    @aggregators[name.to_sym] = klass.new(options.fetch(:custom_options, {}))
  end
end

Instance Method Details

#[](name) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
# File 'lib/logfile_interval/aggregator_set.rb', line 24

def [](name)
  raise ArgumentError, "#{name} field does not exist" unless @aggregators.has_key?(name)
  @aggregators[name.to_sym].values
end

#add(record) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/logfile_interval/aggregator_set.rb', line 12

def add(record)
  @parser_columns.each do |name, options|
    next unless @aggregators[name]
    if record.skip_with_exceptions? && (!options.key?(:noskip) || options[:noskip] == false)
      next
    end

    group_by_value = record[options[:group_by]] if options[:group_by]
    @aggregators[name].add(record[name], group_by_value)
  end
end

#to_hashObject



29
30
31
32
33
34
35
36
# File 'lib/logfile_interval/aggregator_set.rb', line 29

def to_hash
  @aggregators.inject({}) do |h, pair|
    k = pair[0]
    v = pair[1]
    h[k] = v.values
    h
  end
end