redistat 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. data/.travis.yml +8 -0
  2. data/README.md +219 -97
  3. data/lib/redistat.rb +13 -13
  4. data/lib/redistat/buffer.rb +27 -24
  5. data/lib/redistat/collection.rb +5 -5
  6. data/lib/redistat/connection.rb +23 -18
  7. data/lib/redistat/core_ext.rb +1 -1
  8. data/lib/redistat/core_ext/bignum.rb +2 -2
  9. data/lib/redistat/core_ext/date.rb +2 -2
  10. data/lib/redistat/core_ext/fixnum.rb +2 -2
  11. data/lib/redistat/core_ext/hash.rb +4 -4
  12. data/lib/redistat/date.rb +11 -11
  13. data/lib/redistat/event.rb +18 -18
  14. data/lib/redistat/finder.rb +39 -39
  15. data/lib/redistat/finder/date_set.rb +4 -4
  16. data/lib/redistat/key.rb +16 -16
  17. data/lib/redistat/label.rb +14 -14
  18. data/lib/redistat/mixins/database.rb +1 -1
  19. data/lib/redistat/mixins/date_helper.rb +1 -1
  20. data/lib/redistat/mixins/options.rb +8 -8
  21. data/lib/redistat/mixins/synchronize.rb +12 -11
  22. data/lib/redistat/model.rb +25 -17
  23. data/lib/redistat/result.rb +4 -4
  24. data/lib/redistat/scope.rb +5 -5
  25. data/lib/redistat/summary.rb +33 -26
  26. data/lib/redistat/version.rb +1 -1
  27. data/redistat.gemspec +4 -3
  28. data/spec/buffer_spec.rb +27 -25
  29. data/spec/collection_spec.rb +4 -4
  30. data/spec/connection_spec.rb +12 -12
  31. data/spec/core_ext/hash_spec.rb +5 -5
  32. data/spec/database_spec.rb +3 -3
  33. data/spec/date_spec.rb +15 -15
  34. data/spec/event_spec.rb +8 -8
  35. data/spec/finder/date_set_spec.rb +134 -134
  36. data/spec/finder_spec.rb +36 -36
  37. data/spec/key_spec.rb +19 -19
  38. data/spec/label_spec.rb +10 -10
  39. data/spec/model_helper.rb +10 -9
  40. data/spec/model_spec.rb +38 -41
  41. data/spec/options_spec.rb +9 -9
  42. data/spec/result_spec.rb +4 -4
  43. data/spec/scope_spec.rb +6 -6
  44. data/spec/spec_helper.rb +6 -0
  45. data/spec/summary_spec.rb +31 -24
  46. data/spec/synchronize_spec.rb +118 -57
  47. data/spec/thread_safety_spec.rb +6 -6
  48. metadata +88 -126
  49. data/.rvmrc +0 -1
@@ -2,21 +2,21 @@ require "spec_helper"
2
2
 
3
3
  describe Redistat::Finder do
4
4
  include Redistat::Database
5
-
5
+
6
6
  before(:each) do
7
7
  db.flushdb
8
8
  @scope = "PageViews"
9
9
  @label = "about_us"
10
10
  @date = Time.now
11
11
  @key = Redistat::Key.new(@scope, @label, @date, {:depth => :day})
12
- @stats = {"views" => 3, "visitors" => 2}
12
+ @stats = {"views" => 3, "visitors" => 2}
13
13
  @two_hours_ago = 2.hours.ago
14
14
  @one_hour_ago = 1.hour.ago
15
15
  end
16
-
16
+
17
17
  it "should initialize properly" do
18
18
  options = {:scope => "PageViews", :label => "Label", :from => @two_hours_ago, :till => @one_hour_ago, :depth => :hour, :interval => :hour}
19
-
19
+
20
20
  finder = Redistat::Finder.new
21
21
  finder.send(:set_options, options)
22
22
  finder.options[:scope].should be_a(Redistat::Scope)
@@ -24,31 +24,31 @@ describe Redistat::Finder do
24
24
  finder.options[:label].should be_a(Redistat::Label)
25
25
  finder.options[:label].to_s.should == options[:label]
