Class: MongoStat::LogTimes

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_stat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ LogTimes

Returns a new instance of LogTimes.



44
45
46
47
48
49
50
51
52
# File 'lib/mongo_stat.rb', line 44

def initialize(collection)
  self.collection = collection
  self.stats = {}
  self.stats[self.collection] = Hash.new(0.0)
  self.log_counters = {}
  self.log_counters[self.collection] = Hash.new(0)
  self.log_timers = {}
  self.log_timers[self.collection] = Hash.new {|h, v| h[v] = Time.now.to_f}
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



41
42
43
# File 'lib/mongo_stat.rb', line 41

def collection
  @collection
end

#log_countersObject

Returns the value of attribute log_counters.



40
41
42
# File 'lib/mongo_stat.rb', line 40

def log_counters
  @log_counters
end

#log_timersObject

Returns the value of attribute log_timers.



42
43
44
# File 'lib/mongo_stat.rb', line 42

def log_timers
  @log_timers
end

#statsObject

Returns the value of attribute stats.



39
40
41
# File 'lib/mongo_stat.rb', line 39

def stats
  @stats
end

Instance Method Details

#get_averagesObject



92
93
94
95
96
97
98
# File 'lib/mongo_stat.rb', line 92

def get_averages()
  avgs = {}
  self.stats[self.collection].each {|stat, value|
    avgs[stat] = value / self.log_counters[self.collection][stat] rescue 0
  }
  avgs
end

#get_countersObject



88
89
90
# File 'lib/mongo_stat.rb', line 88

def get_counters()
  return self.log_counters[self.collection]
end

#get_totalsObject



84
85
86
# File 'lib/mongo_stat.rb', line 84

def get_totals()
  return self.stats[self.collection]
end

#log(stat, increment_value = 1) ⇒ Object



54
55
56
57
# File 'lib/mongo_stat.rb', line 54

def log(stat, increment_value = 1)
  self.stats[self.collection][stat] += increment_value
  self.log_counters[self.collection][stat] += 1
end

#log_end(stat) ⇒ Object



63
64
65
66
67
# File 'lib/mongo_stat.rb', line 63

def log_end(stat)
  self.stats[self.collection][stat] += Time.now.to_f - self.log_timers[self.collection][stat]
  self.log_timers[self.collection].delete(stat)
  self.log_counters[self.collection][stat] += 1
end

#log_start(stat) ⇒ Object



59
60
61
# File 'lib/mongo_stat.rb', line 59

def log_start(stat)
  self.log_timers[self.collection][stat]
end

#reset(stat) ⇒ Object



69
70
71
72
73
# File 'lib/mongo_stat.rb', line 69

def reset(stat)
  self.stats[self.collection][stat] = 0.0
  self.log_timers[self.collection].delete(stat) rescue nil
  self.log_counters[self.collection][stat] = 0
end

#reset_all_timersObject



80
81
82
# File 'lib/mongo_stat.rb', line 80

def reset_all_timers
  self.log_timers[self.collection] = Hash.new {|h, v| h[v] = Time.now.to_f}
end

#reset_timer(stat) ⇒ Object



75
76
77
# File 'lib/mongo_stat.rb', line 75

def reset_timer(stat)
  self.log_timers[self.collection].delete(stat) rescue nil
end