Class: LogfileInterval::Interval
- Inherits:
-
Object
- Object
- LogfileInterval::Interval
- Defined in:
- lib/logfile_interval/interval.rb
Defined Under Namespace
Classes: OutOfRange, ParserMismatch
Instance Attribute Summary collapse
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #add_record(record) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(end_time, length, parser) ⇒ Interval
constructor
A new instance of Interval.
- #to_hash ⇒ Object
Constructor Details
#initialize(end_time, length, parser) ⇒ Interval
Returns a new instance of Interval.
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, | next unless agg = [:aggregator] @data[name] = agg.new end end |
Instance Attribute Details
#end_time ⇒ Object (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 |
#length ⇒ Object (readonly)
Returns the value of attribute length.
3 4 5 |
# File 'lib/logfile_interval/interval.rb', line 3 def length @length end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
3 4 5 |
# File 'lib/logfile_interval/interval.rb', line 3 def parser @parser end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
4 5 6 |
# File 'lib/logfile_interval/interval.rb', line 4 def size @size end |
#start_time ⇒ Object (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
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, | next unless @data[name] group_by_value = record[[:group_by]] if [: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_hash ⇒ Object
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 |