26
26
  finder.options.should == options.merge(:scope => finder.options[:scope], :label => finder.options[:label])
27
-
27
+
28
28
  finder = Redistat::Finder.scope("hello")
29
29
  finder.options[:scope].to_s.should == "hello"
30
30
  finder.scope.to_s.should == "hello"
31
-
31
+
32
32
  finder = Redistat::Finder.label("hello")
33
33
  finder.options[:label].to_s.should == "hello"
34
34
  finder.label.to_s.should == "hello"
35
-
35
+
36
36
  finder = Redistat::Finder.dates(@two_hours_ago, @one_hour_ago)
37
37
  finder.options[:from].should == @two_hours_ago
38
38
  finder.options[:till].should == @one_hour_ago
39
-
39
+
40
40
  finder = Redistat::Finder.from(@two_hours_ago)
41
41
  finder.options[:from].should == @two_hours_ago
42
42
  finder.from.should == @two_hours_ago
43
-
43
+
44
44
  finder = Redistat::Finder.till(@one_hour_ago)
45
45
  finder.options[:till].should == @one_hour_ago
46
46
  finder.till.should == @one_hour_ago
47
-
47
+
48
48
  finder = Redistat::Finder.depth(:hour)
49
49
  finder.options[:depth].should == :hour
50
50
  finder.depth.should == :hour
51
-
51
+
52
52
  finder = Redistat::Finder.interval(true)
53
53
  finder.options[:interval].should be_true
54
54
  finder.interval.should be_true
@@ -56,24 +56,24 @@ describe Redistat::Finder do
56
56
  finder.options[:interval].should be_false
57
57
  finder.interval.should be_false
58
58
  end
59
-
59
+
60
60
  it "should fetch stats properly" do
61
61
  first_stat, last_stat = create_example_stats
62
-
62
+
63
63
  stats = Redistat::Finder.find({:from => first_stat, :till => last_stat, :scope => @scope, :label => @label, :depth => :hour})
64
64
  stats.from.should == first_stat
65
65
  stats.till.should == last_stat
66
66
  stats.depth.should == :hour
67
-
67
+
68
68
  stats.total.should == { "views" => 12, "visitors" => 8 }
69
69
  stats.total.from.should == first_stat
70
70
  stats.total.till.should == last_stat
71
71
  stats.first.should == stats.total
72
72
  end
73
-
73
+
74
74
  it "should fetch data per unit when interval option is specified" do
75
75
  first_stat, last_stat = create_example_stats
76
-
76
+
77
77
  stats = Redistat::Finder.find(:from => first_stat, :till => last_stat, :scope => @scope, :label => @label, :depth => :hour, :interval => :hour)
78
78
  stats.from.should == first_stat
79
79
  stats.till.should == last_stat
@@ -89,22 +89,22 @@ describe Redistat::Finder do
89
89
  stats[4].should == {}
90
90
  stats[4].date.should == Time.parse("2010-05-14 16:00")
91
91
  end
92
-
92
+
93
93
  it "should return empty hash when attempting to fetch non-existent results" do
94
94
  stats = Redistat::Finder.find({:from => 3.hours.ago, :till => 2.hours.from_now, :scope => @scope, :label => @label, :depth => :hour})
95
95
  stats.total.should == {}
96
96
  end
97
-
97
+
98
98
  it "should throw error on invalid options" do
99
99
  lambda { Redistat::Finder.find(:from => 3.hours.ago) }.should raise_error(Redistat::InvalidOptions)
100
100
  end
101
-
101
+
102
102
  describe "Grouping" do
103
103
  before(:each) do
104
104
  @options = {:scope => "PageViews", :label => "message/public", :from => @two_hours_ago, :till => @one_hour_ago, :depth => :hour, :interval => :hour}
105
105
  @finder = Redistat::Finder.new(@options)
106
106
  end
107
-
107
+
108
108
  it "should return parent finder" do
109
109
  @finder.instance_variable_get("@parent").should be_nil
110
110
  @finder.parent.should be_a(Redistat::Finder)
@@ -116,7 +116,7 @@ describe Redistat::Finder do
116
116
  @finder.parent.options[:label].should be_nil
117
117
  @finder.parent.parent.should be_nil
