Class: Groonga::QueryLog::Extractor

Inherits:
Object
  • Object
show all
Includes:
CommandLineUtils
Defined in:
lib/groonga/query-log/extractor.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandLineUtils

#log_via_stdin?, #stdin_with_pipe?, #stdin_with_redirect?

Constructor Details

#initializeExtractor

Returns a new instance of Extractor.



37
38
39
40
41
# File 'lib/groonga/query-log/extractor.rb', line 37

def initialize
  @options = nil
  @option_parser = nil
  setup_options
end

Instance Attribute Details

#option_parserObject (readonly)

Returns the value of attribute option_parser.



35
36
37
# File 'lib/groonga/query-log/extractor.rb', line 35

def option_parser
  @option_parser
end

#optionsObject

Returns the value of attribute options.



34
35
36
# File 'lib/groonga/query-log/extractor.rb', line 34

def options
  @options
end

Instance Method Details

#run(arguments) ⇒ Object

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

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

Examples:

extractor = Groonga::QueryLog::Extractor.new
extractor.run("--output", "commands.output",
              "--command", "select",
              "query.log")

Parameters:

  • arguments (Array<String>)

    arguments for groonga-query-log-extract. Please execute “groonga-query-log-extract –help” or see #setup_options.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/groonga/query-log/extractor.rb', line 58

def run(arguments)
  begin
    log_paths = @option_parser.parse!(arguments)
  rescue OptionParser::ParseError
    $stderr.puts($!.message)
    return false
  end

  if log_paths.empty?
    unless log_via_stdin?
      $stderr.puts("Error: Please specify input log files.")
      return false
    end
    log = $stdin
  else
    log = log_paths
  end

  if @options.output_path
    File.open(@options.output_path, "w") do |output|
      extract(log, output)
    end
  else
    extract(log, $stdout)
  end

  true
end