Class: Logcli::Actions::Elasticsearch

Inherits:
Object
  • Object
show all
Defined in:
lib/logcli/actions/elasticsearch.rb

Constant Summary collapse

BULK_SIZE =
100
INDEX_NAME =
'log-index'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Elasticsearch

Returns a new instance of Elasticsearch.



11
12
13
14
15
16
17
18
# File 'lib/logcli/actions/elasticsearch.rb', line 11

def initialize opts
  @filenames = opts.fetch(:filenames)
  @mapping_file = opts.fetch(:mapping_file)
  @elasticsearch_url = opts.fetch(:elasticsearch_url)
  @buffer = []
  @id = 1
  @total_records = 0
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



7
8
9
# File 'lib/logcli/actions/elasticsearch.rb', line 7

def buffer
  @buffer
end

#elasticsearch_urlObject (readonly)

Returns the value of attribute elasticsearch_url.



7
8
9
# File 'lib/logcli/actions/elasticsearch.rb', line 7

def elasticsearch_url
  @elasticsearch_url
end

#filenamesObject (readonly)

Returns the value of attribute filenames.



7
8
9
# File 'lib/logcli/actions/elasticsearch.rb', line 7

def filenames
  @filenames
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/logcli/actions/elasticsearch.rb', line 7

def id
  @id
end

#mapping_fileObject (readonly)

Returns the value of attribute mapping_file.



7
8
9
# File 'lib/logcli/actions/elasticsearch.rb', line 7

def mapping_file
  @mapping_file
end

#total_recordsObject (readonly)

Returns the value of attribute total_records.



7
8
9
# File 'lib/logcli/actions/elasticsearch.rb', line 7

def total_records
  @total_records
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/logcli/actions/elasticsearch.rb', line 20

def call
  create_mapping if mapping_file

  filenames.each do |filename|
    File.open(filename).each do |line|
      @buffer << line
      if @buffer.count > BULK_SIZE
        flush
      end
    end
  end
  if @buffer.count > 0
    flush
  end
end