Sha256: d4c7ce39807957365f7049584133787648df886f655f9c5076b1890989b694b0

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module LogfileInterval
  class Interval
    attr_reader   :start_time, :end_time, :length, :parser
    attr_reader   :size

    class OutOfRange < StandardError; end
    class ParserMismatch < StandardError; end

    def initialize(end_time, length, parser)
      raise ArgumentError, 'end_time must be round' unless (end_time.to_i % length.to_i == 0)
      @end_time   = end_time
      @start_time = end_time - length
      @length     = length
      @parser     = parser
      @size = 0

      @data = {}
      parser.columns.each do |name, options|
        next unless agg = options[:aggregator]
        @data[name] = agg.new
      end
    end

    def [](name)
      @data[name].values
    end

    def add_record(record)
      return unless record.valid?
      raise ParserMismatch unless record.class == parser
      raise OutOfRange, 'too recent' if record.time>@end_time
      raise OutOfRange, 'too old'    if record.time<=@start_time

      @size += 1

      parser.columns.each do |name, options|
        next unless @data[name]
        group_by_value = record[options[:group_by]] if options[:group_by]
        @data[name].add(record[name], group_by_value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logfile_interval-1.1.0 lib/logfile_interval/interval.rb