mini-smart-box 0.0.1

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.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/elasticsearch-rails-8.0.1/CHANGELOG.md +44 -0
  3. data/elasticsearch-rails-8.0.1/Gemfile +38 -0
  4. data/elasticsearch-rails-8.0.1/LICENSE.txt +202 -0
  5. data/elasticsearch-rails-8.0.1/README.md +149 -0
  6. data/elasticsearch-rails-8.0.1/Rakefile +67 -0
  7. data/elasticsearch-rails-8.0.1/elasticsearch-rails.gemspec +67 -0
  8. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/controller_runtime.rb +58 -0
  9. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/log_subscriber.rb +67 -0
  10. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/publishers.rb +53 -0
  11. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation/railtie.rb +44 -0
  12. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/instrumentation.rb +53 -0
  13. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/lograge.rb +57 -0
  14. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/tasks/import.rb +128 -0
  15. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails/version.rb +22 -0
  16. data/elasticsearch-rails-8.0.1/lib/elasticsearch/rails.rb +24 -0
  17. data/elasticsearch-rails-8.0.1/lib/rails/templates/01-basic.rb +364 -0
  18. data/elasticsearch-rails-8.0.1/lib/rails/templates/02-pretty.rb +348 -0
  19. data/elasticsearch-rails-8.0.1/lib/rails/templates/03-expert.rb +358 -0
  20. data/elasticsearch-rails-8.0.1/lib/rails/templates/04-dsl.rb +146 -0
  21. data/elasticsearch-rails-8.0.1/lib/rails/templates/05-settings-files.rb +88 -0
  22. data/elasticsearch-rails-8.0.1/lib/rails/templates/articles.yml.gz +0 -0
  23. data/elasticsearch-rails-8.0.1/lib/rails/templates/articles_settings.json +1 -0
  24. data/elasticsearch-rails-8.0.1/lib/rails/templates/index.html.dsl.erb +178 -0
  25. data/elasticsearch-rails-8.0.1/lib/rails/templates/index.html.erb +178 -0
  26. data/elasticsearch-rails-8.0.1/lib/rails/templates/indexer.rb +44 -0
  27. data/elasticsearch-rails-8.0.1/lib/rails/templates/search.css +76 -0
  28. data/elasticsearch-rails-8.0.1/lib/rails/templates/search_controller_test.dsl.rb +148 -0
  29. data/elasticsearch-rails-8.0.1/lib/rails/templates/search_controller_test.rb +148 -0
  30. data/elasticsearch-rails-8.0.1/lib/rails/templates/searchable.dsl.rb +234 -0
  31. data/elasticsearch-rails-8.0.1/lib/rails/templates/searchable.rb +224 -0
  32. data/elasticsearch-rails-8.0.1/lib/rails/templates/seeds.rb +75 -0
  33. data/elasticsearch-rails-8.0.1/spec/instrumentation/log_subscriber_spec.rb +57 -0
  34. data/elasticsearch-rails-8.0.1/spec/instrumentation_spec.rb +103 -0
  35. data/elasticsearch-rails-8.0.1/spec/lograge_spec.rb +52 -0
  36. data/elasticsearch-rails-8.0.1/spec/spec_helper.rb +65 -0
  37. data/mini-smart-box.gemspec +11 -0
  38. metadata +76 -0
