rails_spotlight 0.1.5 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87f0223a0d6c7cac9d284d7d19a7befa0280c52f3731b01cef1207b83628875d
4
- data.tar.gz: 0e5adc3f69234b898dce003bb9c2f82be164bab0afbd5a7b5c826d30cb013b49
3
+ metadata.gz: 507c2c676db5d30d6af1ec0f505e067f16c53514dfd875cd4554041304d95b87
4
+ data.tar.gz: 8c990b6ec150e2a6c9e7b8e0c581d5b1e74987e5502907e847218a78d6324103
5
5
  SHA512:
6
- metadata.gz: fa5134d60d3c1ca1d7b34c30adccd3316f3aab59f445e35d3e547cc6c735ea05056769a4dc5a5d5344b56ffdbdb01969cf0d94286346c5735b450222fc76a68d
7
- data.tar.gz: cc448d4957b17c63e7ad7081df7e988c64548e3a619dc834a7956c32def2132b3c02e8af7e0e1aa603e7b4c0068c942f61118054158a5d872c815205ddb409d0
6
+ metadata.gz: c36b1e95facd5d437c4685a7ba66a02e45eaf152d8e4fc607468fffef5fad8d7fb9c15ed1b760feaa1501b1b2c8a27bd61166917083da90ccb5cf64529530a6c
7
+ data.tar.gz: d1439a47bedd079561dcecb4a8f1f8cde964eea6555da2dc0e16e330bac2b89abc9e0f90a2a499bf50c1939e1b1e64a0622209a14affa1d951abb76ad4c9732a
data/README.md CHANGED
@@ -17,6 +17,14 @@ group :development do
17
17
  end
18
18
  ```
19
19
 
20
+ ## Configuration
21
+
22
+ environment variables
23
+
24
+ ```
25
+ RAILS_SPOTLIGHT_PROJECT=MyProjectName
26
+ ```
27
+
20
28
  ## Testing
21
29
 
22
30
  To run tests for all versions of Rails and Ruby, run:
@@ -40,3 +48,6 @@ The gem is available as open source under the terms of the [MIT License](https:/
40
48
  ## Code of Conduct
41
49
 
42
50
  Everyone interacting in the RailsSpotlight project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/pniemczyk/rails_spotlight/blob/master/CODE_OF_CONDUCT.md).
51
+
52
+
53
+ check https://github.com/alexrudall/ruby-openai
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative '../../support/project'
4
+
3
5
  module RailsSpotlight
4
6
  module Middlewares
5
7
  module Handlers
@@ -19,7 +21,10 @@ module RailsSpotlight
19
21
  end
20
22
 
21
23
  def json_response_body
22
- { source: text_response_body }
24
+ {
25
+ source: text_response_body,
26
+ project: ::RailsSpotlight::Support::Project.instance.name
27
+ }
23
28
  end
24
29
 
25
30
  def write_mode?
@@ -1,30 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative '../../support/project'
4
+
3
5
  module RailsSpotlight
4
6
  module Middlewares
5
7
  module Handlers
6
8
  class SqlActionHandler < BaseActionHandler
7
9
  def execute
8
- if ActiveSupport.const_defined?('ExecutionContext')
9
- ActiveSupport::Notifications.subscribed(method(:logger), 'sql.active_record', monotonic: true) do
10
- ActiveSupport::ExecutionContext.set(rails_spotlight: request_id) do
11
- transaction
12
- end
13
- end
14
- else
15
- ActiveSupport::Notifications.subscribed(method(:logger), 'sql.active_record') do
16
- transaction
17
- end
10
+ validate_project!
11
+ return transaction unless ActiveSupport.const_defined?('ExecutionContext')
12
+
13
+ ActiveSupport::ExecutionContext.set(rails_spotlight: request_id) do
14
+ transaction
18
15
  end
19
16
  end
20
17
 
21
18
  private
22
19
 
20
+ def validate_project!
21
+ Rails.logger.warn required_projects
22
+ return if required_projects.blank?
23
+ return if required_projects.include?(::RailsSpotlight::Support::Project.instance.name)
24
+
25
+ raise UnprocessableEntity, "Check your connetction settings the current query is not allowed to be executed on the #{::RailsSpotlight::Support::Project.instance.name} project"
26
+ end
27
+
23
28
  def transaction
24
29
  ActiveRecord::Base.transaction do
25
- begin
26
- self.result = ActiveRecord::Base.connection.exec_query(query)
27
- rescue => e
30
+ begin # rubocop:disable Style/RedundantBegin
31
+ ActiveSupport::Notifications.subscribed(method(:logger), 'sql.active_record', monotonic: true) do
32
+ run
33
+ end
34
+ rescue => e # rubocop:disable Style/RescueStandardError
28
35
  self.error = e
29
36
  ensure
30
37
  raise ActiveRecord::Rollback unless force_execution?
@@ -32,11 +39,28 @@ module RailsSpotlight
32
39
  end
33
40
  end
34
41
 
35
- attr_accessor :result
36
- attr_accessor :error
42
+ def run
43
+ return self.result = ActiveRecord::Base.connection.exec_query(query) if connection_options.blank? || !ActiveRecord::Base.respond_to?(:connects_to)
44
+
45
+ connections = ActiveRecord::Base.connects_to(**connection_options)
46
+
47
+ adapter = connections.find { |c| c.role == use['role'] && c.shard.to_s == use['shard'] }
48
+ raise UnprocessableEntity, "Connection not found for role: `#{use["role"]}` and shard: `#{use["shard"]}`" if adapter.blank?
49
+
50
+ self.result = adapter.connection.exec_query(query)
51
+ end
52
+
53
+ attr_accessor :result, :error
37
54
 
