Class: Stashquery::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/stash-query/query.rb

Constant Summary collapse

DEFAULT_FIELD =
"message"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf = {}) ⇒ Query



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/stash-query/query.rb', line 24

def initialize(conf = {})
  @config = {}
  @config[:host] = conf[:host] || "ls2-es-lb.int.tropo.com"
  @config[:port] = conf[:port] || "9200"
  if conf[:index_prefixes].is_a? Array and ! conf[:index_prefixes].empty?
    @config[:index_prefixes] = conf[:index_prefixes]
  else
    @config[:index_prefixes] = [ "logstash-" ]
  end
  @config[:scroll_size] = conf[:scroll_size] || "100"
  @config[:scroll_time] = conf[:scroll_time] || "30m"
  @config[:output] = conf[:output_file] || nil
  @query = conf[:query] || nil
  @tags = conf[:tags] || nil
  @start_date = conf[:start_date]
  @end_date = conf[:end_date]
  @config[:write_fields] = []
  set_write_fields(conf[:write_fields])
  @config[:delimiter] = conf[:delimiter] || ','
  @num_results = 0
  @query_finished = false
  @scroll_ids = Array.new

  if conf[:do_print]
    @config[:print] = true
    require 'progress_bar'
  end

  ## Do this better
  unless Query.validate_date(@start_date) and Query.validate_date(@end_date)
    raise "Improper date format entered"
  end

  ## Cleanup output file. Probably a better way to do this.
  unless @config[:output].nil?
    begin
      File.truncate(@config[:output],0)
    rescue
    end
  end

  @es_conn = connect_to_es
  run_query
  sort_file
end

Instance Attribute Details

#end_dateObject (readonly)

Returns the value of attribute end_date.



22
23
24
# File 'lib/stash-query/query.rb', line 22

def end_date
  @end_date
end

#num_resultsObject (readonly)

Returns the value of attribute num_results.



20
21
22
# File 'lib/stash-query/query.rb', line 20

def num_results
  @num_results
end

#query_finishedObject (readonly)

Returns the value of attribute query_finished.



19
20
21
# File 'lib/stash-query/query.rb', line 19

def query_finished
  @query_finished
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



21
22
23
# File 'lib/stash-query/query.rb', line 21

def start_date
  @start_date
end

Class Method Details

.validate_date(str) ⇒ Object



70
71
72
73
# File 'lib/stash-query/query.rb', line 70

def self.validate_date(str)
  return true if str =~ /20[0-9]{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])T[012][0-9]:[0-5][0-9]:[0-5][0-9]\.[0-9]{3}Z/
  return nil
end