Class: Lstash::Query

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

Defined Under Namespace

Classes: FormatError, QueryMissing

Constant Summary collapse

LOGSTASH_PREFIX =
"logstash-".freeze
WILDCARD_QUERY =
"*".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_string = nil, arguments = {}) ⇒ Query

Returns a new instance of Query.



16
17
18
19
20
21
22
23
24
# File 'lib/lstash/query.rb', line 16

def initialize(query_string = nil, arguments = {})
  @query_string = query_string

  @anchor = time_parse(arguments[:anchor], "today")
  @from = time_parse(arguments[:from], "yesterday")
  @to = time_parse(arguments[:to], "today")

  @to = Time.now if @to > Time.now # prevent accessing non-existing times / indices
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



14
15
16
# File 'lib/lstash/query.rb', line 14

def from
  @from
end

#toObject

Returns the value of attribute to.



14
15
16
# File 'lib/lstash/query.rb', line 14

def to
  @to
end

Instance Method Details

#each_period(step, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/lstash/query.rb', line 53

def each_period(step, &block)
  # iterate over the whole range in blocks of the specified period
  time_iterate(@from.utc, @to.utc - 1, step) do |start_at|
    yield index_name(start_at.to_date),
          Query.new(@query_string,
            anchor: @anchor,
            from: start_at,
            to: start_at + step)
  end
end

#filterObject



44
45
46
47
48
49
50
51
# File 'lib/lstash/query.rb', line 44

def filter
  {
    bool: {
      must: es_query,
      filter: es_filter
    }
  }
end

#index_name(date) ⇒ Object



26
27
28
# File 'lib/lstash/query.rb', line 26

def index_name(date)
  "#{LOGSTASH_PREFIX}#{date.strftime("%Y.%m.%d")}"
end

#search(from, size) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/lstash/query.rb', line 34

def search(from, size)
  {
    sort: sort_order,
    _source: %w[message],
    query: filter,
    from: from,
    size: size
  }
end

#wildcard_indicesObject



30
31
32
# File 'lib/lstash/query.rb', line 30

def wildcard_indices
  "logstash-*"
end