class Sawyer::Actions
Attributes
logger[R]
status[R]
Public Class Methods
new(opts, action, args, logger)
click to toggle source
# File lib/sawyer/actions.rb, line 21 def initialize opts, action, args, logger environment = opts[:environment].to_sym from = Chronic.parse(opts[:from]) to = Chronic.parse(opts[:to]) @prefix = opts[:prefix] @limit = opts[:limit] @action = action @args = args @logger = logger @status = 0 @elasticsearch = ENVIRONMENTS[environment.to_sym] @status = send action, from, to, args end
Private Instance Methods
search(from, to, request)
click to toggle source
# File lib/sawyer/actions.rb, line 39 def search from, to, request indexes = [] # INF-4114: Limit searches to 24h max max = 24 limit = from + max.hours if to > limit @logger.warn "Truncating search to first #{max} hours" to = limit end date_from = from.getutc.to_date date_to = to.getutc.to_date until date_from > date_to indexes << date_from.strftime("#{@prefix}-%Y.%m.%d") date_from += 1 # day end unless request.has_key?(:size) and request[:size] == 0 request[:size] = @limit end if request.has_key? :aggregations request[:aggregations].each do |name, agg| agg.keys.each do |k| request[:aggregations][name][k].merge!({ size: @limit }) end end end request[:query] = { filtered: { query: request[:query], filter: { bool: { must: { range: { '@timestamp' => { gte: from.iso8601, lte: to.iso8601 } } } } } } } url = "#{@elasticsearch}/#{indexes * ','}/_search?ignore_unavailable=true&timeout=10000" @logger.debug url @logger.debug request.to_json curl_request = Curl::Easy.http_post(url, request.to_json) do |curl| curl.headers['Accept'] = 'application/json' curl.headers['Content-Type'] = 'application/json' curl.verbose = @logger.debug? end JSON::parse curl_request.body_str end