Module: Pgmonitor::UsageData

Extended by:
UsageData
Included in:
UsageData
Defined in:
lib/pgmonitor/usagedata.rb

Constant Summary collapse

@@data =
{}

Instance Method Summary collapse

Instance Method Details

#add(database, data) ⇒ Object

@@data = {

   mem and cpu are aggregated among all the different postgres processes
        'database' => [ { 'mem' => '50024', 'cpu' => '05', 'connections' => 1, 'timeslice' => Time.now } ]
}


14
15
16
17
# File 'lib/pgmonitor/usagedata.rb', line 14

def add(database, data)
  @@data[database] ||= []
  @@data[database] << data
end

#avgObject



27
28
29
30
31
32
33
34
35
# File 'lib/pgmonitor/usagedata.rb', line 27

def avg
  avgs = {}
  @@data.each do |database, d|
    next if d.size == 0
    avgs[database] = avg_data(d)
  end
  avgs.each { |db, d| avgs.delete(db) if db.nil? or d.nil? }
  avgs
end

#avg_data(mdata) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pgmonitor/usagedata.rb', line 37

def avg_data(mdata)
  fields = %w[mem cpu connections]
  avgs = {}
  num = mdata.size
  fields.each do |field|
    avgs[field] ||= 0
    mdata.each do |d|
      avgs[field] += d[field]
    end
    avgs[field] = avgs[field] / num
  end

  avgs['timeslice'] = mdata.collect { |d| d['timeslice'] }.sort
  t1 = avgs['timeslice'].first
  t2 = avgs['timeslice'].last
  avgs['elapsed_time'] = (t2 - t1).to_i
  avgs
end

#cleanObject



19
20
21
22
23
24
25
# File 'lib/pgmonitor/usagedata.rb', line 19

def clean
  cutoff = Time.now - 60*2
  @@data.each do |database, d|
    d.reject! { |d| d['timeslice'] < cutoff }
    @@data.delete(database) if d.size == 0
  end
end

#dataObject



5
6
7
# File 'lib/pgmonitor/usagedata.rb', line 5

def data
  @@data
end