class GroongaQueryLog::Statistic

Constants

DEFAULT_SLOW_OPERATION_THRESHOLD
DEFAULT_SLOW_RESPONSE_THRESHOLD

Attributes

context_id[R]
elapsed[R]
raw_command[R]
return_code[R]
slow_operation_threshold[RW]
slow_response_threshold[RW]
start_time[R]

Public Class Methods

new(context_id) click to toggle source
# File lib/groonga-query-log/statistic.rb, line 27
def initialize(context_id)
  @context_id = context_id
  @start_time = nil
  @raw_command = nil
  @operations = []
  @elapsed = nil
  @return_code = 0
  @slow_operation_threshold = DEFAULT_SLOW_OPERATION_THRESHOLD
  @slow_response_threshold = DEFAULT_SLOW_RESPONSE_THRESHOLD
end

Public Instance Methods

add_operation(operation) click to toggle source
# File lib/groonga-query-log/statistic.rb, line 105
def add_operation(operation)
  @operations << operation
end
cache_used?() click to toggle source
# File lib/groonga-query-log/statistic.rb, line 69
def cache_used?
  each_operation.any? do |operation|
    operation[:name] == "cache"
  end
end
command() click to toggle source
# File lib/groonga-query-log/statistic.rb, line 48
def command
  @command ||= parse_command
end
each_operation() { |parsed_operation| ... } click to toggle source
# File lib/groonga-query-log/statistic.rb, line 75
def each_operation
  return to_enum(__method__) unless block_given?

  previous_elapsed = 0
  operation_context_context = {
    :filter_index => 0,
    :drilldown_index => 0,
  }
  @operations.each_with_index do |operation, i|
    relative_elapsed = operation[:elapsed] - previous_elapsed
    relative_elapsed_in_seconds = nano_seconds_to_seconds(relative_elapsed)
    previous_elapsed = operation[:elapsed]
    parsed_operation = {
      :i => i,
      :elapsed => operation[:elapsed],
      :elapsed_in_seconds => nano_seconds_to_seconds(operation[:elapsed]),
      :relative_elapsed => relative_elapsed,
      :relative_elapsed_in_seconds => relative_elapsed_in_seconds,
      :name => operation[:name],
      :context => operation_context(operation,
                                    operation_context_context),
      :n_records => operation[:n_records],
      :extra => operation[:extra],
      :slow? => slow_operation?(relative_elapsed_in_seconds),
      :raw_message => operation[:raw_message],
    }
    yield parsed_operation
  end
end
elapsed_in_seconds() click to toggle source
# File lib/groonga-query-log/statistic.rb, line 52
def elapsed_in_seconds
  nano_seconds_to_seconds(@elapsed)
end
end_time() click to toggle source
# File lib/groonga-query-log/statistic.rb, line 56
def end_time
  @start_time + elapsed_in_seconds
end
finish(elapsed, return_code) click to toggle source
# File lib/groonga-query-log/statistic.rb, line 43
def finish(elapsed, return_code)
  @elapsed = elapsed
  @return_code = return_code
end
last_time() click to toggle source

@deprecated since 1.5.3. Use {end_time} instead.

# File lib/groonga-query-log/statistic.rb, line 61
def last_time
  end_time
end
operations() click to toggle source
# File lib/groonga-query-log/statistic.rb, line 109
def operations
  _operations = []
  each_operation do |operation|
    _operations << operation
  end
  _operations
end
select_family_command?() click to toggle source
# File lib/groonga-query-log/statistic.rb, line 117
def select_family_command?
  return false if command.nil?
  case command.command_name
  when "select", "range_filter"
    true
  when "logical_select", "logical_range_filter"
    true
  else
    false
  end
end
slow?() click to toggle source
# File lib/groonga-query-log/statistic.rb, line 65
def slow?
  elapsed_in_seconds >= @slow_response_threshold
end
start(start_time, command) click to toggle source
# File lib/groonga-query-log/statistic.rb, line 38
def start(start_time, command)
  @start_time = start_time
  @raw_command = command
end
to_hash() click to toggle source
# File lib/groonga-query-log/statistic.rb, line 129
def to_hash
  data = {
    "start_time" => start_time.to_f,
    "end_time" => end_time.to_f,
    "elapsed" => elapsed_in_seconds,
    "return_code" => return_code,
    "slow" => slow?,
  }
  if command
    data["command"] = {
      "raw" => raw_command,
      "name" => command.name,
      "parameters" => command_arguments,
    }
  else
    data["command"] = {
      "raw" => raw_command
    }
  end
  operations = []
  each_operation do |operation|
    operation_data = {}
    operation_data["name"] = operation[:name]
    operation_data["relative_elapsed"] = operation[:relative_elapsed_in_seconds]
    operation_data["context"] = operation[:context]
    operation_data["slow"] = operation[:slow?]
    operation_data["n_records"] = operation[:n_records]
    operations << operation_data
  end
  data["operations"] = operations
  data
end

Private Instance Methods

command_arguments() click to toggle source
# File lib/groonga-query-log/statistic.rb, line 213
def command_arguments
  command.arguments.collect do |key, value|
    {"key" => key, "value" => value}
  end
end
nano_seconds_to_seconds(nano_seconds) click to toggle source
# File lib/groonga-query-log/statistic.rb, line 171
def nano_seconds_to_seconds(nano_seconds)
  nano_seconds / 1000.0 / 1000.0 / 1000.0
end
operation_context(operation, context) click to toggle source
# File lib/groonga-query-log/statistic.rb, line 175
def operation_context(operation, context)
  return nil unless select_family_command?

  extra = operation[:extra]
  return extra if extra

  label = operation[:name]
  case label
  when "filter"
    query = nil
    query = command.query if command.respond_to?(:query)
    if query and context[:query_used].nil?
      context[:query_used] = true
      "query: #{query}"
    else
      index = context[:filter_index]
      context[:filter_index] += 1
      command.conditions[index]
    end
  when "sort"
    command.sortby
  when "score"
    command.scorer
  when "output"
    command.output_columns
  when "drilldown"
    index = context[:drilldown_index]
    context[:drilldown_index] += 1
    command.drilldowns[index]
  else
    nil
  end
end
parse_command() click to toggle source
# File lib/groonga-query-log/statistic.rb, line 163
def parse_command
  command = nil
  Groonga::Command::Parser.parse(@raw_command) do |_status, _command|
    command = _command
  end
  command
end
slow_operation?(elapsed) click to toggle source
# File lib/groonga-query-log/statistic.rb, line 209
def slow_operation?(elapsed)
  elapsed >= @slow_operation_threshold
end