118
118
  end
119
-
119
+
120
120
  it "should find children" do
121
121
  Redistat::Key.new("PageViews", "message/public/die").update_index
122
122
  Redistat::Key.new("PageViews", "message/public/live").update_index
@@ -130,25 +130,25 @@ describe Redistat::Finder do
130
130
  subs.should include('fester')
131
131
  end
132
132
  end
133
-
133
+
134
134
  describe "Lazy-Loading" do
135
-
135
+
136
136
  before(:each) do
137
137
  @first_stat, @last_stat = create_example_stats
138
138
 
139
139
  @finder = Redistat::Finder.new
140
140
  @finder.from(@first_stat).till(@last_stat).scope(@scope).label(@label).depth(:hour)
141
-
141
+
142
142
  @match = [{}, {"visitors"=>"4", "views"=>"6"},
143
143
  {"visitors"=>"2", "views"=>"3"},
144
144
  {"visitors"=>"2", "views"=>"3"}, {}]
145
145
  end
146
-
146
+
147
147
  it "should lazy-load" do
148
148
  @finder.instance_variable_get("@result").should be_nil
149
149
  stats = @finder.all
150
150
  @finder.instance_variable_get("@result").should_not be_nil
151
-
151
+
152
152
  stats.should == @finder.find # find method directly fetches results
153
153
  stats.total.should == @finder.total
154
154
  stats.total.should == { "views" => 12, "visitors" => 8 }
@@ -163,35 +163,35 @@ describe Redistat::Finder do
163
163
  stats = @finder.all
164
164
  stats.total.should == { "views" => 6, "visitors" => 4 }
165
165
  end
166
-
166
+
167
167
  it "should handle #map" do
168
168
  @finder.interval(:hour)
169
169
  @finder.map { |r| r }.should == @match
170
170
  end
171
-
171
+
172
172
  it "should handle #each" do
173
173
  @finder.interval(:hour)
174
-
174
+
175
175
  res = []
176
176
  @finder.each { |r| res << r }
177
177
  res.should == @match
178
178
  end
179
-
179
+
180
180
  it "should handle #each_with_index" do
181
181
  @finder.interval(:hour)
182
-
182
+
183
183
  res = {}
184
184
  match = {}
185
185
  @finder.each_with_index { |r, i| res[i] = r }
186
186
  @match.each_with_index { |r, i| match[i] = r }
187
187
  res.should == match
188
188
  end
189
-
189
+
190
190
  end # "Lazy-Loading"
191
-
192
-
191
+
192
+
193
193
  # helper methods
194
-
194
+
195
195
  def create_example_stats
196
196
  key = Redistat::Key.new(@scope, @label, (first = Time.parse("2010-05-14 13:43")))
197
197
  Redistat::Summary.send(:update_fields, key, @stats, :hour)
@@ -203,5 +203,5 @@ describe Redistat::Finder do
203
203
  Redistat::Summary.send(:update_fields, key, @stats, :hour)
204
204
  [first - 1.hour, last + 1.hour]
205
205
  end
206
-
206
+
207
207
  end
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe Redistat::Key do
4
4
  include Redistat::Database
5
-
5
+
6
6
  before(:each) do
7
7
  db.flushdb
8
8
  @scope = "PageViews"
@@ -11,7 +11,7 @@ describe Redistat::Key do
11
11
  @date = Time.now
12
12
  @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour})
13
13
  end
14
-
14
+
15
15
  it "should initialize properly" do
16
16
  @key.scope.to_s.should == @scope
17
17
  @key.label.to_s.should == @label
@@ -20,7 +20,7 @@ describe Redistat::Key do
20
20
  @key.date.should be_instance_of(Redistat::Date)
21
21
  @key.date.to_time.to_s.should == @date.to_s
22
22
  end
23
-
23
+
24
24
  it "should convert to string properly" do
25
25
  @key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}"
26
26
  props = [:year, :month, :day, :hour, :min, :sec]
@@ -31,19 +31,19 @@ describe Redistat::Key do
31
31
  key = Redistat::Key.new(@scope, nil, @date, {:depth => :hour})
