gummi 0.3.7 → 0.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/gummi.rb CHANGED
@@ -12,6 +12,7 @@ require 'repobahn/entity'
12
12
  require 'gummi/version'
13
13
  require 'gummi/errors'
14
14
  require 'gummi/api'
15
+ require 'gummi/configuration'
15
16
 
16
17
  require 'gummi/db_layer/index'
17
18
  require 'gummi/db_layer/document/attributes'
@@ -39,8 +40,8 @@ require 'gummi/entity_layer/entity'
39
40
 
40
41
  module Gummi
41
42
  def self.env
42
- if defined? Rails
43
- Rails.env
43
+ if defined? ::Rails
44
+ ::Rails.env
44
45
  else
45
46
  RAILS_ENV || 'development'
46
47
  end
@@ -0,0 +1,38 @@
1
+ require 'logger'
2
+
3
+ module Gummi
4
+ class Configuration
5
+
6
+ attr_accessor :logger
7
+
8
+ def initialize(options={})
9
+ @logger = options[:logger] || default_logger
10
+ end
11
+
12
+ private
13
+
14
+ def default_logger
15
+ if defined?(Rails)
16
+ Rails.logger
17
+ else
18
+ Logger.new(STDOUT)
19
+ end
20
+ end
21
+ end
22
+
23
+ def self.config
24
+ @config ||= Configuration.new
25
+ end
26
+
27
+ # Public: Yields the configuration instance.
28
+ #
29
+ def self.configure(&block)
30
+ yield config
31
+ end
32
+
33
+ # Public: Reset the configuration (useful for testing).
34
+ #
35
+ def self.reset!
36
+ @config = nil
37
+ end
38
+ end
@@ -4,8 +4,8 @@ module Gummi
4
4
  include Gummi::DbLayer::Index
5
5
 
6
6
  def self.name
7
- if defined? Rails
8
- "#{Rails.application.class.name.deconstantize.underscore}_#{Rails.env}"
7
+ if defined? ::Rails
8
+ "#{::Rails.application.class.name.deconstantize.underscore}_#{::Rails.env}"
9
9
  else
10
10
  "gummi_#{Gummi.env}"
11
11
  end
@@ -152,14 +152,16 @@ module Gummi
152
152
  # ––––––––––––––––––––––––
153
153
 
154
154
  def get!(id, parent_id = nil)
155
- options = { index: index.name, type: document_type, id: id, fields: %w{ _source _parent } }
156
- if parent_id
157
- options.merge! parent: parent_id
158
- elsif parent_document_type
159
- raise ArgumentError, "The parent_id attribute is required for getting #{name} from Elastic Search"
160
- end
161
- response = Hashie::Mash.new client.get options
162
- hit_to_document response
155
+ options = { index: index.name, type: document_type, id: id, fields: %w{ _source _parent } }
156
+ if parent_id
157
+ options.merge! parent: parent_id
158
+ elsif parent_document_type
159
+ raise ArgumentError, "The parent_id attribute is required for getting #{name} from Elastic Search"
160
+ end
161
+ response = ActiveSupport::Notifications.instrument "search.elasticsearch", name: "Document#get!", search: options do
162
+ Hashie::Mash.new client.get options
163
+ end
164
+ hit_to_document response
163
165
  end
164
166
 
165
167
  def delete!(id, parent_id = nil)
@@ -169,7 +171,9 @@ module Gummi
169
171
  elsif parent_document_type
170
172
  raise ArgumentError, "The parent_id attribute is required for getting #{name} from Elastic Search"
171
173
  end
172
- response = Hashie::Mash.new client.delete options
174
+ response = ActiveSupport::Notifications.instrument "search.elasticsearch", name: "Document#delete!", search: options do
175
+ Hashie::Mash.new client.delete options
176
+ end
173
177
  response.ok && response.found
174
178
  end
175
179
 
@@ -194,7 +198,10 @@ module Gummi
194
198
  options = {}
195
199
  end
196
200
  raise ArgumentError unless [:create, :index].include?(method)
197
- response = Hashie::Mash.new client.send(method, options.merge(index: index.name, type: document_type, id: self.id, body: attributes))
201
+ opts = options.merge(index: index.name, type: document_type, id: self.id, body: attributes)
202
+ response = ActiveSupport::Notifications.instrument "search.elasticsearch", name: "Document#create(#{method})", search: opts do
203
+ Hashie::Mash.new client.send(method, opts)
204
+ end
198
205
  if response.ok
199
206
  self.id = response._id
200
207
  self.version = response._version
@@ -9,7 +9,7 @@ module Gummi
9
9
  def initialize(response, converter, per_page, page)
10
10
  @success = !!response
11
11
  @response = Hashie::Mash.new response
