Class: Logeater::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/logeater/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, path, options = {}) ⇒ Reader

Returns a new instance of Reader.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/logeater/reader.rb', line 10

def initialize(app, path, options={})
  @app = app
  @path = path
  @filename = File.basename(path)
  @parser = Logeater::Parser.new
  @show_progress = options.fetch :progress, false
  @batch_size = options.fetch :batch_size, 500
  @verbose = options.fetch :verbose, false
  @count = 0
  @requests = {}
  @completed_requests = []
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



8
9
10
# File 'lib/logeater/reader.rb', line 8

def app
  @app
end

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



8
9
10
# File 'lib/logeater/reader.rb', line 8

def batch_size
  @batch_size
end

#filenameObject (readonly)

Returns the value of attribute filename.



8
9
10
# File 'lib/logeater/reader.rb', line 8

def filename
  @filename
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/logeater/reader.rb', line 8

def path
  @path
end

Instance Method Details

#each_lineObject Also known as: scan



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/logeater/reader.rb', line 68

def each_line
  File.open(path) do |file|
    io = File.extname(path) == ".gz" ? Zlib::GzipReader.new(file) : file
    pbar = ProgressBar.create(title: filename, total: file.size, autofinish: false, output: $stderr) if show_progress?
    io.each_line do |line|
      yield line
      pbar.progress = file.pos if show_progress?
    end
    pbar.finish if show_progress?
  end
end

#each_requestObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/logeater/reader.rb', line 81

def each_request
  count = 0
  each_line do |line|
    process_line! line do |request|
      yield request
      count += 1
    end
  end
  count
end

#importObject



30
31
32
33
34
35
36
37
38
# File 'lib/logeater/reader.rb', line 30

def import
  @count = 0
  each_request do |attributes|
    completed_requests.push Logeater::Request.new(attributes)
    save! if completed_requests.length >= batch_size
  end
  save!
  @count
end

#parse(to: $stdout) ⇒ Object



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

def parse(to: $stdout)
  to << "["
  first = true
  each_request do |attributes|
    if first
      first = false
    else
      to << ",\n"
    end

    to << Oj.dump(attributes, mode: :compat)
  end
ensure
  to << "]"
end

#reimportObject



25
26
27
28
# File 'lib/logeater/reader.rb', line 25

def reimport
  remove_existing_entries!
  import
end

#remove_existing_entries!Object



56
57
58
# File 'lib/logeater/reader.rb', line 56

def remove_existing_entries!
  Logeater::Request.where(app: app, logfile: filename).delete_all
end

#show_progress?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/logeater/reader.rb', line 60

def show_progress?
  @show_progress
end

#verbose?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/logeater/reader.rb', line 64

def verbose?
  @verbose
end