32
32
  key.to_s.should == "#{@scope}:#{key.date.to_s(:hour)}"
33
33
  end
34
-
34
+
35
35
  it "should abide to hashed_label option" do
36
36
  @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :hashed_label => true})
37
37
  @key.to_s.should == "#{@scope}/#{@label_hash}:#{@key.date.to_s(:hour)}"
38
38
  @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour, :hashed_label => false})
39
39
  @key.to_s.should == "#{@scope}/#{@label}:#{@key.date.to_s(:hour)}"
40
40
  end
41
-
41
+
42
42
  it "should have default depth option" do
43
43
  @key = Redistat::Key.new(@scope, @label, @date)
44
44
  @key.depth.should == :hour
45
45
  end
46
-
46
+
47
47
  it "should allow changing attributes" do
48
48
  # scope
49
49
  @key.scope.to_s.should == @scope
@@ -64,13 +64,13 @@ describe Redistat::Key do
64
64
  @key.label.to_s.should == @label
65
65
  @key.label_hash == @label_hash
66
66
  end
67
-
67
+
68
68
  describe "Grouping" do
69
69
  before(:each) do
70
70
  @label = "message/public/offensive"
71
71
  @key = Redistat::Key.new(@scope, @label, @date, {:depth => :hour})
72
72
  end
73
-
73
+
74
74
  it "should create a group of keys from label group" do
75
75
  label = 'message/public/offensive'