12
- @took = @response.hits.took if @response.hits
12
+ @took = @response.took
13
13
  @total = @response.hits.total if @response.hits
14
14
  @hits = @response.hits.hits if @response.hits
15
15
  @facets = @response.facets || Hashie::Mash.new
@@ -28,7 +28,9 @@ module Gummi
28
28
  end
29
29
 
30
30
  def execute
31
- Gummi::DbLayer::Document::Search::Result.new client.search(to_client_args), document_class, per_page, page
31
+ ActiveSupport::Notifications.instrument "search.elasticsearch", name: "#{self.class.name}#execute", search: to_client_args do
32
+ Gummi::DbLayer::Document::Search::Result.new client.search(to_client_args), document_class, per_page, page
33
+ end
32
34
  rescue Faraday::Error::ConnectionFailed
33
35
  Gummi::DbLayer::Document::Search::Result.new nil, document_class, per_page, page
34
36
  end
@@ -51,4 +53,4 @@ module Gummi
51
53
  end
52
54
  end
53
55
  end
54
- end
56
+ end
@@ -21,7 +21,10 @@ module Gummi
21
21
  #
22
22
  def teardown
23
23
  raise NotImplementedError if Gummi.env == 'production'
24
- response = client.indices.delete index: name
24
+
25
+ response = ActiveSupport::Notifications.instrument "search.elasticsearch", name: "Index(#{self.name}).teardown", search: {} do
26
+ client.indices.delete index: name
27
+ end
25
28
  response.present?
26
29
  rescue ::Elasticsearch::Transport::Transport::Errors::NotFound
27
30
  true
@@ -32,8 +35,10 @@ module Gummi
32
35
  end
33
36
 
34
37
  def refresh
35
- client.indices.refresh
36
- client.cluster.health wait_for_status: :yellow
38
+ ActiveSupport::Notifications.instrument "search.elasticsearch", name: "Index(#{self.name}).refresh", search: {} do
39
+ client.indices.refresh
40
+ client.cluster.health wait_for_status: :yellow
41
+ end
37
42
  end
38
43
 
39
44
  def settings
@@ -0,0 +1,5 @@
1
+ module Gummi
2
+ module Rails
3
+
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ module Gummi
2
+ module Rails
3
+ module Instrumentation
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,41 @@
1
+ require 'active_support/core_ext/module/attr_internal'
2
+
3
+ module Gummi
4
+ module Rails
5
+ module Instrumentation
6
+
7
+ # Hooks into ActionController to display Elasticsearch runtime
8
+ #
9
+ # @see https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/controller_runtime.rb
10
+ #
11
+ module ControllerRuntime
12
+ extend ActiveSupport::Concern
13
+
14
+ protected
15
+
16
+ attr_internal :elasticsearch_runtime
17
+
18
+ def cleanup_view_runtime
19
+ elasticsearch_rt_before_render = Gummi::Rails::Instrumentation::LogSubscriber.reset_runtime
20
+ runtime = super
21
+ elasticsearch_rt_after_render = Gummi::Rails::Instrumentation::LogSubscriber.reset_runtime
22
+ self.elasticsearch_runtime = elasticsearch_rt_before_render + elasticsearch_rt_after_render
23
+ runtime - elasticsearch_rt_after_render
24
+ end
25
+
26
+ def append_info_to_payload(payload)
27
+ super
28
+ payload[:elasticsearch_runtime] = (elasticsearch_runtime || 0) + Gummi::Rails::Instrumentation::LogSubscriber.reset_runtime
29
+ end
30
+
31
+ module ClassMethods
32
+ def log_process_action(payload)
33
+ messages, elasticsearch_runtime = super, payload[:elasticsearch_runtime]
34
+ messages << ("Elasticsearch: %.1fms" % elasticsearch_runtime.to_f) if elasticsearch_runtime
35
+ messages
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,43 @@
1
+ module Gummi
2
+ module Rails
3
+ module Instrumentation
4
+
5
+ # A log subscriber to attach to Elasticsearch related events
6
+ #
7
+ # @see https://github.com/rails/rails/blob/master/activerecord/lib/active_record/log_subscriber.rb
8
+ #
9
+ class LogSubscriber < ActiveSupport::LogSubscriber
10
+ def self.runtime=(value)
11
+ Thread.current["elasticsearch_runtime"] = value
12
+ end
13
+
14
+ def self.runtime
15
+ Thread.current["elasticsearch_runtime"] ||= 0
16
+ end
17
+
18
+ def self.reset_runtime
19
+ rt, self.runtime = runtime, 0
20
+ rt
21
+ end
22
+
23
+ # Intercept `search.elasticsearch` events, and display them in the Rails log
24
+ #
25
+ def search(event)
26
+ self.class.runtime += event.duration
27
+ return unless logger.debug?
28
+ payload = event.payload
29
+ name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
30
+ search = payload[:search].inspect.gsub(/:(\w+)=>/, '\1: ')
31
+ logger.debug "#{color(name, GREEN, true)} #{search}"
32
+ end
33
+
34
+ def logger
35
+ Gummi.config.logger
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+
43
+ Gummi::Rails::Instrumentation::LogSubscriber.attach_to :elasticsearch
@@ -0,0 +1,23 @@
1
+ module Gummi
2
+ module Rails
3
+ module Instrumentation
4
+
5
+ # Rails initializer class to require Elasticsearch::Rails::Instrumentation files,
6
+ # set up Elasticsearch::Model and hook into ActionController to display Elasticsearch-related duration
7
+ #
8
+ # @see http://edgeguides.rubyonrails.org/active_support_instrumentation.html
9
+ #
10
+ class Railtie < ::Rails::Railtie
11
+ initializer "gummi.instrumentation" do |app|
12
+ require 'gummi/rails/instrumentation/log_subscriber'
13
+ require 'gummi/rails/instrumentation/controller_runtime'
14
+
15
+ ActiveSupport.on_load(:action_controller) do
16
+ include Gummi::Rails::Instrumentation::ControllerRuntime
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
data/lib/gummi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Gummi
2
- VERSION = '0.3.7'
2
+ VERSION = '0.3.8'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gummi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.3.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-13 00:00:00.000000000 Z
12
+ date: 2014-02-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: virtus
@@ -206,6 +206,7 @@ files:
206
206
  - gummi.gemspec
