searchkick 4.6.3 → 5.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,246 +0,0 @@
1
- # based on https://gist.github.com/mnutt/566725
2
- require "active_support/core_ext/module/attr_internal"
3
-
4
- module Searchkick
5
- module QueryWithInstrumentation
6
- def execute_search
7
- name = searchkick_klass ? "#{searchkick_klass.name} Search" : "Search"
8
- event = {
9
- name: name,
10
- query: params
11
- }
12
- ActiveSupport::Notifications.instrument("search.searchkick", event) do
13
- super
14
- end
15
- end
16
- end
17
-
18
- module IndexWithInstrumentation
19
- def store(record)
20
- event = {
21
- name: "#{record.searchkick_klass.name} Store",
22
- id: search_id(record)
23
- }
24
- if Searchkick.callbacks_value == :bulk
25
- super
26
- else
27
- ActiveSupport::Notifications.instrument("request.searchkick", event) do
28
- super
29
- end
30
- end
31
- end
32
-
33
- def remove(record)
34
- name = record && record.searchkick_klass ? "#{record.searchkick_klass.name} Remove" : "Remove"
35
- event = {
36
- name: name,
37
- id: search_id(record)
38
- }
39
- if Searchkick.callbacks_value == :bulk
40
- super
41
- else
42
- ActiveSupport::Notifications.instrument("request.searchkick", event) do
43
- super
44
- end
45
- end
46
- end
47
-
48
- def update_record(record, method_name)
49
- event = {
50
- name: "#{record.searchkick_klass.name} Update",
51
- id: search_id(record)
52
- }
53
- if Searchkick.callbacks_value == :bulk
54
- super
55
- else
56
- ActiveSupport::Notifications.instrument("request.searchkick", event) do
57
- super
58
- end
59
- end
60
- end
61
-
62
- def bulk_index(records)
63
- if records.any?
64
- event = {
65
- name: "#{records.first.searchkick_klass.name} Import",
66
- count: records.size
67
- }
68
- event[:id] = search_id(records.first) if records.size == 1
69
- if Searchkick.callbacks_value == :bulk
70
- super
71
- else
72
- ActiveSupport::Notifications.instrument("request.searchkick", event) do
73
- super
74
- end
75
- end
76
- end
77
- end
78
- alias_method :import, :bulk_index
79
-
80
- def bulk_update(records, *args)
81
- if records.any?
82
- event = {
83
- name: "#{records.first.searchkick_klass.name} Update",
84
- count: records.size
85
- }
86
- event[:id] = search_id(records.first) if records.size == 1
87
- if Searchkick.callbacks_value == :bulk
88
- super
89
- else
90
- ActiveSupport::Notifications.instrument("request.searchkick", event) do
91
- super
92
- end
93
- end
94
- end
95
- end
96
-
97
- def bulk_delete(records)
98
- if records.any?
99
- event = {
100
- name: "#{records.first.searchkick_klass.name} Delete",
101
- count: records.size
102
- }
103
- event[:id] = search_id(records.first) if records.size == 1
104
- if Searchkick.callbacks_value == :bulk
105
- super
106
- else
107
- ActiveSupport::Notifications.instrument("request.searchkick", event) do
108
- super
109
- end
110
- end
111
- end
112
- end
113
- end
114
-
115
- module IndexerWithInstrumentation
116
- def perform
117
- if Searchkick.callbacks_value == :bulk
118
- event = {
119
- name: "Bulk",
120
- count: queued_items.size
121
- }
122
- ActiveSupport::Notifications.instrument("request.searchkick", event) do
123
- super
124
- end
125
- else
126
- super
127
- end
128
- end
129
- end
130
-
131
- module SearchkickWithInstrumentation
132
- def multi_search(searches)
133
- event = {
134
- name: "Multi Search",
135
- body: searches.flat_map { |q| [q.params.except(:body).to_json, q.body.to_json] }.map { |v| "#{v}\n" }.join,
136
- }
137
- ActiveSupport::Notifications.instrument("multi_search.searchkick", event) do
138
- super
139
- end
140
- end
141
- end
142
-
143
- # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/log_subscriber.rb
144
- class LogSubscriber < ActiveSupport::LogSubscriber
145
- def self.runtime=(value)
146
- Thread.current[:searchkick_runtime] = value
147
- end
148
-
149
- def self.runtime
150
- Thread.current[:searchkick_runtime] ||= 0
151
- end
152
-
153
- def self.reset_runtime
154
- rt = runtime
155
- self.runtime = 0
156
- rt
157
- end
158
-
159
- def search(event)
160
- self.class.runtime += event.duration
161
- return unless logger.debug?
162
-
163
- payload = event.payload
164
- name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
165
-
166
- index = payload[:query][:index].is_a?(Array) ? payload[:query][:index].join(",") : payload[:query][:index]
167
- type = payload[:query][:type]
168
- request_params = payload[:query].except(:index, :type, :body)
169
-
170
- params = []
171
- request_params.each do |k, v|
172
- params << "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"
173
- end
174
-
175
- debug " #{color(name, YELLOW, true)} #{index}#{type ? "/#{type.join(',')}" : ''}/_search#{params.any? ? '?' + params.join('&') : nil} #{payload[:query][:body].to_json}"
176
- end
177
-
178
- def request(event)
179
- self.class.runtime += event.duration
180
- return unless logger.debug?
181
-
182
- payload = event.payload
183
- name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
184
-
185
- debug " #{color(name, YELLOW, true)} #{payload.except(:name).to_json}"
186
- end
187
-
188
- def multi_search(event)
189
- self.class.runtime += event.duration
190
- return unless logger.debug?
191
-
192
- payload = event.payload
193
- name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
194
-
195
- debug " #{color(name, YELLOW, true)} _msearch #{payload[:body]}"
196
- end
197
- end
198
-
199
- # https://github.com/rails/rails/blob/master/activerecord/lib/active_record/railties/controller_runtime.rb
200
- module ControllerRuntime
201
- extend ActiveSupport::Concern
202
-
203
- protected
204
-
205
- attr_internal :searchkick_runtime
206
-
207
- def process_action(action, *args)
208
- # We also need to reset the runtime before each action
209
- # because of queries in middleware or in cases we are streaming
210
- # and it won't be cleaned up by the method below.
211
- Searchkick::LogSubscriber.reset_runtime
212
- super
213
- end
214
-
215
- def cleanup_view_runtime
216
- searchkick_rt_before_render = Searchkick::LogSubscriber.reset_runtime
217
- runtime = super
218
- searchkick_rt_after_render = Searchkick::LogSubscriber.reset_runtime
219
- self.searchkick_runtime = searchkick_rt_before_render + searchkick_rt_after_render
220
- runtime - searchkick_rt_after_render
221
- end
222
-
223
- def append_info_to_payload(payload)
224
- super
225
- payload[:searchkick_runtime] = (searchkick_runtime || 0) + Searchkick::LogSubscriber.reset_runtime
226
- end
227
-
228
- module ClassMethods
229
- def log_process_action(payload)
230
- messages = super
231
- runtime = payload[:searchkick_runtime]
232
- messages << ("Searchkick: %.1fms" % runtime.to_f) if runtime.to_f > 0
233
- messages
234
- end
235
- end
236
- end
237
- end
238
-
239
- Searchkick::Query.prepend(Searchkick::QueryWithInstrumentation)
240
- Searchkick::Index.prepend(Searchkick::IndexWithInstrumentation)
241
- Searchkick::Indexer.prepend(Searchkick::IndexerWithInstrumentation)
242
- Searchkick.singleton_class.prepend(Searchkick::SearchkickWithInstrumentation)
243
- Searchkick::LogSubscriber.attach_to :searchkick
244
- ActiveSupport.on_load(:action_controller) do
245
- include Searchkick::ControllerRuntime
246
- end