class Fluent::NorikraPlugin::FetchRequest

Constants

METHODS
TAG_TYPES

Attributes

interval[RW]
method[RW]
tag_generator[RW]
tag_prefix[RW]
target[RW]
time[RW]

Public Class Methods

new(method, target, interval, tag_type, tag_arg, tag_prefix) click to toggle source
# File lib/fluent/plugin/norikra/fetch_request.rb, line 9
def initialize(method, target, interval, tag_type, tag_arg, tag_prefix)
  raise ArgumentError, "unknown method '#{method}'" unless METHODS.include?(method.to_sym)

  @method = method.to_sym
  @target = target
  @interval = interval.to_i

  raise ArgumentError, "unknown tag type specifier '#{tag_type}'" unless TAG_TYPES.include?(tag_type.to_s)
  raw_tag_prefix = tag_prefix.to_s
  if (! raw_tag_prefix.empty?) && (! raw_tag_prefix.end_with?('.')) # tag_prefix specified, and ends without dot
    raw_tag_prefix += '.'
  end

  @tag_generator = case tag_type.to_s
                   when 'query_name' then lambda{|query_name,record| raw_tag_prefix + query_name}
                   when 'field'      then lambda{|query_name,record| raw_tag_prefix + (record[tag_arg] || 'NULL')}
                   when 'string'     then lambda{|query_name,record| raw_tag_prefix + tag_arg}
                   else
                     raise "bug"
                   end
  @time = Time.now + 1 # should be fetched soon ( 1sec later )
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/fluent/plugin/norikra/fetch_request.rb, line 32
def <=>(other)
  self.time <=> other.time
end
event(client) click to toggle source
# File lib/fluent/plugin/norikra/fetch_request.rb, line 63
def event(client)
  events = client.event(@target) # [[time(int from epoch), event], ...]
  {@target => events}
end
fetch(client) click to toggle source

returns hash: { tag => [[time, record], …], … }

# File lib/fluent/plugin/norikra/fetch_request.rb, line 41
def fetch(client)
  # events { query_name => [[time, record], ...], ... }
  events = case @method
           when :event then event(client)
           when :sweep then sweep(client)
           else
             raise "BUG: unknown method: #{@method}"
           end

  output = {}

  events.keys.each do |query_name|
    events[query_name].each do |time, record|
      tag = @tag_generator.call(query_name, record)
      output[tag] ||= []
      output[tag] << [time, record]
    end
  end

  output
end
next!() click to toggle source
# File lib/fluent/plugin/norikra/fetch_request.rb, line 36
def next!
  @time = Time.now + @interval
end
sweep(client) click to toggle source
# File lib/fluent/plugin/norikra/fetch_request.rb, line 68
def sweep(client)
  client.sweep(@target) # {query_name => event_array, ...}
end