Class: LogfileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/picky-statistics/logfile_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ LogfileReader

Returns a new instance of LogfileReader.



15
16
17
18
# File 'lib/picky-statistics/logfile_reader.rb', line 15

def initialize path
  @path = File.expand_path path
  check_file
end

Instance Attribute Details

#last_offsetObject (readonly)

in bytes



6
7
8
# File 'lib/picky-statistics/logfile_reader.rb', line 6

def last_offset
  @last_offset
end

#path=(value) ⇒ Object (writeonly)

Sets the attribute path

Parameters:

  • value

    the value to set the attribute path to.



7
8
9
# File 'lib/picky-statistics/logfile_reader.rb', line 7

def path=(value)
  @path = value
end

Instance Method Details

#calculate_last_offset_from(statistics) ⇒ Object



75
76
77
# File 'lib/picky-statistics/logfile_reader.rb', line 75

def calculate_last_offset_from statistics
  @last_offset += last_offset_from statistics
end

#check_fileObject



19
20
21
22
23
24
25
# File 'lib/picky-statistics/logfile_reader.rb', line 19

def check_file
  if File.exists? @path
    exclaim "Logfile #{@path} found."
  else
    raise "Log file #{@path} not found."
  end
end

#exclaim(text) ⇒ Object



9
10
11
# File 'lib/picky-statistics/logfile_reader.rb', line 9

def exclaim text
  puts text
end

#last_offset_from(statistics) ⇒ Object



78
79
80
# File 'lib/picky-statistics/logfile_reader.rb', line 78

def last_offset_from statistics
  `wc #{statistics}`.split(/\s+/)[3].to_i
end

#process(line) ⇒ Object

Processes one line and returns an array.



64
65
66
67
68
69
70
71
72
73
# File 'lib/picky-statistics/logfile_reader.rb', line 64

def process line
  [
    line[2,  19],
    Float(line[22, 8]),
    line[31, 50].gsub!(/\s*\z/, ''),
    Integer(line[82, 8]),
    Integer(line[92, 3]),
    Integer(line[96, 2])
  ]
end

#resetObject



27
28
29
# File 'lib/picky-statistics/logfile_reader.rb', line 27

def reset
  @last_offset = 0
end

#reset_from(time) ⇒ Object



84
85
86
87
88
89
# File 'lib/picky-statistics/logfile_reader.rb', line 84

def reset_from time
  with_temp_file(offset) do |statistics|
    full[:total].reset_from statistics
  end
  @counts
end

#since(log_offset = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/picky-statistics/logfile_reader.rb', line 31

def since log_offset = nil
  @last_offset = log_offset || 0
  
  start_time = Time.now

  # Add all the data to the results.
  #
  results = []
  with_temp_file(log_offset) do |statistics|
    calculate_last_offset_from statistics
    
    File.open(statistics, 'r') do |file|
      while line = file.gets
        next if line =~ /\A\s*\#/
        results << process(line) rescue nil
      end
    end
  end

  duration = Time.now - start_time
  exclaim "Parsed log from byte #{log_offset} in #{duration}s"
    
  results
end

#since_lastObject



58
59
60
# File 'lib/picky-statistics/logfile_reader.rb', line 58

def since_last
  since @last_offset
end

#with_temp_file(offset = 0) ⇒ Object

Use the offset to speed up statistics gathering.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/picky-statistics/logfile_reader.rb', line 93

def with_temp_file offset = 0
  # Quickly return if no logs have been written since the last time.
  #
  return if last_offset_from(@path) <= last_offset

  Tempfile.open 'picky' do |temp_file|
    temp_path = temp_file.path
    `tail -c +#{offset} #{@path} > #{temp_path}`
      
    yield temp_path
  end
end