76
76
  result = [ "message/public/offensive",
@@ -81,49 +81,49 @@ describe Redistat::Key do
81
81
 
82
82
  key.groups.map { |k| k.label.to_s }.should == result
83
83
  end
84
-
84
+
85
85
  it "should know it's parent" do
86
86
  @key.parent.should be_a(Redistat::Key)
87
87
  @key.parent.label.to_s.should == 'message/public'
88
88
  Redistat::Key.new(@scope, 'hello', @date).parent.should be_nil
89
89
  end
90
-
90
+
91
91
  it "should update label index and return children" do
92
92
  db.smembers("#{@scope}#{Redistat::LABEL_INDEX}#{@key.label.parent}").should == []
93
93
  @key.children.should have(0).items
94
-
94
+
95
95
  @key.update_index # indexing 'message/publish/offensive'
96
96
  Redistat::Key.new("PageViews", "message/public/die").update_index # indexing 'message/publish/die'
97
97
  Redistat::Key.new("PageViews", "message/public/live").update_index # indexing 'message/publish/live'
98
-
98
+
99
99
  members = db.smembers("#{@scope}#{Redistat::LABEL_INDEX}#{@key.label.parent}") # checking 'message/public'
100
100
  members.should have(3).item
101
101
  members.should include('offensive')
102
102
  members.should include('live')
103
103
  members.should include('die')
104
-
104
+
105
105
  key = @key.parent
106
106
  key.children.first.should be_a(Redistat::Key)
107
107
  key.children.should have(3).item
108
108
  key.children.map { |k| k.label.me }.should == members
109
-
109
+
110
110
  members = db.smembers("#{@scope}#{Redistat::LABEL_INDEX}#{key.label.parent}") # checking 'message'
111
111
  members.should have(1).item
112
112
  members.should include('public')
113
-
113
+
114
114
  key = key.parent
115
115
  key.children.should have(1).item
116
116
  key.children.map { |k| k.label.me }.should == members
117
-
117
+
118
118
  members = db.smembers("#{@scope}#{Redistat::LABEL_INDEX}") # checking ''
119
119
  members.should have(1).item
120
120
  members.should include('message')
121
-
121
+
122
122
  key.parent.should be_nil
123
123
  key = Redistat::Key.new("PageViews")
124
124
  key.children.should have(1).item
125
125
  key.children.map { |k| k.label.me }.should include('message')
126
126
  end
127
127
  end
128
-
129
- end
128
+
129
+ end
@@ -2,29 +2,29 @@ require "spec_helper"
2
2
 
3
3
  describe Redistat::Label do
4
4
  include Redistat::Database
5
-
5
+
6
6
  before(:each) do
7
7
  db.flushdb
8
8
  @name = "about_us"
9
9
  @label = Redistat::Label.new(@name)
10
10
  end
11
-
11
+
12
12
  it "should initialize properly and SHA1 hash the label name" do
13
13
  @label.name.should == @name
14
14
  @label.hash.should == Digest::SHA1.hexdigest(@name)
15
15
  end
16
-
16
+
17
17
  it "should store a label hash lookup key" do
18
18
  label = Redistat::Label.new(@name, {:hashed_label => true}).save
19
19
  label.saved?.should be_true
20
20
  db.hget(Redistat::KEY_LABELS, label.hash).should == @name
21
-
21
+
22
22
  name = "contact_us"
23
23
  label = Redistat::Label.create(name, {:hashed_label => true})
24
24
  label.saved?.should be_true
25
25
  db.hget(Redistat::KEY_LABELS, label.hash).should == name
26
26
  end
27
-
27
+
28
28
  it "should join labels" do
29
29
  include Redistat
30
30
  label = Label.join('email', 'message', 'public')
@@ -37,18 +37,18 @@ describe Redistat::Label do
37
37
  label.should be_a(Label)
38
38
  label.to_s.should == 'email/message/public'
39
39
  end
40
-
40
+
41
41
  describe "Grouping" do
42
42
  before(:each) do
43
43
  @name = "message/public/offensive"
44
44
  @label = Redistat::Label.new(@name)
45
45
  end
46
-
46
+
47
47
  it "should know it's parent label group" do
48
48
  @label.parent.to_s.should == 'message/public'
49
49
  Redistat::Label.new('hello').parent.should be_nil
50
50
  end
51
-
51
+
52
52
  it "should separate label names into groups" do
53
53
  @label.name.should == @name
54
54
  @label.groups.map { |l| l.to_s }.should == [ "message/public/offensive",
@@ -67,5 +67,5 @@ describe Redistat::Label do
67
67
  @label.groups.map { |l| l.to_s }.should == [ "message" ]
68
68
  end
69
69
  end
70
-
71
- end
70
+
71
+ end
@@ -2,29 +2,30 @@ require "redistat"
2
2
 
3
3
  class ModelHelper1
4
4
  include Redistat::Model
5
-
6
-
5
+
6
+
7
7
  end
8
8
 
9
9
  class ModelHelper2
10
10
  include Redistat::Model
11
-
11
+
12
12
  depth :day
13
13
  store_event true
14
14
  hashed_label true
15
-
15
+
16
16
  end
17
17
 
18
18
  class ModelHelper3
19
19
  include Redistat::Model
20
-
20
+
21
21
  connect_to :port => 8379, :db => 14
22
-
22
+
23
23
  end
24
24
 
25
25
  class ModelHelper4
26
26
  include Redistat::Model
27
-
27
+
28
28
  scope "FancyHelper"
29
-
30
- end
29
+ expire :hour => 24*3600
30
+
31
+ end
@@ -3,7 +3,7 @@ require "model_helper"
3
3
 
4
4
  describe Redistat::Model do
5
5
  include Redistat::Database
6
-
6
+
7
7
  before(:each) do
8
8
  @time = Time.utc(2010, 8, 28, 12, 0, 0)
9
9
  ModelHelper1.redis.flushdb
@@ -11,12 +11,12 @@ describe Redistat::Model do
11
11
  ModelHelper3.redis.flushdb
12
12
  ModelHelper4.redis.flushdb
13
13
  end
14
-
14
+
15
15
  it "should should name itself correctly" do
16
16
  ModelHelper1.send(:name).should == "ModelHelper1"
17
17
  ModelHelper2.send(:name).should == "ModelHelper2"
18
18
  end
19
-
19
+
20
20
  it "should return a Finder" do
21
21
  two_hours_ago = 2.hours.ago
22
22
  one_hour_ago = 1.hour.ago
@@ -27,18 +27,19 @@ describe Redistat::Model do
27
27
  finder.options[:from].should == two_hours_ago
28
28
  finder.options[:till].should == one_hour_ago
29
29
  end
30
-
30
+
31
31
  it "should #find_event" do
32
32
  Redistat::Event.should_receive(:find).with('ModelHelper1', 1)
33
33
  ModelHelper1.find_event(1)
34
34
  end
35
-
35
+
36
36
  it "should listen to model-defined options" do
37
37
  ModelHelper2.depth.should == :day
38
38
  ModelHelper2.store_event.should == true
39
39
  ModelHelper2.hashed_label.should == true
40
40
  ModelHelper2.scope.should be_nil
41
-
41
+ ModelHelper2.expire.should be_nil
42
+
42
43
  ModelHelper1.depth.should == nil
43
44
  ModelHelper1.store_event.should == nil
44
45
  ModelHelper1.hashed_label.should == nil
@@ -54,134 +55,135 @@ describe Redistat::Model do
54
55
  ModelHelper1.depth.should == nil
55
56
  ModelHelper1.store_event.should == nil
56
57
  ModelHelper1.hashed_label.should == nil
57
-
58
+
58
59
  ModelHelper4.scope.should == "FancyHelper"
59
60
  ModelHelper4.send(:name).should == "FancyHelper"
61
+ ModelHelper4.expire.should == {:hour => 24*3600}
60
62
  end
61
-
63
+
62
64
  it "should store and fetch stats" do
63
65
  ModelHelper1.store("sheep.black", {:count => 6, :weight => 461}, @time.hours_ago(4))
64
66
  ModelHelper1.store("sheep.black", {:count => 2, :weight => 156}, @time)
65
-
67
+
66
68
  stats = ModelHelper1.fetch("sheep.black", @time.hours_ago(2), @time.hours_since(1))
67
69
  stats.total["count"].should == 2
68
70
  stats.total["weight"].should == 156
69
71
  stats.first.should == stats.total
70
-
72
+
71
73
  stats = ModelHelper1.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1))
