Class: Ultragrep::LogCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/ultragrep/log_collector.rb

Constant Summary collapse

DATE_FROM_FILENAME =

this constant is pretty implentation-specific. fix at will.

/(\d+)(\.\w+)?$/
HOUR =
60 * 60
DAY =
24 * HOUR

Instance Method Summary collapse

Constructor Details

#initialize(globs, options) ⇒ LogCollector

Returns a new instance of LogCollector.



11
12
13
# File 'lib/ultragrep/log_collector.rb', line 11

def initialize(globs, options)
  @globs, @options = globs, options
end

Instance Method Details

#collect_filesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ultragrep/log_collector.rb', line 15

def collect_files
  file_list = Dir.glob(@globs)
  file_lists = if @options[:tail]
    # TODO fix before we open source -- this is a hard-coded file format.
    tail_list = file_list.map do |f|
      today = Time.now.strftime("%Y%m%d")
      "tail -f #{f}" if f =~ /-#{today}$/
    end.compact
    [tail_list]
  else
    filter_and_group_files(file_list)
  end

  return nil if file_lists.empty?

  $stderr.puts("Grepping #{file_lists.map { |f| f.join(" ") }.join("\n\n\n")}") if @options[:verbose]
  file_lists
end

#filter_and_group_files(files) ⇒ Object



34
35
36
37
38
# File 'lib/ultragrep/log_collector.rb', line 34

def filter_and_group_files(files)
  files = filter_files_by_host(files)
  files = filter_files_by_date(files, @options.fetch(:range_start)..@options.fetch(:range_end))
  files.group_by { |f| f[DATE_FROM_FILENAME, 1] }.values
end

#filter_files_by_date(files, range) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ultragrep/log_collector.rb', line 45

def filter_files_by_date(files, range)
  files.select do |file|
    filename_date = file[DATE_FROM_FILENAME, 1]
    if filename_date.nil?
      $stderr.puts("Could not parse date out of #{file}, skipping.")
      next
    end

    begin
      logfile_date = Time.parse(filename_date).to_i
    rescue
      $stderr.puts("Could not parse date out of #{file}, skipping.")
      next
    end
    range_overlap?(range, logfile_date..(logfile_date + DAY - 1))
  end
end

#filter_files_by_host(files) ⇒ Object



40
41
42
43
# File 'lib/ultragrep/log_collector.rb', line 40

def filter_files_by_host(files)
  return files unless @options[:host_filter]
  files.select { |file| @options[:host_filter].include?(file.split("/")[-2]) }
end

#range_overlap?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ultragrep/log_collector.rb', line 63

def range_overlap?(a, b)
  a.first <= b.last && b.first <= a.last
end