Class: LogfileInterval::Interval

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

Defined Under Namespace

Classes: OutOfRange, ParserMismatch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(end_time, length, parser) ⇒ Interval

Returns a new instance of Interval.

Raises:

  • (ArgumentError)


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

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

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



3
4
5
# File 'lib/logfile_interval/interval.rb', line 3

def end_time
  @end_time
end

#lengthObject (readonly)

Returns the value of attribute length.



3
4
5
# File 'lib/logfile_interval/interval.rb', line 3

def length
  @length
end

#parserObject (readonly)

Returns the value of attribute parser.



3
4
5
# File 'lib/logfile_interval/interval.rb', line 3

def parser
  @parser
end

#sizeObject (readonly)

Returns the value of attribute size.



4
5
6
# File 'lib/logfile_interval/interval.rb', line 4

def size
  @size
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



3
4
5
# File 'lib/logfile_interval/interval.rb', line 3

def start_time
  @start_time
end

Instance Method Details

#[](name) ⇒ Object



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

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

#add_record(record) ⇒ Object

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/logfile_interval/interval.rb', line 41

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

#each(&block) ⇒ Object



28
29
30
# File 'lib/logfile_interval/interval.rb', line 28

def each(&block)
  @data.each(&block)
end

#to_hashObject



32
33
34
35
36
37
38
39
# File 'lib/logfile_interval/interval.rb', line 32

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