Permalink
Browse files

#348457 timeline_within can give full timelined without optimization

  • Loading branch information...
s.averyanov
s.averyanov committed Jul 30, 2012
1 parent b240470 commit 8abc5c2df0bc1e4c675accc1800c3e3fab6f3a15
Showing with 47 additions and 25 deletions.
  1. +22 −20 lib/pulse-meter/sensor/timeline.rb
  2. +25 −5 spec/shared_examples/timeline_sensor.rb
@@ -108,12 +108,17 @@ def timeline(time_ago)
# Returts sensor data within given time
# @param from [Time] lower bound
# @param till [Time] upper bound
+ # @param skip_optimization [Boolean] must be set to true to skip interval optimization
# @return [Array<SensorData>]
# @raise ArgumentError if argumets are not valid time objects
- def timeline_within(from, till)
+ def timeline_within(from, till, skip_optimization = false)
raise ArgumentError unless from.kind_of?(Time) && till.kind_of?(Time)
start_time, end_time = from.to_i, till.to_i
- actual_interval = optimized_inteval(start_time, end_time)
+ actual_interval = if skip_optimization
+ interval
+ else
+ optimized_interval(start_time, end_time)
+ end
current_interval_id = get_interval_id(start_time) + actual_interval
keys = []
ids = []
@@ -134,19 +139,6 @@ def timeline_within(from, till)
res
end
- # Makes interval optimization so that the requested timespan contains less than MAX_TIMESPAN_POINTS values
- # @param start_time [Fixnum] unix timestamp of timespan start
- # @param end_time [Fixnum] unix timestamp of timespan start
- # @return [Fixnum] optimized interval in seconds.
- def optimized_inteval(start_time, end_time)
- res_interval = interval
- timespan = end_time - start_time
- while timespan / res_interval > MAX_TIMESPAN_POINTS - 1
- res_interval *= 2
- end
- res_interval
- end
-
# Returns sensor data for given interval making in-memory summarization
# and returns calculated value
# @param interval_id [Fixnum]
@@ -174,11 +166,7 @@ def drop_within(from, till)
keys << raw_data_key(current_interval_id)
current_interval_id += interval
end
- if keys.empty?
- 0
- else
- redis.del(*keys)
- end
+ keys.empty? ? 0 : redis.del(*keys)
end
# Returns Redis key by which raw data for current interval is stored
@@ -255,6 +243,20 @@ def process_event(value = nil)
end
end
+ # Makes interval optimization so that the requested timespan contains less than MAX_TIMESPAN_POINTS values
+ # @param start_time [Fixnum] unix timestamp of timespan start
+ # @param end_time [Fixnum] unix timestamp of timespan start
+ # @return [Fixnum] optimized interval in seconds.
+ def optimized_interval(start_time, end_time)
+ res_interval = interval
+ timespan = end_time - start_time
+ while timespan / res_interval > MAX_TIMESPAN_POINTS - 1
+ res_interval *= 2
+ end
+ res_interval
+ end
+
+
end
end
end
@@ -242,12 +242,32 @@ def inner_data(obj)
end
end
- it "should not return more than #{PulseMeter::Sensor::Timeline::MAX_TIMESPAN_POINTS} points" do
- max = PulseMeter::Sensor::Timeline::MAX_TIMESPAN_POINTS
- (1..10).each do |i|
- timespan = sensor.interval * max * (2**i)
- sensor.timeline_within(Time.now, Time.now - timespan).size.should < max
+ context "to avoid getting to much data" do
+ let(:max) {PulseMeter::Sensor::Timeline::MAX_TIMESPAN_POINTS}
+ it "should skip some points not to exceed MAX_TIMESPAN_POINTS" do
+ count = max * 2
+ sensor.timeline_within(
+ Time.at(@start_of_interval - 1),
+ Time.at(@start_of_interval + count * interval)
+ ).size.should < max
+ end
+
+ it "should not skip any points when timeline orginal size is less then MAX_TIMESPAN_POINTS" do
+ count = max - 1
+ sensor.timeline_within(
+ Time.at(@start_of_interval - 1),
+ Time.at(@start_of_interval + count * interval)
+ ).size.should == count
+ end
+
+ it "should give full data in case skip_optimization parameter set to true" do
+ count = max * 2
+ sensor.timeline_within(
+ Time.at(@start_of_interval - 1),
+ Time.at(@start_of_interval + count * interval),
+ true
+ ).size.should == count
end
end
end

0 comments on commit 8abc5c2

Please sign in to comment.