counter 0.0.5 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/counter.gemspec +1 -1
- data/lib/counter/moving_count.rb +9 -5
- data/test/test_moving_count.rb +16 -0
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.1
|
data/counter.gemspec
CHANGED
data/lib/counter/moving_count.rb
CHANGED
@@ -67,17 +67,21 @@ class MovingCount < ActiveRecord::Base
|
|
67
67
|
# Yields a Counter instance, all standard methods apply.
|
68
68
|
# Raises an exception if the timestamp would fall too soon under the <tt>sample_interval</tt>.
|
69
69
|
def self.record_counts timestamp=Time.now, &block
|
70
|
+
timestamp = Time.at(timestamp) if timestamp.is_a?(Integer)
|
70
71
|
c = Counter.new
|
71
72
|
yield(c)
|
72
73
|
|
73
74
|
self.transaction do
|
74
75
|
check_sample_valid(timestamp)
|
76
|
+
|
77
|
+
unless c.counts.empty?
|
78
|
+
q = "INSERT INTO #{self.table_name} (sample_time, category, count) VALUES "
|
79
|
+
c.counts.each { |key, count| q += self.sanitize_sql(['(?, ?, ?),', timestamp, key, count]) }
|
80
|
+
q.chop!
|
75
81
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
self.connection.execute(q)
|
82
|
+
self.connection.execute(q)
|
83
|
+
end
|
84
|
+
|
81
85
|
self.delete_all(['sample_time < ?', (timestamp - history_to_keep)])
|
82
86
|
end
|
83
87
|
|
data/test/test_moving_count.rb
CHANGED
@@ -20,6 +20,11 @@ class MovingCountTest < Test::Unit::TestCase
|
|
20
20
|
assert_equal 1, PageView.find_by_category('http://www.nytimes.com/article.html').count
|
21
21
|
end
|
22
22
|
|
23
|
+
should "not save if no data was recorded" do
|
24
|
+
assert_nothing_raised { PageView.record_counts { |c| } }
|
25
|
+
assert_equal 0, PageView.count
|
26
|
+
end
|
27
|
+
|
23
28
|
context "sample interval" do
|
24
29
|
should "raise a RuntimeError to enforce sample interval" do
|
25
30
|
setup_existing_counts(59)
|
@@ -63,6 +68,17 @@ class MovingCountTest < Test::Unit::TestCase
|
|
63
68
|
|
64
69
|
assert_equal 2, PageView.count(:conditions => {:sample_time => Time.now})
|
65
70
|
end
|
71
|
+
|
72
|
+
should "use allow timestamp to be specified as a UNIX stamp" do
|
73
|
+
Timecop.freeze
|
74
|
+
|
75
|
+
PageView.record_counts(Time.now.to_i) do |c|
|
76
|
+
c.saw('http://www.nytimes.com')
|
77
|
+
c.saw('http://www.nytimes.com/article.html')
|
78
|
+
end
|
79
|
+
|
80
|
+
assert_equal 2, PageView.count(:conditions => {:sample_time => Time.now})
|
81
|
+
end
|
66
82
|
end
|
67
83
|
|
68
84
|
context "purge" do
|