Class: Logeater::Reader
- Inherits:
-
Object
- Object
- Logeater::Reader
- Defined in:
- lib/logeater/reader.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#batch_size ⇒ Object
readonly
Returns the value of attribute batch_size.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #each_line ⇒ Object (also: #scan)
- #each_request ⇒ Object
- #import ⇒ Object
-
#initialize(app, path, options = {}) ⇒ Reader
constructor
A new instance of Reader.
- #parse(to: $stdout) ⇒ Object
- #reimport ⇒ Object
- #remove_existing_entries! ⇒ Object
- #show_progress? ⇒ Boolean
- #verbose? ⇒ Boolean
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, ={}) @app = app @path = path @filename = File.basename(path) @parser = Logeater::Parser.new @show_progress = .fetch :progress, false @batch_size = .fetch :batch_size, 500 @verbose = .fetch :verbose, false @count = 0 @requests = {} @completed_requests = [] end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
8 9 10 |
# File 'lib/logeater/reader.rb', line 8 def app @app end |
#batch_size ⇒ Object (readonly)
Returns the value of attribute batch_size.
8 9 10 |
# File 'lib/logeater/reader.rb', line 8 def batch_size @batch_size end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
8 9 10 |
# File 'lib/logeater/reader.rb', line 8 def filename @filename end |
#path ⇒ Object (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_line ⇒ Object 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 = ProgressBar.create(title: filename, total: file.size, autofinish: false, output: $stderr) if show_progress? io.each_line do |line| yield line .progress = file.pos if show_progress? end .finish if show_progress? end end |
#each_request ⇒ Object
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 |
#import ⇒ Object
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 |
#reimport ⇒ Object
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
60 61 62 |
# File 'lib/logeater/reader.rb', line 60 def show_progress? @show_progress end |
#verbose? ⇒ Boolean
64 65 66 |
# File 'lib/logeater/reader.rb', line 64 def verbose? @verbose end |