Module: StatCollector

Extended by:
StatCollector
Included in:
StatCollector
Defined in:
lib/stat_collector.rb

Instance Method Summary collapse

Instance Method Details

#cache_hitObject



43
44
45
46
47
48
# File 'lib/stat_collector.rb', line 43

def cache_hit
  target_db[:pg_statio_user_tables]
    .select("(sum(heap_blks_hit) - sum(heap_blks_read)) / sum(heap_blks_hit) as ratio".lit)
    .first[:ratio]
    .to_f
end

#capture_stats!Object



18
19
20
21
22
# File 'lib/stat_collector.rb', line 18

def capture_stats!
  s = stats
  DB[:stats] << {data: s.to_json}
  "connections=#{s[:connections]} stat_statements=#{s[:stat_statements].count} cache_hit=#{cache_hit}"
end

#connectionsObject



32
33
34
# File 'lib/stat_collector.rb', line 32

def connections
  target_db[:pg_stat_activity].count
end

#locksObject



50
51
52
# File 'lib/stat_collector.rb', line 50

def locks
  target_db[:pg_locks].exclude(:granted).count
end

#reset_target_stats!Object



24
25
26
# File 'lib/stat_collector.rb', line 24

def reset_target_stats!
  target_db.execute "select pg_stat_statements_reset()"
end

#stat_statementsObject



36
37
38
39
40
41
# File 'lib/stat_collector.rb', line 36

def stat_statements
  TARGET_DB[:pg_stat_statements]
    .select(:query, :calls, :total_time)
    .exclude(query: '<insufficient privilege>')
    .all
end

#statsObject



9
10
11
12
13
14
15
16
# File 'lib/stat_collector.rb', line 9

def stats
  {
    connections: connections,
    stat_statements: stat_statements,
    cache_hit: cache_hit,
    locks: locks
  }
end

#target_dbObject



28
29
30
# File 'lib/stat_collector.rb', line 28

def target_db
  TARGET_DB
end