Class: SqlGenius::Core::Analysis::StatsCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/sql_genius/core/analysis/stats_collector.rb

Overview

Background sampler that periodically queries performance_schema for the top 50 digests by SUM_TIMER_WAIT, computes per-interval deltas, and records snapshots into a StatsHistory ring buffer.

The connection_provider is a callable (lambda/proc) that returns a Core::Connection on each invocation. This allows each adapter to supply its own connection strategy:

Rails:   -> { ActiveRecordAdapter.new(ActiveRecord::Base.connection) }
Desktop: -> { session.checkout { |c| c } }

Constant Summary collapse

DEFAULT_INTERVAL =
60
STOP_JOIN_TIMEOUT =
5
TOP_N =
50

Instance Method Summary collapse

Constructor Details

#initialize(connection_provider:, history:, interval: DEFAULT_INTERVAL) ⇒ StatsCollector

Returns a new instance of StatsCollector.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sql_genius/core/analysis/stats_collector.rb', line 21

def initialize(connection_provider:, history:, interval: DEFAULT_INTERVAL)
  @connection_provider = connection_provider
  @history = history
  @interval = interval
  @mutex = Mutex.new
  @cv = ConditionVariable.new
  @stop_signal = false
  @running = false
  @thread = nil
  @previous = {}
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/sql_genius/core/analysis/stats_collector.rb', line 51

def running?
  @running
end

#startObject



33
34
35
36
37
38
39
40
# File 'lib/sql_genius/core/analysis/stats_collector.rb', line 33

def start
  return self if @running

  @stop_signal = false
  @running = true
  @thread = Thread.new { run_loop }
  self
end

#stopObject



42
43
44
45
46
47
48
49
# File 'lib/sql_genius/core/analysis/stats_collector.rb', line 42

def stop
  @mutex.synchronize do
    @stop_signal = true
    @cv.signal
  end
  @thread&.join(STOP_JOIN_TIMEOUT)
  @thread = nil
end