class GroongaQueryLog::Command::ShowRunningQueries

Public Class Methods

new() click to toggle source
# File lib/groonga-query-log/command/show-running-queries.rb, line 26
def initialize
  @base_time = nil
end

Public Instance Methods

run(command_line) click to toggle source
# File lib/groonga-query-log/command/show-running-queries.rb, line 30
def run(command_line)
  input_paths = create_parser.parse(command_line)
  each_parsing_statistic(input_paths) do |statistic|
    timestamp = statistic.start_time.strftime("%Y-%m-%d %H:%M:%S.%6N")
    puts("#{timestamp}:#{statistic.raw_command}")
  end
  true
end

Private Instance Methods

create_parser() click to toggle source
# File lib/groonga-query-log/command/show-running-queries.rb, line 40
def create_parser
  parser = OptionParser.new
  parser.version = VERSION
  parser.banner += " QUERY_LOG"

  parser.separator("")
  parser.separator("Options:")

  parser.on("--base-time=TIME",
            "Show running queries at TIME",
            "You can use popular time format for TIME such as W3C-DTF",
            "(now)") do |base_time|
    @base_time = Time.parse(base_time)
  end
end
each_parsing_statistic(input_paths) { |statistic| ... } click to toggle source
# File lib/groonga-query-log/command/show-running-queries.rb, line 56
def each_parsing_statistic(input_paths)
  parser = Parser.new
  catch do |tag|
    input_paths.each do |input_path|
      File.open(input_path) do |input|
        parser.parse(input) do |statistic|
          next if @base_time.nil?
          next if statistic.start_time < @base_time
          if statistic.start_time == @base_time
            yield(statistic)
          end
          throw(tag)
        end
      end
    end
  end
  parser.parsing_statistics.each do |statistic|
    yield(statistic)
  end
end