Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
#348457 timeline_within can give full timelined without optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
s.averyanov committed Jul 30, 2012
1 parent b240470 commit 8abc5c2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
42 changes: 22 additions & 20 deletions lib/pulse-meter/sensor/timeline.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -108,12 +108,17 @@ def timeline(time_ago)
# Returts sensor data within given time # Returts sensor data within given time
# @param from [Time] lower bound # @param from [Time] lower bound
# @param till [Time] upper bound # @param till [Time] upper bound
# @param skip_optimization [Boolean] must be set to true to skip interval optimization
# @return [Array<SensorData>] # @return [Array<SensorData>]
# @raise ArgumentError if argumets are not valid time objects # @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) raise ArgumentError unless from.kind_of?(Time) && till.kind_of?(Time)
start_time, end_time = from.to_i, till.to_i 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 current_interval_id = get_interval_id(start_time) + actual_interval
keys = [] keys = []
ids = [] ids = []
Expand All @@ -134,19 +139,6 @@ def timeline_within(from, till)
res res
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_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 # Returns sensor data for given interval making in-memory summarization
# and returns calculated value # and returns calculated value
# @param interval_id [Fixnum] # @param interval_id [Fixnum]
Expand Down Expand Up @@ -174,11 +166,7 @@ def drop_within(from, till)
keys << raw_data_key(current_interval_id) keys << raw_data_key(current_interval_id)
current_interval_id += interval current_interval_id += interval
end end
if keys.empty? keys.empty? ? 0 : redis.del(*keys)
0
else
redis.del(*keys)
end
end end


# Returns Redis key by which raw data for current interval is stored # Returns Redis key by which raw data for current interval is stored
Expand Down Expand Up @@ -255,6 +243,20 @@ def process_event(value = nil)
end end
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 end
end end
30 changes: 25 additions & 5 deletions spec/shared_examples/timeline_sensor.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -242,12 +242,32 @@ def inner_data(obj)
end end
end end


it "should not return more than #{PulseMeter::Sensor::Timeline::MAX_TIMESPAN_POINTS} points" do context "to avoid getting to much data" do
max = PulseMeter::Sensor::Timeline::MAX_TIMESPAN_POINTS let(: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


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 end
end end
Expand Down

0 comments on commit 8abc5c2

Please sign in to comment.