Class: Bucketize::HourlyValues
- Inherits:
-
Object
- Object
- Bucketize::HourlyValues
- Includes:
- Enumerable
- Defined in:
- lib/ce-bucketize/hourly_values.rb
Overview
An Enumerable for hourly consumption values. The values are aggregated from
the raw GreenButton data.
Author: ahmed.seddiq Version: 1.0
Constant Summary collapse
- HOUR =
hour in seconds
3600
Instance Attribute Summary collapse
-
#max_gap_length ⇒ Object
The length (in seconds) of the maximum allowed gap in input data.
-
#readings ⇒ Object
readonly
This is a readonly attribute that will hold the combined interpolated raw data.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
The “each” method required by the Enumerable mixin.
-
#initialize(gb_data_description, from = nil, to = nil) ⇒ HourlyValues
constructor
Creates a new instance of the HourlyValues collection.
Constructor Details
#initialize(gb_data_description, from = nil, to = nil) ⇒ HourlyValues
Creates a new instance of the HourlyValues collection.
gb_data_description - the input GreenButton data. from - the start Time, must represent an hour start. to - the end Time, must represent an hour start.
Raises ArgumentError if from or to don’t represent an hour start.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ce-bucketize/hourly_values.rb', line 34 def initialize(gb_data_description, from=nil, to=nil) unless from.nil? or (from.min == 0 and from.sec == 0) raise ArgumentError, '"from" should be an hour start' end unless to.nil? or (to.min == 0 and to.sec == 0) raise ArgumentError, '"to" should be an hour start' end @gb_data_description = gb_data_description @from = from @to = to @max_gap_length = 4 * HOUR @readings = prepare_readings end |
Instance Attribute Details
#max_gap_length ⇒ Object
The length (in seconds) of the maximum allowed gap in input data.
20 21 22 |
# File 'lib/ce-bucketize/hourly_values.rb', line 20 def max_gap_length @max_gap_length end |
#readings ⇒ Object (readonly)
This is a readonly attribute that will hold the combined interpolated raw data. It will be initialized in the constructor.
24 25 26 |
# File 'lib/ce-bucketize/hourly_values.rb', line 24 def readings @readings end |
Instance Method Details
#each(&block) ⇒ Object
The “each” method required by the Enumerable mixin. It yields the hourly consumption values for the GreenButton data associated with this instance.
Yields HourValue instances representing the hourly consumption of the data.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ce-bucketize/hourly_values.rb', line 54 def each(&block) return enum_for(:each) if block.nil? readings = @readings.each finished = false # will be set to true when raw data are all consumed. cv = 0 # will aggregate current hour value current_reading = readings.next t0 = current_reading.time_start # the start of the first reading d0 = current_reading.time_duration.to_f # duration of the first reading # last reading time. tn = @readings.last.time_start + @readings.last.time_duration - HOUR hour_start = @from || t0 # will point to the current hour start last_hour_start = ((@to - HOUR) unless @to.nil?) || tn # the last hour_start d = (t0 + d0 - hour_start).to_f # remaining seconds from the current reading. rem = (d/d0) * current_reading.value # remaining value from the current reading. hf = 1.to_f # remaining fraction of hour for the current hour value until finished do # ratio to be consumed from the current reading rat = [1.0, (hf * HOUR) / d].min # increment current value by the ratio from the current reading cv = cv + rat * rem # decrement consumed value from current reading rem = [0, rem * (1 - rat)].max # update remaining fraction by the consumed duration hf = [0, hf - ((rat * d)/ HOUR)].max # update remaining duration d = d * (1 - rat) if hf == 0.0 # a complete hour is calculated yield Bucketize::HourValue.new(hour_start, cv.round(2)) # reset the current hour value and fraction cv = 0 hf = 1.to_f hour_start += HOUR if hour_start > last_hour_start finished = true end end if rem == 0 # current reading is totally consumed. begin # get next reading current_reading = readings.next d = current_reading.time_duration.to_f rem = current_reading.value rescue StopIteration finished = true end end end end |