Module: StatCollector

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

Instance Method Summary collapse

Instance Method Details

#cache_hitObject



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

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



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

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



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

def connections
  target_db[:pg_stat_activity].count
end

#locksObject



56
57
58
# File 'lib/stat_collector.rb', line 56

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

#prune_stats!Object



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

def prune_stats!
  DB[:stats].where('created_at < ?', Time.now - (12 * 60 * 60)).delete
end

#reset_target_stats!Object



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

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

#stat_statementsObject



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

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



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

def target_db
  TARGET_DB
end