Kouhei Sutou
null+****@clear*****
Thu Jan 12 11:24:34 JST 2017
Kouhei Sutou 2017-01-12 11:24:34 +0900 (Thu, 12 Jan 2017) New Revision: 631868bdecc401793d453b66a378200c5055b282 https://github.com/ranguba/groonga-client-model/commit/631868bdecc401793d453b66a378200c5055b282 Message: Support logging Added files: lib/groonga_client_model/log_subscriber.rb lib/groonga_client_model/railties/controller_runtime.rb Modified files: lib/groonga_client_model/client.rb lib/groonga_client_model/railtie.rb Modified: lib/groonga_client_model/client.rb (+15 -1) =================================================================== --- lib/groonga_client_model/client.rb 2016-12-27 18:49:52 +0900 (a055277) +++ lib/groonga_client_model/client.rb 2017-01-12 11:24:34 +0900 (905cffc) @@ -1,4 +1,4 @@ -# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2016-2017 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 @@ -31,8 +31,22 @@ module GroongaClientModel def open Groonga::Client.open(url: @url) do |client| + client.extend(Notifiable) yield(client) end end + + module Notifiable + private + def execute_command(command, &block) + name = "groonga.groonga_client_model" + payload = { + :command => command, + } + ActiveSupport::Notifications.instrument(name, payload) do + super(command, &block) + end + end + end end end Added: lib/groonga_client_model/log_subscriber.rb (+90 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga_client_model/log_subscriber.rb 2017-01-12 11:24:34 +0900 (4af4077) @@ -0,0 +1,90 @@ +# Copyright (C) 2017 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 + +module GroongaClientModel + class LogSubscriber < ActiveSupport::LogSubscriber + if respond_to?(:thread_cattr_accessor) + thread_cattr_accessor :runtime, instance_accessor: false + else + # For ActiveSupport < 5 + class << self + def runtime + Thread.current["groonga_client_model.log_subscriber.runtime"] + end + + def runtime=(value) + Thread.current["groonga_client_model.log_subscriber.runtime"] = value + end + end + end + + class << self + def reset_runtime + self.runtime = 0.0 + end + + def measure + before_runtime = runtime + yield + runtime - before_runtime + end + end + + def groonga(event) + self.class.runtime += event.duration + + debug do + command = event.payload[:command] + + title = color("#{command.command_name} (#{event.duration.round(1)}ms)", + title_color(command), + true) + formatted_command = color(command.to_command_format, + command_color(command), + true) + " #{title} #{formatted_command}" + end + end + + private + def title_color(command) + if command.command_name == "select" + MAGENTA + else + CYAN + end + end + + def command_color(command) + case command.command_name + when "select" + BLUE + when "load" + GREEN + when "delete" + RED + else + MAGENTA + end + end + + def logger + GroongaClientModel.logger + end + + attach_to :groonga_client_model + end +end Modified: lib/groonga_client_model/railtie.rb (+7 -0) =================================================================== --- lib/groonga_client_model/railtie.rb 2016-12-27 18:49:52 +0900 (876c4a9) +++ lib/groonga_client_model/railtie.rb 2017-01-12 11:24:34 +0900 (c0f9134) @@ -44,6 +44,13 @@ module GroongaClientModel end end + initializer "groonga_client_model.log_runtime" do + require "groonga_client_model/railties/controller_runtime" + ActiveSupport.on_load(:action_controller) do + include GroongaClientModel::Railties::ControllerRuntime + end + end + initializer "groonga_client_model.set_configs" do |app| ActiveSupport.on_load(:groonga_client_model) do app.config.groonga_client_model.each do |key, value| Added: lib/groonga_client_model/railties/controller_runtime.rb (+56 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga_client_model/railties/controller_runtime.rb 2017-01-12 11:24:34 +0900 (9a17d3e) @@ -0,0 +1,56 @@ +# Copyright (C) 2017 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_client_model/log_subscriber" + +module GroongaClientModel + module Railties + module ControllerRuntime + extend ActiveSupport::Concern + + attr_internal :groonga_runtime + + def process_action(action, *args) + GroongaClientModel::LogSubscriber.reset_runtime + super + end + + def cleanup_view_runtime + total_runtime = nil + groonga_runtime_in_view = GroongaClientModel::LogSubscriber.measure do + total_runtime = super + end + total_runtime - groonga_runtime_in_view + end + + def append_info_to_payload(payload) + super + payload[:groonga_runtime] = GroongaClientModel::LogSubscriber.runtime + end + + module ClassMethods + def log_process_action(payload) + messages = super + groonga_runtime = payload[:groonga_runtime] + if groonga_runtime + messages << ("Groonga: %.1fms" % groonga_runtime.to_f) + end + messages + end + end + end + end +end -------------- next part -------------- HTML����������������������������...Download