Module: StatCollector

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

Instance Method Summary collapse

Instance Method Details

#cache_hitObject



45
46
47
48
49
50
# File 'lib/stat_collector.rb', line 45

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



20
21
22
23
24
# File 'lib/stat_collector.rb', line 20

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



34
35
36
# File 'lib/stat_collector.rb', line 34

def connections
  target_db[:pg_stat_activity].count
end

#locksObject



52
53
54
# File 'lib/stat_collector.rb', line 52

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

#reset_target_stats!Object



26
27
28
# File 'lib/stat_collector.rb', line 26

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

#stat_statementsObject



38
39
40
41
42
43
# File 'lib/stat_collector.rb', line 38

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

#statsObject



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

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

#target_dbObject



30
31
32
# File 'lib/stat_collector.rb', line 30

def target_db
  TARGET_DB
end