Class: UPM::LogParser

Inherits:
Object
  • Object
show all
Defined in:
lib/upm/log_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, log_glob) ⇒ LogParser

Returns a new instance of LogParser.



4
5
6
7
# File 'lib/upm/log_parser.rb', line 4

def initialize(klass, log_glob)
  @klass = klass
  @log_glob = log_glob
end

Instance Method Details

#displayObject



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

def display
  lesspipe(tail: true) do |less|
    groups = log_events.split_between { |a,b| (b.date.to_i - a.date.to_i) > 60 }

    groups.each do |group|
      first, last = group.first.date, group.last.date
      elapsed     = (last.to_i - first.to_i) / 60

      empty_group = true

      group.each do |ev|
        # Print the header only if the query matched something in this group
        if empty_group
          empty_group = false
          less.puts
          less.puts "<8>== <11>#{first.strftime("<10>%Y-%m-%d <7>at <2>%l:%M %p")} <7>(<9>#{elapsed} <7>minute session) <8>========".colorize
        end

        less.puts ev
      end
    end
  end # lesspipe
end

#log_eventsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/upm/log_parser.rb', line 9

def log_events
  return to_enum(:log_events) unless block_given?

  yielder = proc do |io|
    io.each_line do |line|
      if e = @klass.from_line(line.strip)
        yield e
      end
    end
  end

  logs = Dir[@log_glob].sort_by { |path| File.mtime(path) } 

  logs.each do |log|
    if log =~ /\.gz$/
      IO.popen(["zcat", log], &yielder)
    else
      open(log, &yielder)
    end 
  end
end