72
74
  stats.total[:count].should == 8
73
75
  stats.total[:weight].should == 617
74
76
  stats.first.should == stats.total
75
-
77
+
76
78
  ModelHelper1.store("sheep.white", {:count => 5, :weight => 393}, @time.hours_ago(4))
77
79
  ModelHelper1.store("sheep.white", {:count => 4, :weight => 316}, @time)
78
-
80
+
79
81
  stats = ModelHelper1.fetch("sheep.white", @time.hours_ago(2), @time.hours_since(1))
80
82
  stats.total[:count].should == 4
81
83
  stats.total[:weight].should == 316
82
84
  stats.first.should == stats.total
83
-
85
+
84
86
  stats = ModelHelper1.fetch("sheep.white", @time.hours_ago(5), @time.hours_since(1))
85
87
  stats.total[:count].should == 9
86
88
  stats.total[:weight].should == 709
87
89
  stats.first.should == stats.total
88
90
  end
89
-
91
+
90
92
  it "should store and fetch grouping enabled stats" do
91
93
  ModelHelper1.store("sheep/black", {:count => 6, :weight => 461}, @time.hours_ago(4))
92
94
  ModelHelper1.store("sheep/black", {:count => 2, :weight => 156}, @time)
93
95
  ModelHelper1.store("sheep/white", {:count => 5, :weight => 393}, @time.hours_ago(4))
94
96
  ModelHelper1.store("sheep/white", {:count => 4, :weight => 316}, @time)
95
-
97
+
96
98
  stats = ModelHelper1.fetch("sheep/black", @time.hours_ago(2), @time.hours_since(1))
97
99
  stats.total["count"].should == 2
98
100
  stats.total["weight"].should == 156
99
101
  stats.first.should == stats.total
100
-
102
+
101
103
  stats = ModelHelper1.fetch("sheep/black", @time.hours_ago(5), @time.hours_since(1))
102
104
  stats.total[:count].should == 8
103
105
  stats.total[:weight].should == 617
104
106
  stats.first.should == stats.total
105
-
107
+
106
108
  stats = ModelHelper1.fetch("sheep/white", @time.hours_ago(2), @time.hours_since(1))
107
109
  stats.total[:count].should == 4
108
110
  stats.total[:weight].should == 316
109
111
  stats.first.should == stats.total
110
-
112
+
111
113
  stats = ModelHelper1.fetch("sheep/white", @time.hours_ago(5), @time.hours_since(1))
112
114
  stats.total[:count].should == 9
