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)


20
21
22
23
# File 'lib/logfile_interval/aggregator_set.rb', line 20

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
# File 'lib/logfile_interval/aggregator_set.rb', line 12

def add(record)
  @parser_columns.each do |name, options|
    next unless @aggregators[name]
    group_by_value = record[options[:group_by]] if options[:group_by]
    @aggregators[name].add(record[name], group_by_value)
  end
end

#to_hashObject



25
26
27
28
29
30
31
32
# File 'lib/logfile_interval/aggregator_set.rb', line 25

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