Permalink
Browse files

command for sensor data removal

  • Loading branch information...
1 parent 6acafa0 commit 1c48ec3fa996931d4bffc11e72b8cc108020018b @savonarola committed Jun 8, 2012
Showing with 88 additions and 2 deletions.
  1. +21 −1 lib/pulse-meter/sensor/timeline.rb
  2. +67 −1 spec/shared_examples/timeline_sensor.rb
View
22 lib/pulse-meter/sensor/timeline.rb
@@ -149,7 +149,6 @@ def optimized_inteval(start_time, end_time)
res_interval
end
-
# Returns sensor data for given interval making in-memory summarization
# and returns calculated value
# @param interval_id [Fixnum]
@@ -160,6 +159,27 @@ def get_raw_value(interval_id)
SensorData.new(Time.at(interval_id), nil)
end
+ # Drops sensor data within given time
+ # @param from [Time] lower bound
+ # @param till [Time] upper bound
+ # @raise ArgumentError if argumets are not valid time objects
+ def drop_within(from, till)
+ raise ArgumentError unless from.kind_of?(Time) && till.kind_of?(Time)
+ start_time, end_time = from.to_i, till.to_i
+ current_interval_id = get_interval_id(start_time) + interval
+ keys = []
+ while current_interval_id < end_time
+ keys << data_key(current_interval_id)
+ keys << raw_data_key(current_interval_id)
+ current_interval_id += interval
+ end
+ if keys.empty?
+ 0
+ else
+ redis.del(*keys)
+ end
+ end
+
# Returns Redis key by which raw data for current interval is stored
def current_raw_data_key
raw_data_key(current_interval_id)
View
68 spec/shared_examples/timeline_sensor.rb
@@ -163,7 +163,7 @@ def inner_data(obj)
end
describe "#timeline_within" do
- it "shoulde raise exception unless both arguments are Time objects" do
+ it "should raise exception unless both arguments are Time objects" do
[:q, nil, -1].each do |bad_value|
expect{ sensor.timeline_within(Time.now, bad_value) }.to raise_exception(ArgumentError)
expect{ sensor.timeline_within(bad_value, Time.now) }.to raise_exception(ArgumentError)
@@ -244,6 +244,72 @@ def inner_data(obj)
end
end
+ describe "#drop_within" do
+ it "should raise exception unless both arguments are Time objects" do
+ [:q, nil, -1].each do |bad_value|
+ expect{ sensor.drop_within(Time.now, bad_value) }.to raise_exception(ArgumentError)
+ expect{ sensor.drop_within(bad_value, Time.now) }.to raise_exception(ArgumentError)
+ end
+ end
+
+ it "should drop as many raw results as there are sensor interval beginnings in the passed interval" do
+ Timecop.freeze(@start_of_interval){ sensor.event(sample_event) }
+ Timecop.freeze(@start_of_interval + interval){ sensor.event(sample_event) }
+
+ future = @start_of_interval + interval * 3
+ Timecop.freeze(future) do
+ sensor.drop_within(
+ Time.at(@start_of_interval + interval - 1),
+ Time.at(@start_of_interval + interval + 1)
+ ).should == 1
+
+ data = sensor.timeline_within(
+ Time.at(@start_of_interval + interval - 1),
+ Time.at(@start_of_interval + interval + 1)
+ )
+ data.size.should == 1
+ data.first.value.should be_nil # since data is dropped
+
+ end
+
+ Timecop.freeze(@start_of_interval + interval + 2) do
+ sensor.drop_within(
+ Time.at(@start_of_interval + interval + 1),
+ Time.at(@start_of_interval + interval + 2)
+ ).should == 0
+ end
+ end
+
+ it "should drop as many reduced results as there are sensor interval beginnings in the passed interval" do
+ Timecop.freeze(@start_of_interval){ sensor.event(sample_event) }
+ Timecop.freeze(@start_of_interval + interval){ sensor.event(sample_event) }
+
+ future = @start_of_interval
+ Timecop.freeze(future) do
+ sensor.reduce_all_raw
+ sensor.drop_within(
+ Time.at(@start_of_interval + interval - 1),
+ Time.at(@start_of_interval + interval + 1)
+ ).should == 1
+
+ data = sensor.timeline_within(
+ Time.at(@start_of_interval + interval - 1),
+ Time.at(@start_of_interval + interval + 1)
+ )
+ data.size.should == 1
+ data.first.value.should be_nil # since data is dropped
+
+ end
+
+ Timecop.freeze(@start_of_interval + interval + 2) do
+ sensor.drop_within(
+ Time.at(@start_of_interval + interval + 1),
+ Time.at(@start_of_interval + interval + 2)
+ ).should == 0
+ end
+ end
+ end
+
describe "SensorData value for an interval" do
def check_sensor_data(sensor, value)
data = sensor.timeline(2).first

0 comments on commit 1c48ec3

Please sign in to comment.