@@ -0,0 +1,67 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ module Elasticsearch
19
+ module Rails
20
+ module Instrumentation
21
+ # A log subscriber to attach to Elasticsearch related events
22
+ #
23
+ # @see https://github.com/rails/rails/blob/master/activerecord/lib/active_record/log_subscriber.rb
24
+ #
25
+ class LogSubscriber < ActiveSupport::LogSubscriber
26
+ def self.runtime=(value)
27
+ Thread.current["elasticsearch_runtime"] = value
28
+ end
29
+
30
+ def self.runtime
31
+ Thread.current["elasticsearch_runtime"] ||= 0
32
+ end
33
+
34
+ def self.reset_runtime
35
+ rt, self.runtime = runtime, 0
36
+ rt
37
+ end
38
+
39
+ # Intercept `search.elasticsearch` events, and display them in the Rails log
40
+ #
41
+ def search(event)
42
+ self.class.runtime += event.duration
43
+ return unless logger.debug?
44
+
45
+ payload = event.payload
46
+ name = "#{payload[:klass]} #{payload[:name]} (#{event.duration.round(1)}ms)"
47
+ search = payload[:search].inspect.gsub(/:(\w+)=>/, '\1: ')
48
+ debug %Q| #{color(name, GREEN, color_option(true))} #{colorize_logging ? "\e[2m#{search}\e[0m" : search}|
49
+ end
50
+
51
+ private
52
+
53
+ def color_option(bold_value)
54
+ new_color_syntax? ? { bold: bold_value } : bold_value
55
+ end
56
+
57
+ def new_color_syntax?
58
+ return @new_color_syntax if defined?(@new_color_syntax)
59
+
60
+ @new_color_syntax = ::ActiveSupport.respond_to?(:gem_version) && ::ActiveSupport::gem_version >= '7.1'
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ Elasticsearch::Rails::Instrumentation::LogSubscriber.attach_to :elasticsearch
@@ -0,0 +1,53 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ module Elasticsearch
19
+ module Rails
20
+ module Instrumentation
21
+ module Publishers
22
+
23
+ # Wraps the `SearchRequest` methods to perform the instrumentation
24
+ #
25
+ # @see SearchRequest#execute_with_instrumentation!
26
+ # @see http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html
27
+ #
28
+ module SearchRequest
29
+
30
+ def self.included(base)
31
+ base.class_eval do
32
+ unless method_defined?(:execute_without_instrumentation!)
33
+ alias_method :execute_without_instrumentation!, :execute!
34
+ alias_method :execute!, :execute_with_instrumentation!
35
+ end
36
+ end
37
+ end
38
+
39
+ # Wrap `Search#execute!` and perform instrumentation
40
+ #
41
+ def execute_with_instrumentation!
42
+ ActiveSupport::Notifications.instrument "search.elasticsearch",
43
+ name: 'Search',
44
+ klass: (self.klass.is_a?(Elasticsearch::Model::Proxy::ClassMethodsProxy) ? self.klass.target.to_s : self.klass.to_s),
45
+ search: self.definition do
46
+ execute_without_instrumentation!
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,44 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ module Elasticsearch
19
+ module Rails
20
+ module Instrumentation
21
+
22
+ # Rails initializer class to require Elasticsearch::Rails::Instrumentation files,
23
+ # set up Elasticsearch::Model and hook into ActionController to display Elasticsearch-related duration
24
+ #
25
+ # @see http://edgeguides.rubyonrails.org/active_support_instrumentation.html
26
+ #
27
+ class Railtie < ::Rails::Railtie
28
+ initializer "elasticsearch.instrumentation" do |app|
29
+ require 'elasticsearch/rails/instrumentation/log_subscriber'
30
+ require 'elasticsearch/rails/instrumentation/controller_runtime'
31
+
32
+ Elasticsearch::Model::Searching::SearchRequest.class_eval do
33
+ include Elasticsearch::Rails::Instrumentation::Publishers::SearchRequest
34
+ end if defined?(Elasticsearch::Model::Searching::SearchRequest)
35
+
36
+ ActiveSupport.on_load(:action_controller) do
37
+ include Elasticsearch::Rails::Instrumentation::ControllerRuntime
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,53 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ require 'elasticsearch/rails/instrumentation/railtie'
19
+ require 'elasticsearch/rails/instrumentation/publishers'
20
+
21
+ module Elasticsearch
22
+ module Rails
23
+
24
+ # This module adds support for displaying statistics about search duration in the Rails application log
25
+ # by integrating with the `ActiveSupport::Notifications` framework and `ActionController` logger.
26
+ #
27
+ # == Usage
28
+ #
29
+ # Require the component in your `application.rb` file:
30
+ #
31
+ # require 'elasticsearch/rails/instrumentation'
32
+ #
33
+ # You should see an output like this in your application log in development environment:
34
+ #
35
+ # Article Search (321.3ms) { index: "articles", type: "article", body: { query: ... } }
36
+ #
37
+ # Also, the total duration of the request to Elasticsearch is displayed in the Rails request breakdown:
38
+ #
39
+ # Completed 200 OK in 615ms (Views: 230.9ms | ActiveRecord: 0.0ms | Elasticsearch: 321.3ms)
40
+ #
41
+ # @note The displayed duration includes the HTTP transfer -- the time it took Elasticsearch
42
+ # to process your request is available in the `response.took` property.
43
+ #
44
+ # @see Elasticsearch::Rails::Instrumentation::Publishers
45
+ # @see Elasticsearch::Rails::Instrumentation::Railtie
46
+ #
47
+ # @see http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html
48
+ #
49
+ #
50
+ module Instrumentation
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,57 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ module Elasticsearch
19
+ module Rails
20
+ module Lograge
21
+
22
+ # Rails initializer class to require Elasticsearch::Rails::Instrumentation files,
23
+ # set up Elasticsearch::Model and add Lograge configuration to display Elasticsearch-related duration
24
+ #
25
+ # Require the component in your `application.rb` file and enable Lograge:
26
+ #
27
+ # require 'elasticsearch/rails/lograge'
28
+ #
29
+ # You should see the full duration of the request to Elasticsearch as part of each log event:
30
+ #
31
+ # method=GET path=/search ... status=200 duration=380.89 view=99.64 db=0.00 es=279.37
32
+ #
33
+ # @see https://github.com/roidrage/lograge
34
+ #
35
+ class Railtie < ::Rails::Railtie
36
+ initializer "elasticsearch.lograge" do |app|
37
+ require 'elasticsearch/rails/instrumentation/publishers'
38
+ require 'elasticsearch/rails/instrumentation/log_subscriber'
39
+ require 'elasticsearch/rails/instrumentation/controller_runtime'
40
+
41
+ Elasticsearch::Model::Searching::SearchRequest.class_eval do
42
+ include Elasticsearch::Rails::Instrumentation::Publishers::SearchRequest
43
+ end if defined?(Elasticsearch::Model::Searching::SearchRequest)
44
+
45
+ ActiveSupport.on_load(:action_controller) do
46
+ include Elasticsearch::Rails::Instrumentation::ControllerRuntime
47
+ end
48
+
49
+ config.lograge.custom_options = lambda do |event|
50
+ { es: event.payload[:elasticsearch_runtime].to_f.round(2) }
51
+ end
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,128 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # A collection of Rake tasks to facilitate importing data from your models into Elasticsearch.
19
+ #
20
+ # Add this e.g. into the `lib/tasks/elasticsearch.rake` file in your Rails application:
21
+ #
22
+ # require 'elasticsearch/rails/tasks/import'
23
+ #
24
+ # To import the records from your `Article` model, run:
25
+ #
26
+ # $ bundle exec rake environment elasticsearch:import:model CLASS='MyModel'
27
+ #
28
+ # Run this command to display usage instructions:
29
+ #
30
+ # $ bundle exec rake -D elasticsearch
31
+ #
32
+ STDOUT.sync = true
33
+ STDERR.sync = true
34
+
35
+ begin; require 'ansi/progressbar'; rescue LoadError; end
36
+
37
+ namespace :elasticsearch do
38
+
39
+ task :import => 'import:model'
40
+
41
+ namespace :import do
42
+ import_model_desc = <<-DESC.gsub(/ /, '')
43
+ Import data from your model (pass name as CLASS environment variable).
44
+
45
+ $ rake environment elasticsearch:import:model CLASS='MyModel'
46
+
47
+ Force rebuilding the index (delete and create):
48
+ $ rake environment elasticsearch:import:model CLASS='Article' FORCE=y
49
+
50
+ Customize the batch size:
51
+ $ rake environment elasticsearch:import:model CLASS='Article' BATCH=100
52
+
53
+ Set target index name:
54
+ $ rake environment elasticsearch:import:model CLASS='Article' INDEX='articles-new'
55
+
56
+ Pass an ActiveRecord scope to limit the imported records:
57
+ $ rake environment elasticsearch:import:model CLASS='Article' SCOPE='published'
58
+ DESC
59
+ desc import_model_desc
60
+ task model: :environment do
61
+ if ENV['CLASS'].to_s == ''
62
+ puts '='*90, 'USAGE', '='*90, import_model_desc, ""
63
+ exit(1)
64
+ end
65
+
66
+ klass = eval(ENV['CLASS'].to_s)
67
+ total = klass.count rescue nil
68
+ pbar = ANSI::Progressbar.new(klass.to_s, total) rescue nil
69
+ pbar.__send__ :show if pbar
70
+
71
+ unless ENV['DEBUG']
72
+ begin
73
+ klass.__elasticsearch__.client.transport.logger.level = Logger::WARN
74
+ rescue NoMethodError; end
75
+ begin
76
+ klass.__elasticsearch__.client.transport.tracer.level = Logger::WARN
77
+ rescue NoMethodError; end
78
+ end
79
+
80
+ total_errors = klass.__elasticsearch__.import force: ENV.fetch('FORCE', false),
81
+ batch_size: ENV.fetch('BATCH', 1000).to_i,
82
+ index: ENV.fetch('INDEX', nil),
83
+ scope: ENV.fetch('SCOPE', nil) do |response|
84
+ pbar.inc response['items'].size if pbar
85
+ STDERR.flush
86
+ STDOUT.flush
87
+ end
88
+ pbar.finish if pbar
89
+
90
+ puts "[IMPORT] #{total_errors} errors occurred" unless total_errors.zero?
91
+ puts '[IMPORT] Done'
92
+ end
93
+
94
+ desc <<-DESC.gsub(/ /, '')
95
+ Import all indices from `app/models` (or use DIR environment variable).
96
+
97
+ $ rake environment elasticsearch:import:all DIR=app/models
98
+ DESC
99
+ task all: :environment do
100
+ dir = ENV['DIR'].to_s != '' ? ENV['DIR'] : Rails.root.join("app/models")
101
+
102
+ puts "[IMPORT] Loading models from: #{dir}"
103
+ Dir.glob(File.join("#{dir}/**/*.rb")).each do |path|
104
+ model_filename = path[/#{Regexp.escape(dir.to_s)}\/([^\.]+).rb/, 1]
105
+
106
+ next if model_filename.match(/^concerns\//i) # Skip concerns/ folder
107
+
108
+ begin
109
+ klass = model_filename.camelize.constantize
110
+ rescue NameError
111
+ require(path) ? retry : raise(RuntimeError, "Cannot load class '#{klass}'")
112
+ end
113
+
114
+ # Skip if the class doesn't have Elasticsearch integration
115
+ next unless klass.respond_to?(:__elasticsearch__)
116
+
117
+ puts "[IMPORT] Processing model: #{klass}..."
118
+
119
+ ENV['CLASS'] = klass.to_s
120
+ Rake::Task["elasticsearch:import:model"].invoke
121
+ Rake::Task["elasticsearch:import:model"].reenable
122
+ puts
123
+ end
124
+ end
125
+
126
+ end
127
+
128
+ end
@@ -0,0 +1,22 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ module Elasticsearch
19
+ module Rails
20
+ VERSION = '8.0.1'.freeze
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ require "elasticsearch/rails/version"
19
+
20
+ module Elasticsearch
21
+ module Rails
22
+ # Your code goes here...
23
+ end
24
+ end