tire-contrib 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tire-contrib.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Karel Minarik and Contributors
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,16 @@
1
+ Tire Contributed Components
2
+ ================================
3
+
4
+ Contributions, additions, extensions and other utilities for the [Tire](http://karmi.github.com/tire/)
5
+ Rubygem for _ElasticSearch_.
6
+
7
+ See specific files or folders inside the `lib/tire` folder.
8
+
9
+ rails-logger
10
+ ------------
11
+
12
+ Adds support for displaying Tire related statistics to Rails' log.
13
+
14
+ -----
15
+
16
+ [Karel Minarik](http://karmi.cz) and [contributors](http://github.com/karmi/tire-contrib/contributors)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,34 @@
1
+ require 'active_support/core_ext/module/attr_internal'
2
+
3
+ module Tire
4
+ module Rails
5
+ module ControllerRuntime
6
+ extend ActiveSupport::Concern
7
+
8
+ protected
9
+
10
+ attr_internal :tire_runtime
11
+
12
+ def cleanup_view_runtime
13
+ tire_rt_before_render = Tire::Rails::LogSubscriber.reset_runtime
14
+ runtime = super
15
+ tire_rt_after_render = Tire::Rails::LogSubscriber.reset_runtime
16
+ self.tire_runtime = tire_rt_before_render + tire_rt_after_render
17
+ runtime - tire_rt_after_render
18
+ end
19
+
20
+ def append_info_to_payload(payload)
21
+ super
22
+ payload[:tire_runtime] = (tire_runtime || 0) + Tire::Rails::LogSubscriber.reset_runtime
23
+ end
24
+
25
+ module ClassMethods
26
+ def log_process_action(payload)
27
+ messages, tire_runtime = super, payload[:tire_runtime]
28
+ messages << ("Search: %.1fms" % tire_runtime.to_f) if tire_runtime
29
+ messages
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ module Tire
2
+ module Rails
3
+ module Instrumentation
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ alias_method_chain :perform, :instrumentation
8
+ end
9
+
10
+ module InstanceMethods
11
+ def perform_with_instrumentation
12
+ # Wrapper around the Search.perform method that logs search times.
13
+ #
14
+ ActiveSupport::Notifications.instrument("search.tire", :name => 'Search', :search => self.to_json) do
15
+ perform_without_instrumentation
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ module Tire
2
+ module Rails
3
+ class LogSubscriber < ActiveSupport::LogSubscriber
4
+ def self.runtime=(value)
5
+ Thread.current["tire_search_runtime"] = value
6
+ end
7
+
8
+ def self.runtime
9
+ Thread.current["tire_search_runtime"] ||= 0
10
+ end
11
+
12
+ def self.reset_runtime
13
+ rt, self.runtime = runtime, 0
14
+ rt
15
+ end
16
+
17
+ def search(event)
18
+ self.class.runtime += event.duration
19
+ return unless logger.debug?
20
+
21
+ payload = event.payload
22
+
23
+ name = "%s (%.1fms)" % [payload[:name], event.duration]
24
+ query = payload[:search].to_s.squeeze ' '
25
+
26
+ debug " #{color(name, BLUE, true)} #{query}"
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ # Register with namespace 'tire', so event names look like '*.tire',
33
+ # e.g. 'search.tire' will invoke the search method in the LogSubscriber above.
34
+ #
35
+ Tire::Rails::LogSubscriber.attach_to :tire
@@ -0,0 +1,25 @@
1
+ module Tire
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+
5
+ initializer "tire.initializer" do |app|
6
+ require 'tire/rails-logger/instrumentation'
7
+ require 'tire/rails-logger/log_subscriber'
8
+ require 'tire/rails-logger/controller_runtime'
9
+
10
+ # Inject instrumentation into Tire::Search
11
+ #
12
+ Tire::Search::Search.module_eval do
13
+ include Tire::Rails::Instrumentation
14
+ end
15
+
16
+ # Hook into Rails controllers to provide logging
17
+ #
18
+ ActiveSupport.on_load(:action_controller) do
19
+ include Tire::Rails::ControllerRuntime
20
+ end
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ # Rails Logger
2
+ # ============
3
+ #
4
+ # Author: Oliver Eilhard <oliver.eilhard@gmail.com>
5
+ #
6
+ #
7
+ # Adds support for displaying Tire related statistics to Rails' log.
8
+ #
9
+ # Hooks into the ActiveSupport instrumentation framework to publish statistics
10
+ # about searches, and display them in the application log.
11
+ #
12
+ # Usage:
13
+ # ------
14
+ #
15
+ # Require the component in your `application.rb`:
16
+ #
17
+ # require 'tire/rails-logger'
18
+ #
19
+ # You should see an output like this in your application log:
20
+ #
21
+ # Started GET "/articles?utf8=%E2%9C%93&q=bull*&commit=search" for 127.0.0.1 at 2011-09-15 19:07:00 +0200
22
+ # Processing by ArticlesController#index as HTML
23
+ # Parameters: {"utf8"=>"✓", "q"=>"bull*", "commit"=>"search"}
24
+ # Tire search (6.7ms) {"query":{"query_string":{"query":"bull*"}}}
25
+ # Article Load (0.3ms) SELECT `articles`.* FROM `articles` WHERE `articles`.`id` IN (104126575)
26
+ # Rendered articles/index.html.erb within layouts/application (13.0ms)
27
+ # Completed 200 OK in 63ms (Views: 53.7ms | ActiveRecord: 1.8ms | Tire: 6.7ms)
28
+ #
29
+ #
30
+ require 'tire/rails-logger/railtie'
@@ -0,0 +1,5 @@
1
+ module Tire
2
+ module Contrib
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ # Require all Tire::Contrib components
2
+ #
3
+ Dir[ File.join File.expand_path('../', __FILE__), 'tire', '*.rb' ].each do |component|
4
+ p component
5
+ # require component
6
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+
4
+ class ActiveRecordArticle < ActiveRecord::Base
5
+
6
+ include Tire::Model::Search
7
+ include Tire::Model::Callbacks
8
+
9
+ tire do
10
+ mapping do
11
+ indexes :title, :type => 'string', :boost => 10, :analyzer => 'snowball'
12
+ indexes :created_at, :type => 'date'
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,70 @@
1
+ require 'test_helper'
2
+ require 'active_record'
3
+ require 'active_support/core_ext/module/aliasing'
4
+ require 'active_support/log_subscriber/test_helper'
5
+ require 'tire/rails-logger/log_subscriber'
6
+ require 'tire/rails-logger/instrumentation'
7
+
8
+ require File.expand_path('../active_record_article', __FILE__)
9
+
10
+ module Tire
11
+ module Rails
12
+
13
+ class LogSubscriberTest < Test::Unit::TestCase
14
+
15
+ include ActiveSupport::LogSubscriber::TestHelper
16
+
17
+ def setup
18
+ super
19
+
20
+ # Make sure instrumentation wraps the perform method
21
+ #
22
+ Tire::Search::Search.module_eval do
23
+ include Tire::Rails::Instrumentation
24
+ end
25
+
26
+ # Attach log subscriber
27
+ #
28
+ Tire::Rails::LogSubscriber.attach_to :tire
29
+
30
+ ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" )
31
+
32
+ ActiveRecord::Migration.verbose = false
33
+ ActiveRecord::Schema.define(:version => 1) do
34
+ create_table :active_record_articles do |t|
35
+ t.string :title
36
+ t.datetime :created_at, :default => 'NOW()'
37
+ end
38
+ end
39
+
40
+ ActiveRecordArticle.destroy_all
41
+ Tire.index('active_record_articles').delete
42
+
43
+ 1.upto(9) { |number| ActiveRecordArticle.create :title => "Test#{number}" }
44
+ ActiveRecordArticle.index.refresh
45
+
46
+ load File.expand_path('../active_record_article.rb', __FILE__)
47
+ end
48
+
49
+ def teardown
50
+ ActiveRecordArticle.destroy_all
51
+ Tire.index('active_record_articles').delete
52
+ super
53
+ end
54
+
55
+ context "Rails notifications" do
56
+
57
+ should "log event on search" do
58
+ ActiveRecordArticle.search '*', :load => true
59
+ wait
60
+ assert_equal 1, @logger.logged(:debug).size
61
+ assert_match /Tire/, @logger.logged(:debug).last
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+ end
70
+
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+
3
+ require 'pathname'
4
+ require 'test/unit'
5
+
6
+ require 'shoulda'
7
+ require 'turn' unless ENV["TM_FILEPATH"] || ENV["CI"]
8
+ require 'mocha'
9
+
10
+ require 'tire'
11
+
12
+ class Test::Unit::TestCase
13
+
14
+ def mock_response(body, code=200, headers={})
15
+ Tire::HTTP::Response.new(body, code, headers)
16
+ end
17
+
18
+ def fixtures_path
19
+ Pathname( File.expand_path( 'fixtures', File.dirname(__FILE__) ) )
20
+ end
21
+
22
+ def fixture_file(path)
23
+ File.read File.expand_path( path, fixtures_path )
24
+ end
25
+
26
+ end
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "tire-contrib/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "tire-contrib"
7
+ s.version = Tire::Contrib::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Karel Minarik", "Oliver Eilhard"]
10
+ s.email = ["karmi@karmi.cz", "oliver.eilhard@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Contributions and additions for the Tire gem}
13
+
14
+ s.rubyforge_project = "tire-contrib"
15
+
16
+ s.add_dependency "bundler", "~> 1.0.0"
17
+ s.add_dependency "tire"
18
+
19
+ s.add_development_dependency "turn"
20
+ s.add_development_dependency "shoulda"
21
+ s.add_development_dependency "mocha"
22
+ s.add_development_dependency "sdoc"
23
+ s.add_development_dependency "rcov"
24
+
25
+ s.extra_rdoc_files = [ "README.markdown", "MIT-LICENSE" ]
26
+ s.rdoc_options = [ "--charset=UTF-8" ]
27
+
28
+ s.files = `git ls-files`.split("\n")
29
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
30
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
31
+ s.require_paths = ["lib"]
32
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tire-contrib
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Karel Minarik
13
+ - Oliver Eilhard
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-16 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 0
32
+ version: 1.0.0
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: tire
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: turn
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: shoulda
61
+ prerelease: false
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ type: :development
70
+ version_requirements: *id004
71
+ - !ruby/object:Gem::Dependency
72
+ name: mocha
73
+ prerelease: false
74
+ requirement: &id005 !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id005
83
+ - !ruby/object:Gem::Dependency
84
+ name: sdoc
85
+ prerelease: false
86
+ requirement: &id006 !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ type: :development
94
+ version_requirements: *id006
95
+ - !ruby/object:Gem::Dependency
96
+ name: rcov
97
+ prerelease: false
98
+ requirement: &id007 !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ type: :development
106
+ version_requirements: *id007
107
+ description:
108
+ email:
109
+ - karmi@karmi.cz
110
+ - oliver.eilhard@gmail.com
111
+ executables: []
112
+
113
+ extensions: []
114
+
115
+ extra_rdoc_files:
116
+ - README.markdown
117
+ - MIT-LICENSE
118
+ files:
119
+ - .gitignore
120
+ - Gemfile
121
+ - MIT-LICENSE
122
+ - README.markdown
123
+ - Rakefile
124
+ - lib/tire-contrib.rb
125
+ - lib/tire-contrib/version.rb
126
+ - lib/tire/rails-logger.rb
127
+ - lib/tire/rails-logger/controller_runtime.rb
128
+ - lib/tire/rails-logger/instrumentation.rb
129
+ - lib/tire/rails-logger/log_subscriber.rb
130
+ - lib/tire/rails-logger/railtie.rb
131
+ - test/rails-logger/active_record_article.rb
132
+ - test/rails-logger/log_subscriber_test.rb
133
+ - test/test_helper.rb
134
+ - tire-contrib.gemspec
135
+ has_rdoc: true
136
+ homepage: ""
137
+ licenses: []
138
+
139
+ post_install_message:
140
+ rdoc_options:
141
+ - --charset=UTF-8
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ segments:
149
+ - 0
150
+ version: "0"
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ segments:
156
+ - 0
157
+ version: "0"
158
+ requirements: []
159
+
160
+ rubyforge_project: tire-contrib
161
+ rubygems_version: 1.3.6
162
+ signing_key:
163
+ specification_version: 3
164
+ summary: Contributions and additions for the Tire gem
165
+ test_files:
166
+ - test/rails-logger/active_record_article.rb
167
+ - test/rails-logger/log_subscriber_test.rb
168
+ - test/test_helper.rb