Module: Rollwire::WriteCounter
- Defined in:
- lib/rollwire/write_counter.rb
Defined Under Namespace
Classes: Frame
Constant Summary collapse
- IGNORED_EVENT_NAMES =
%w[SCHEMA TRANSACTION].freeze
- MUTATION_SQL =
/\A(?:INSERT|UPDATE|DELETE)\b/i- LOCKING_READ_SQL =
/\ASELECT\b.*\bFOR\s+UPDATE\b/im- LEADING_COMMENTS =
%r{\A(?:\s|/\*.*?\*/|--[^\n]*(?:\n|\z))*}m
Class Method Summary collapse
- .current_writes(connection) ⇒ Object
- .frame_count ⇒ Object
- .pop_frame(connection) ⇒ Object
- .push_frame(connection) ⇒ Object
- .record(payload) ⇒ Object
- .reset! ⇒ Object
- .start! ⇒ Object
Class Method Details
.current_writes(connection) ⇒ Object
45 46 47 48 |
# File 'lib/rollwire/write_counter.rb', line 45 def current_writes(connection) frame = current_frames&.reverse_each&.find { |candidate| candidate.connection.equal?(connection) } frame&.writes || 0 end |
.frame_count ⇒ Object
64 65 66 |
# File 'lib/rollwire/write_counter.rb', line 64 def frame_count current_frames&.length || 0 end |
.pop_frame(connection) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/rollwire/write_counter.rb', line 35 def pop_frame(connection) frames = current_frames return unless frames index = frames.rindex { |frame| frame.connection.equal?(connection) } frame = frames.delete_at(index) if index ExecutionState.delete(:write_counter_frames) if frames.empty? frame end |
.push_frame(connection) ⇒ Object
31 32 33 |
# File 'lib/rollwire/write_counter.rb', line 31 def push_frame(connection) current_frames! << Frame.new(connection: connection, writes: 0) end |
.record(payload) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/rollwire/write_counter.rb', line 50 def record(payload) return unless Rollwire.configuration.count_writes return unless current_frames&.any? return unless write_event?(payload) current_frames.each do |frame| frame.writes += 1 if frame.connection.equal?(payload[:connection]) end end |
.reset! ⇒ Object
60 61 62 |
# File 'lib/rollwire/write_counter.rb', line 60 def reset! ExecutionState.delete(:write_counter_frames) end |
.start! ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rollwire/write_counter.rb', line 16 def start! return if @subscription @subscription_mutex ||= Mutex.new @subscription_mutex.synchronize do return if @subscription @subscription = ActiveSupport::Notifications.subscribe("sql.active_record") do |*arguments| record(arguments.last) rescue StandardError => e Rollwire.warn_internal_error(e) end end end |