class GroongaQueryLog::Command::Extract

Attributes

option_parser[R]
options[RW]

Public Class Methods

new() click to toggle source
# File lib/groonga-query-log/command/extract.rb, line 31
def initialize
  @options = nil
  @option_parser = nil
  setup_options
end

Public Instance Methods

run(arguments) click to toggle source

Executes extractor for Groonga’s query logs. “groonga-query-log-extract” command runs this method.

@example

extractor = GroongaQueryLog::Command::Extract.new
extractor.run("--output", "commands.output",
              "--command", "select",
              "query.log")

If only paths of query log files are specified, this method prints command(s) of them to console.

@param [Array<String>] arguments arguments for

groonga-query-log-extract. Please execute
"groonga-query-log-extract --help" or see #setup_options.
# File lib/groonga-query-log/command/extract.rb, line 52
def run(arguments)
  begin
    log_paths = @option_parser.parse!(arguments)
  rescue OptionParser::ParseError
    $stderr.puts($!.message)
    return false
  end

  begin
    if @options.output
      File.open(@options.output, "w") do |output|
        extract(log_paths, output)
      end
    else
      extract(log_paths, $stdout)
    end
  rescue Interrupt, Errno::EPIPE
  rescue Error
    $stderr.puts($!.message)
    return false
  end

  true
end

Private Instance Methods

extract(log_paths, output) click to toggle source
# File lib/groonga-query-log/command/extract.rb, line 152
def extract(log_paths, output)
  if @options.inspect_query
    formatter = InspectFormatter.new(output)
  else
    formatter = DumpFormatter.new(output)
  end
  formatter.start
  parser = Parser.new
  parse_log(parser, log_paths) do |statistic|
    extract_command(statistic, formatter)
  end
  formatter.finish
end
extract_command(statistic, formatter) click to toggle source
# File lib/groonga-query-log/command/extract.rb, line 166
def extract_command(statistic, formatter)
  command = statistic.command
  return unless target?(command)
  unless @options.include_arguments
    command.arguments.clear
  end
  command_text = nil
  case @options.unify_format
  when "uri"
    command_text = command.to_uri_format
  when "command"
    command_text = command.to_command_format
  else
    command_text = command.to_s
  end
  formatter.command(statistic, command_text)
end
setup_options() click to toggle source
# File lib/groonga-query-log/command/extract.rb, line 78
def setup_options
  @options = OpenStruct.new
  @options.unify_format = nil
  @options.commands = []
  @options.exclude_commands = []
  @options.include_arguments = true
  @options.output = nil
  @options.inspect_query = false
  @option_parser = OptionParser.new do |parser|
    parser.version = VERSION
    parser.banner += " QUERY_LOG1 ..."

    available_formats = ["uri", "command"]
    parser.on("--unify-format=FORMAT",
              available_formats,
              "Unify command format to FORMAT.",
              "(#{available_formats.join(', ')})",
              "[not unify]") do |format|
      @options.unify_format = format
    end

    parser.on("--command=COMMAND",
              "Extract only COMMAND.",
              "To extract one or more commands,",
              "specify this command a number of times.",
              "Use /.../ as COMMAND to match command with regular expression.",
              "[all commands]") do |command|
      case command
      when /\A\/(.*)\/(i)?\z/
        @options.commands << Regexp.new($1, $2 == "i")
      when
        @options.commands << command
      end
    end

    parser.on("--exclude-command=COMMAND",
              "Don't extract COMMAND.",
              "To ignore one or more commands,",
              "specify this command a number of times.",
              "Use /.../ as COMMAND to match command with regular expression.",
              "[no commands]") do |command|
      case command
      when /\A\/(.*)\/(i)?\z/
        @options.exclude_commands << Regexp.new($1, $2 == "i")
      when
        @options.exclude_commands << command
      end
    end

    parser.on("--[no-]include-arguments",
              "Whether include command arguments",
              "[#{@options.include_arguments}]") do |include_arguments|
      @options.include_arguments = include_arguments
    end

    parser.on("--output=OUTPUT",
              "If you specify path as OUTPUT,",
              "executed commands are printed to the path.",
              "If you specify a URL like",
              "http://localhost:10041/?table=QueryLogEntries,",
              "each entry are stored to QueryLogEntries Groonga table",
              "running at localhost on port 10041.",
              "[standard output]") do |output|
      @options.output = output
    end

    parser.on("--[no-]inspect-query",
              "Inspect query.",
              "[#{@options.inspect_query}]") do |boolean|
      @options.inspect_query = boolean
    end
  end
end
target?(command) click to toggle source
# File lib/groonga-query-log/command/extract.rb, line 184
def target?(command)
  return false if command.nil?

  name = command.command_name
  target_commands = @options.commands
  exclude_commands = @options.exclude_commands

  unless target_commands.empty?
    return target_commands.any? {|target_command| target_command === name}
  end

  unless exclude_commands.empty?
    return (not exclude_commands.any? {|exclude_command| exclude_command === name})
  end

  true
end