38
55
  def json_response_body
39
- { result: result, logs: logs, error: error.inspect, query_mode: force_execution? ? 'force' : 'default' }
56
+ {
57
+ query: query,
58
+ result: result,
59
+ logs: logs,
60
+ error: error.present? ? error.inspect : nil,
61
+ query_mode: force_execution? ? 'force' : 'default',
62
+ project: ::RailsSpotlight::Support::Project.instance.name
63
+ }
40
64
  end
41
65
 
42
66
  def logger(_, started, finished, unique_id, payload)
@@ -53,6 +77,25 @@ module RailsSpotlight
53
77
  @query ||= json_request_body.fetch('query')
54
78
  end
55
79
 
80
+ def raw_options
81
+ @raw_options ||= json_request_body.fetch('options', {}) || {}
82
+ end
83
+
84
+ def required_projects
85
+ @required_projects ||= raw_options.fetch('projects', [])
86
+ end
87
+
88
+ def use
89
+ @use ||= { 'shard' => 'default', 'role' => 'reading' }.merge(raw_options.fetch('use', {}))
90
+ end
91
+
92
+ def connection_options
93
+ @connection_options ||= raw_options
94
+ .symbolize_keys
95
+ .slice(:database, :shards)
96
+ .reject { |_, v| v.nil? || (!v.is_a?(TrueClass) && !v.is_a?(FalseClass) && v.empty?) } # TODO: Check for each rails version
97
+ end
98
+
56
99
  def force_execution?
57
100
  @force_execution ||= json_request_body['mode'] == 'force'
58
101
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsSpotlight
4
+ module Support
5
+ class Project
6
+ include Singleton
7
+
8
+ def name
9
+ @name ||= ENV['RAILS_SPOTLIGHT_PROJECT'] || if app_class.respond_to?(:module_parent_name)
10
+ app_class.module_parent_name
11
+ else
12
+ app_class.parent_name
13
+ end
14
+ end
15
+
16
+ def app_class
17
+ @app_class ||= Rails.application.class
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsSpotlight
4
- VERSION = '0.1.5'
4
+ VERSION = '0.1.7'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_spotlight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pawel Niemczyk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-17 00:00:00.000000000 Z
11
+ date: 2023-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack-contrib
@@ -187,6 +187,7 @@ files:
187
187
  - lib/rails_spotlight/middlewares/handlers/verify_action_handler.rb
188
188
  - lib/rails_spotlight/middlewares/request_handler.rb
189
189
  - lib/rails_spotlight/railtie.rb
190
+ - lib/rails_spotlight/support/project.rb
190
191
  - lib/rails_spotlight/version.rb
191
192
  - sig/rails_spotlight.rbs
192
193
  homepage: https://github.com/pniemczyk/rails_spotlight
@@ -212,7 +213,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
213
  - !ruby/object:Gem::Version
213
214
  version: '0'
214
215
  requirements: []
215
- rubygems_version: 3.4.12
216
+ rubygems_version: 3.3.3
216
217
  signing_key:
217
218
  specification_version: 4
218
219
  summary: Lets have a look at your rails application in details