113
115
  stats.total[:weight].should == 709
114
116
  stats.first.should == stats.total
115
-
117
+
116
118
  stats = ModelHelper1.fetch("sheep", @time.hours_ago(2), @time.hours_since(1))
117
119
  stats.total[:count].should == 6
118
120
  stats.total[:weight].should == 472
119
121
  stats.first.should == stats.total
120
-
122
+
121
123
  stats = ModelHelper1.fetch("sheep", @time.hours_ago(5), @time.hours_since(1))
122
124
  stats.total[:count].should == 17
123
125
  stats.total[:weight].should == 1326
124
126
  stats.first.should == stats.total
125
127
  end
126
-
128
+
127
129
  it "should connect to different Redis servers on a per-model basis" do
128
130
  ModelHelper3.redis.client.db.should == 14
129
-
131
+
130
132
  ModelHelper3.store("sheep.black", {:count => 6, :weight => 461}, @time.hours_ago(4), :label_indexing => false)
131
133
  ModelHelper3.store("sheep.black", {:count => 2, :weight => 156}, @time, :label_indexing => false)
132
-
134
+
133
135
  db.keys("*").should be_empty
134
136
  ModelHelper1.redis.keys("*").should be_empty
135
137
  db("ModelHelper3").keys("*").should have(5).items
136
138
  ModelHelper3.redis.keys("*").should have(5).items
137
-
139
+
138
140
  stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(2), @time.hours_since(1), :label_indexing => false)
139
141
  stats.total["count"].should == 2
140
142
  stats.total["weight"].should == 156
141
143
  stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1), :label_indexing => false)
142
144
  stats.total[:count].should == 8
143
145
  stats.total[:weight].should == 617
144
-
146
+
145
147
  ModelHelper3.connect_to(:port => 8379, :db => 13)
146
148
  ModelHelper3.redis.client.db.should == 13
147
-
149
+
148
150
  stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1), :label_indexing => false)
149
151
  stats.total.should == {}
150
-
152
+
151
153
  ModelHelper3.connect_to(:port => 8379, :db => 14)
152
154
  ModelHelper3.redis.client.db.should == 14
153
-
155
+
154
156
  stats = ModelHelper3.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1), :label_indexing => false)
155
157
  stats.total[:count].should == 8
156
158
  stats.total[:weight].should == 617
157
159
  end
158
-
160
+
159
161
  describe "Write Buffer" do
160
162
  before(:each) do
161
163
  Redistat.buffer_size = 20
162
164
  end
163
-
165
+
164
166
  after(:each) do
165
167
  Redistat.buffer_size = 0
166
168
  end
167
-
169
+
168
170
  it "should buffer calls in memory before committing to Redis" do
169
171
  14.times do
170
172
  ModelHelper1.store("sheep.black", {:count => 1, :weight => 461}, @time.hours_ago(4))
171
173
  end
172
174
  ModelHelper1.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1)).total.should == {}
173
-
175
+
174
176
  5.times do
175
177
  ModelHelper1.store("sheep.black", {:count => 1, :weight => 156}, @time)
176
178
  end
177
179
  ModelHelper1.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1)).total.should == {}
178
-
180
+
179
181
  ModelHelper1.store("sheep.black", {:count => 1, :weight => 156}, @time)
180
182
  stats = ModelHelper1.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1))
181
183
  stats.total["count"].should == 20
182
184
  stats.total["weight"].should == 7390
183
185
  end
184
-
186
+
185
187
  it "should force flush buffer when #flush(true) is called" do
186
188
  ModelHelper1.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1)).total.should == {}
187
189
  14.times do
@@ -189,16 +191,11 @@ describe Redistat::Model do
189
191
  end
190
192
  ModelHelper1.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1)).total.should == {}
191
193
  Redistat.buffer.flush(true)
192
-
194
+
193
195
  stats = ModelHelper1.fetch("sheep.black", @time.hours_ago(5), @time.hours_since(1))
194
196
  stats.total["count"].should == 14
195
197
  stats.total["weight"].should == 6454
196
198
  end
197
199
  end
198
-
199
- end
200
-
201
-
202
-
203
-
204
200
 
201
+ end