Kouhei Sutou
null+****@clear*****
Mon Dec 4 22:34:44 JST 2017
Kouhei Sutou 2017-12-04 22:34:44 +0900 (Mon, 04 Dec 2017) New Revision: 2db3f4f509871d2f8c6487309f9a892427e903a8 https://github.com/groonga/groonga-query-log/commit/2db3f4f509871d2f8c6487309f9a892427e903a8 Message: Unindent Modified files: lib/groonga-query-log/statistic.rb Modified: lib/groonga-query-log/statistic.rb (+165 -165) =================================================================== --- lib/groonga-query-log/statistic.rb 2017-12-04 22:34:00 +0900 (4897396) +++ lib/groonga-query-log/statistic.rb 2017-12-04 22:34:44 +0900 (24f8100) @@ -17,174 +17,174 @@ require "groonga/command/parser" module GroongaQueryLog - class Statistic - DEFAULT_SLOW_OPERATION_THRESHOLD = 0.1 - DEFAULT_SLOW_RESPONSE_THRESHOLD = 0.2 - - attr_reader :context_id, :start_time, :raw_command - attr_reader :elapsed, :return_code - attr_accessor :slow_operation_threshold, :slow_response_threshold - def initialize(context_id) - @context_id = context_id - @start_time = nil - @command = 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 - - def start(start_time, command) - @start_time = start_time - @raw_command = command - end - - def finish(elapsed, return_code) - @elapsed = elapsed - @return_code = return_code - end - - def command - Groonga::Command::Parser.parse(@raw_command) do |status, command| - case status - when :on_load_start - @loading = false - @command ||= command - when :on_command - @command ||= command - end - end - @command - end - - def elapsed_in_seconds - nano_seconds_to_seconds(@elapsed) - end - - def last_time - @start_time + elapsed_in_seconds - end - - def slow? - elapsed_in_seconds >= @slow_response_threshold - end - - def each_operation - return to_enum(__method__) unless block_given? - - previous_elapsed = 0 - ensure_parse_command - 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[:name], - operation_context_context), - :n_records => operation[:n_records], - :slow? => slow_operation?(relative_elapsed_in_seconds), - } - yield parsed_operation - end - end - - def add_operation(operation) - @operations << operation - end - - def operations - _operations = [] - each_operation do |operation| - _operations << operation - end - _operations - end - - def select_command? - command.name == "select" - end - - def to_hash - data = { - "start_time" => start_time.to_f, - "last_time" => last_time.to_f, - "elapsed" => elapsed_in_seconds, - "return_code" => return_code, - "slow" => slow?, - } - arguments = command.arguments.collect do |key, value| - {"key" => key, "value" => value} - end - data["command"] = { - "raw" => raw_command, - "name" => command.name, - "parameters" => arguments, - } - 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?] - operations << operation_data - end - data["operations"] = operations - data + class Statistic + DEFAULT_SLOW_OPERATION_THRESHOLD = 0.1 + DEFAULT_SLOW_RESPONSE_THRESHOLD = 0.2 + + attr_reader :context_id, :start_time, :raw_command + attr_reader :elapsed, :return_code + attr_accessor :slow_operation_threshold, :slow_response_threshold + def initialize(context_id) + @context_id = context_id + @start_time = nil + @command = 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 + + def start(start_time, command) + @start_time = start_time + @raw_command = command + end + + def finish(elapsed, return_code) + @elapsed = elapsed + @return_code = return_code + end + + def command + Groonga::Command::Parser.parse(@raw_command) do |status, command| + case status + when :on_load_start + @loading = false + @command ||= command + when :on_command + @command ||= command end + end + @command + end + + def elapsed_in_seconds + nano_seconds_to_seconds(@elapsed) + end + + def last_time + @start_time + elapsed_in_seconds + end + + def slow? + elapsed_in_seconds >= @slow_response_threshold + end + + def each_operation + return to_enum(__method__) unless block_given? + + previous_elapsed = 0 + ensure_parse_command + 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[:name], + operation_context_context), + :n_records => operation[:n_records], + :slow? => slow_operation?(relative_elapsed_in_seconds), + } + yield parsed_operation + end + end - private - def nano_seconds_to_seconds(nano_seconds) - nano_seconds / 1000.0 / 1000.0 / 1000.0 - end + def add_operation(operation) + @operations << operation + end - def operation_context(label, context) - return nil if @select_command.nil? - - case label - when "filter" - if @select_command.query and context[:query_used].nil? - context[:query_used] = true - "query: #{@select_command.query}" - else - index = context[:filter_index] - context[:filter_index] += 1 - @select_command.conditions[index] - end - when "sort" - @select_command.sortby - when "score" - @select_command.scorer - when "output" - @select_command.output_columns - when "drilldown" - index = context[:drilldown_index] - context[:drilldown_index] += 1 - @select_command.drilldowns[index] - else - nil - end - end + def operations + _operations = [] + each_operation do |operation| + _operations << operation + end + _operations + end + + def select_command? + command.name == "select" + end + + def to_hash + data = { + "start_time" => start_time.to_f, + "last_time" => last_time.to_f, + "elapsed" => elapsed_in_seconds, + "return_code" => return_code, + "slow" => slow?, + } + arguments = command.arguments.collect do |key, value| + {"key" => key, "value" => value} + end + data["command"] = { + "raw" => raw_command, + "name" => command.name, + "parameters" => arguments, + } + 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?] + operations << operation_data + end + data["operations"] = operations + data + end + + private + def nano_seconds_to_seconds(nano_seconds) + nano_seconds / 1000.0 / 1000.0 / 1000.0 + end + + def operation_context(label, context) + return nil if @select_command.nil? + + case label + when "filter" + if @select_command.query and context[:query_used].nil? + context[:query_used] = true + "query: #{@select_command.query}" + else + index = context[:filter_index] + context[:filter_index] += 1 + @select_command.conditions[index] + end + when "sort" + @select_command.sortby + when "score" + @select_command.scorer + when "output" + @select_command.output_columns + when "drilldown" + index = context[:drilldown_index] + context[:drilldown_index] += 1 + @select_command.drilldowns[index] + else + nil + end + end - def ensure_parse_command - return unless select_command? - @select_command = Groonga::Command::Parser.parse(@raw_command) - end + def ensure_parse_command + return unless select_command? + @select_command = Groonga::Command::Parser.parse(@raw_command) + end - def slow_operation?(elapsed) - elapsed >= @slow_operation_threshold - end - end + def slow_operation?(elapsed) + elapsed >= @slow_operation_threshold + end + end end -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20171204/96f38a7b/attachment-0001.htm