mongo_stat 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/mongo_stat.rb +103 -0
  2. metadata +79 -0
data/lib/mongo_stat.rb ADDED
@@ -0,0 +1,103 @@
1
+ require 'mongo'
2
+ require 'active_support/core_ext'
3
+
4
+ module MongoStat
5
+ class MongoStat
6
+
7
+ attr_accessor :stats
8
+ attr_accessor :collection
9
+ attr_accessor :mongo_host
10
+ attr_accessor :mongo_db
11
+
12
+
13
+ def initialize(host, db, collection)
14
+ self.collection = collection
15
+ self.mongo_host = host
16
+ self.mongo_db = db
17
+ self.stats = {}
18
+ self.stats[self.collection] = Hash.new(0)
19
+ end
20
+
21
+ def log(stat, increment_value = 1)
22
+ self.stats[self.collection][stat] += increment_value
23
+ end
24
+
25
+ def save()
26
+ mdb = Mongo::Connection.new(self.mongo_host).db(self.mongo_db)
27
+ stat_coll = mdb.collection(self.collection)
28
+ stat_coll.find({'date' => Time.now.to_date.to_time}).first
29
+ stat_coll.update({'date' => Time.now.to_date.to_time}, {'$inc' => self.stats[collection]}, {:upsert => true})
30
+ mdb.connection.close
31
+ puts "Stats logged: #{self.stats[self.collection]}" if $DEBUG_MODE == true
32
+ self.stats[self.collection] = Hash.new(0)
33
+ end
34
+
35
+ end
36
+
37
+ class LogTimes
38
+
39
+ attr_accessor :stats
40
+ attr_accessor :log_counters
41
+ attr_accessor :collection
42
+ attr_accessor :log_timers
43
+
44
+ def initialize(collection)
45
+ self.collection = collection
46
+ self.stats = {}
47
+ self.stats[self.collection] = Hash.new(0.0)
48
+ self.log_counters = {}
49
+ self.log_counters[self.collection] = Hash.new(0)
50
+ self.log_timers = {}
51
+ self.log_timers[self.collection] = Hash.new {|h, v| h[v] = Time.now.to_f}
52
+ end
53
+
54
+ def log(stat, increment_value = 1)
55
+ self.stats[self.collection][stat] += increment_value
56
+ self.log_counters[self.collection][stat] += 1
57
+ end
58
+
59
+ def log_start(stat)
60
+ self.log_timers[self.collection][stat]
61
+ end
62
+
63
+ def log_end(stat)
64
+ self.stats[self.collection][stat] += Time.now.to_f - self.log_timers[self.collection][stat]
65
+ self.log_timers[self.collection].delete(stat)
66
+ self.log_counters[self.collection][stat] += 1
67
+ end
68
+
69
+ def reset(stat)
70
+ self.stats[self.collection][stat] = 0.0
71
+ self.log_timers[self.collection].delete(stat) rescue nil
72
+ self.log_counters[self.collection][stat] = 0
73
+ end
74
+
75
+ def reset_timer(stat)
76
+ self.log_timers[self.collection].delete(stat) rescue nil
77
+ end
78
+
79
+
80
+ def reset_all_timers
81
+ self.log_timers[self.collection] = Hash.new {|h, v| h[v] = Time.now.to_f}
82
+ end
83
+
84
+ def get_totals()
85
+ return self.stats[self.collection]
86
+ end
87
+
88
+ def get_counters()
89
+ return self.log_counters[self.collection]
90
+ end
91
+
92
+ def get_averages()
93
+ avgs = {}
94
+ self.stats[self.collection].each {|stat, value|
95
+ avgs[stat] = value / self.log_counters[self.collection][stat] rescue 0
96
+ }
97
+ avgs
98
+ end
99
+
100
+
101
+ end
102
+
103
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_stat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Fields
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongo
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: active_support
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A simple front-end for high performance aggregate counter stores in mongodb
47
+ email: adam@morningside-analytics.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/mongo_stat.rb
53
+ homepage: http://rubygems.org/gems/mongo_stat
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements:
72
+ - Mongo Gem
73
+ - Active Support
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.25
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: MongoStat
79
+ test_files: []