Navigation Menu

Skip to content

Commit

Permalink
Add groonga-query-log-show-running-queries command
Browse files Browse the repository at this point in the history
It is useful for detecting crash query by passing crash time and query
log. You can get running queries on crash. One of them will be a query
that causes crash.
  • Loading branch information
kou committed Jun 23, 2014
1 parent 4cc57aa commit 8b85483
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
23 changes: 23 additions & 0 deletions bin/groonga-query-log-show-running-queries
@@ -0,0 +1,23 @@
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

require "groonga/query-log/command/show-running-queries"

command = Groonga::QueryLog::Command::ShowRunningQueries.new
exit(command.run(ARGV))
80 changes: 80 additions & 0 deletions lib/groonga/query-log/command/show-running-queries.rb
@@ -0,0 +1,80 @@
# Copyright (C) 2014 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

require "optparse"
require "time"

require "groonga/query-log/version"
require "groonga/query-log/parser"

module Groonga
module QueryLog
module Command
class ShowRunningQueries
def initialize
@timestamp = nil
end

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
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 |timestamp|
@timestamp = Time.parse(timestamp)
end
end

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 @timestamp.nil?
next if statistic.start_time < @timestamp
if statistic.start_time == @timestamp
yield(statistic)
end
throw(tag)
end
end
end
end
parser.parsing_statistics.each do |statistic|
yield(statistic)
end
end
end
end
end
end

0 comments on commit 8b85483

Please sign in to comment.