207
207
  - lib/gummi.rb
208
208
  - lib/gummi/api.rb
209
+ - lib/gummi/configuration.rb
209
210
  - lib/gummi/db_layer/default_index.rb
210
211
  - lib/gummi/db_layer/document.rb
211
212
  - lib/gummi/db_layer/document/attributes.rb
@@ -214,7 +215,6 @@ files:
214
215
  - lib/gummi/db_layer/document/search/raw.rb
215
216
  - lib/gummi/db_layer/document/search/result.rb
216
217
  - lib/gummi/db_layer/document/search/searching.rb
217
- - lib/gummi/db_layer/favorstuff.rb
218
218
  - lib/gummi/db_layer/fields/boolean.rb
219
219
  - lib/gummi/db_layer/fields/integer.rb
220
220
  - lib/gummi/db_layer/fields/keyword.rb
@@ -227,6 +227,11 @@ files:
227
227
  - lib/gummi/db_layer/index.rb
228
228
  - lib/gummi/entity_layer/entity.rb
229
229
  - lib/gummi/errors.rb
230
+ - lib/gummi/rails.rb
231
+ - lib/gummi/rails/instrumentation.rb
232
+ - lib/gummi/rails/instrumentation/controller_runtime.rb
233
+ - lib/gummi/rails/instrumentation/log_subscriber.rb
234
+ - lib/gummi/rails/instrumentation/railtie.rb
230
235
  - lib/gummi/repository_layer/repository.rb
231
236
  - lib/gummi/repository_layer/repository/result.rb
232
237
  - lib/gummi/version.rb
@@ -269,12 +274,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
269
274
  - - ! '>='
270
275
  - !ruby/object:Gem::Version
271
276
  version: '0'
277
+ segments:
278
+ - 0
279
+ hash: 37788337669997294
272
280
  required_rubygems_version: !ruby/object:Gem::Requirement
273
281
  none: false
274
282
  requirements:
275
283
  - - ! '>='
276
284
  - !ruby/object:Gem::Version
277
285
  version: '0'
286
+ segments:
287
+ - 0
288
+ hash: 37788337669997294
278
289
  requirements: []
279
290
  rubyforge_project:
280
291
  rubygems_version: 1.8.23
@@ -1,15 +0,0 @@
1
- def self.favored_by(user_id, lot_ids = nil, search_options = {})
2
- seek = Seek.new search_options, valid_sort_bys: %w{ catalogue_number }, max_per_page: 50, default_sort_by: :catalogue_number, default_sort_direction: :asc
3
- favor_query = { filtered: { query: { match_all: {} }, filter: { term: { user_id: user_id.to_i } } } }
4
- favor_filter = { has_child: { type: DB::Favoring.document_type, query: favor_query } }
5
- if lot_ids
6
- lot_ids = lot_ids.presence || [-1]
7
- lot_query = { ids: { values: lot_ids } }
8
- query = { filtered: { query: lot_query, filter: favor_filter }}
9
- body = { query: query }
10
- else
11
- body = { query: favor_filter }
12
- end
13
- body.merge! sort: { seek.sort_by => seek.sort_direction }
14
- raw_search seek.to_hash.merge options: { body: body }
15
- end