spec/bucket_spec.rb in simple_metrics-0.0.1 vs spec/bucket_spec.rb in simple_metrics-0.2.2

- old
+ new

@@ -41,11 +41,11 @@ Mongo.ensure_collections_exist bucket.save(stats, ts) end let(:stats) do - Stats.create_counter(:name => "key1", :value => 5) + DataPoint.create_counter(:name => "key1", :value => 5) end it "saves given data in bucket" do results = bucket.find_all_by_name("key1") results.should have(1).item @@ -69,13 +69,13 @@ Mongo.ensure_collections_exist end describe "#find_all_by_name" do it "returns all stats for given name" do - stats_same1 = Stats.create_counter(:name => "key1", :value => 5) - stats_same2 = Stats.create_counter(:name => "key1", :value => 3) - stats_different = Stats.create_counter(:name => "key2", :value => 3) + stats_same1 = DataPoint.create_counter(:name => "key1", :value => 5) + stats_same2 = DataPoint.create_counter(:name => "key1", :value => 3) + stats_different = DataPoint.create_counter(:name => "key2", :value => 3) bucket.save(stats_same1, ts) bucket.save(stats_same2, ts) bucket.save(stats_different, ts) @@ -85,12 +85,12 @@ end end describe "#find_all_in_ts" do it "returns all stats in given timestamp" do - stats1 = Stats.create_counter(:name => "key1", :value => 5) - stats2 = Stats.create_counter(:name => "key2", :value => 3) + stats1 = DataPoint.create_counter(:name => "key1", :value => 5) + stats2 = DataPoint.create_counter(:name => "key2", :value => 3) bucket.save(stats1, ts) bucket.save(stats2, bucket.next_ts_bucket(ts)) result1 = bucket.find_all_in_ts(ts).first @@ -103,14 +103,14 @@ end end describe "#find_all_in_ts_by_name" do it "returns all stats for given name and timestamp" do - stats1a = Stats.create_counter(:name => "key1", :value => 5) - stats1b = Stats.create_counter(:name => "key1", :value => 7) - stats2 = Stats.create_counter(:name => "key2", :value => 7) - stats1_different_ts = Stats.create_counter(:name => "key1", :value => 3) + stats1a = DataPoint.create_counter(:name => "key1", :value => 5) + stats1b = DataPoint.create_counter(:name => "key1", :value => 7) + stats2 = DataPoint.create_counter(:name => "key2", :value => 7) + stats1_different_ts = DataPoint.create_counter(:name => "key1", :value => 3) bucket.save(stats1a, ts) bucket.save(stats1b, ts) bucket.save(stats2, ts) bucket.save(stats1_different_ts, bucket.next_ts_bucket(ts)) @@ -120,22 +120,67 @@ results.first.name.should == "key1" results.last.name.should == "key1" end end + describe "#find_all_in_ts_by_wildcard" do + it "returns all stats for given name and timestamp" do + stats1 = DataPoint.create_counter(:name => "com.test.key1", :value => 5) + stats2 = DataPoint.create_counter(:name => "com.test.key2", :value => 7) + stats_different = DataPoint.create_counter(:name => "com.test2.key1", :value => 3) + + from = bucket.ts_bucket(ts) + to = from + bucket.save(stats1, ts) + bucket.save(stats2, ts) + bucket.save(stats_different, ts) + + results = bucket.find_all_in_ts_range_by_wildcard(from, to, "com.test.*") + + results.should have(2).items + results.first.name.should == "com.test.key1" + results.last.name.should == "com.test.key2" + end + end + + describe "#fill_gaps" do + it "returns stats and fills missing gaps with null entries" do + stats = DataPoint.create_counter(:name => "com.test.key1", :value => 5) + + from = ts - 10 + to = ts + 10 + bucket.save(stats, ts) + ts_bucket = bucket.ts_bucket(ts) + + results = bucket.fill_gaps(from, to, [stats]) + + results.should have(3).items + results[0].name.should == "com.test.key1" + results[1].name.should == "com.test.key1" + results[2].name.should == "com.test.key1" + + results[0].value.should be_nil + results[1].value.should == 5 + results[2].value.should be_nil + + results[0].ts.should == ts_bucket - 10 + results[1].ts.should == ts_bucket + results[2].ts.should == ts_bucket + 10 + end + end end # describe "finder methods" describe "#aggregate_all" do before do Mongo.truncate_collections Mongo.ensure_collections_exist end - it "aggregates all stats" do - stats1a = Stats.create_counter(:name => "key1", :value => 5) - stats1b = Stats.create_counter(:name => "key1", :value => 7) - stats2 = Stats.create_counter(:name => "key2", :value => 3) + it "aggregates all counter data points" do + stats1a = DataPoint.create_counter(:name => "key1", :value => 5) + stats1b = DataPoint.create_counter(:name => "key1", :value => 7) + stats2 = DataPoint.create_counter(:name => "key2", :value => 3) bucket2 = Bucket[1] ts_at_insert = bucket2.previous_ts_bucket(ts) bucket.save(stats1a, ts_at_insert) bucket.save(stats1b, ts_at_insert) @@ -152,30 +197,77 @@ key2_result = results.find {|stat| stat.name == "key2"} key2_result.value.should == 3 key2_result.should be_counter end + + it "aggregates all gauge data points" do + stats1a = DataPoint.create_gauge(:name => "key1", :value => 5) + stats1b = DataPoint.create_gauge(:name => "key1", :value => 7) + stats2 = DataPoint.create_gauge(:name => "key2", :value => 3) + + bucket2 = Bucket[1] + ts_at_insert = bucket2.previous_ts_bucket(ts) + bucket.save(stats1a, ts_at_insert) + bucket.save(stats1b, ts_at_insert) + bucket.save(stats2, ts_at_insert) + + Bucket.aggregate_all(ts) + + results = bucket2.find_all_in_ts(ts_at_insert) + results.should have(2).items + + key1_result = results.find {|stat| stat.name == "key1"} + key1_result.value.should == 6 + key1_result.should be_gauge + + key2_result = results.find {|stat| stat.name == "key2"} + key2_result.value.should == 3 + key2_result.should be_gauge + end + end # describe "#aggregate_all" - describe "#flush_stats" do + describe "#flush_data_points" do before do - stats1 = Stats.create_counter(:name => "key1", :value => 5) - stats2 = Stats.create_counter(:name => "key1", :value => 7) - stats3 = Stats.create_counter(:name => "key2", :value => 3) + Mongo.truncate_collections + Mongo.ensure_collections_exist + + stats1 = DataPoint.create_counter(:name => "key1", :value => 5) + stats2 = DataPoint.create_counter(:name => "key1", :value => 7) + stats3 = DataPoint.create_counter(:name => "key2", :value => 3) @stats = [stats1, stats2, stats3] end it "saves all stats in finest/first bucket" do - Bucket.flush_stats(@stats) + Bucket.flush_data_points(@stats) results = bucket.find_all_in_ts(ts) - results.should have(3).items + results.should have(2).items end it "calls aggregate_all afterwards" do mock(Bucket).aggregate_all(ts) - Bucket.flush_stats(@stats) + Bucket.flush_data_points(@stats) end - end # describe "#flush_stats" + + it "saves all stats and aggregate if duplicates found" do + Bucket.flush_data_points(@stats) + + results = bucket.find_all_in_ts(ts) + results.should have(2).items + results.first.name.should == "key1" + results.last.name.should == "key2" + results.first.value == 12 + results.last.value == 3 + end + + it "raises error if name matches but type does not" do + stats4 = DataPoint.create_gauge(:name => "key1", :value => 3) + input = @stats + [stats4] + expect { Bucket.flush_data_points(input) }.to raise_error(SimpleMetrics::DataPoint::NonMatchingTypesError) + end + + end # describe "#flush_data_points" end end