Class: Bucketize::Bucketizer
- Inherits:
-
Object
- Object
- Bucketize::Bucketizer
- Defined in:
- lib/ce-bucketize/bucketizer.rb
Overview
This class is responsible for executing an aggregation method for the Bucketize module. It orchestras some other helper classes to perform the required aggregation.
Author: ahmed.seddiq Version: 1.0
Instance Method Summary collapse
-
#daily_consumption_costs(tariff_rules) ⇒ Object
Fetches and aggregates the data based on the given array of TariffRules.
-
#daily_consumption_values ⇒ Object
Fetches and aggregates the consumption data to daily usage values.
-
#daily_data(hourly_data, aggregation_method) ⇒ Object
Internal: aggregates the given hourly data to a daily data.
-
#initialize(download_options, from = nil, to = nil) ⇒ Bucketizer
constructor
Initializes the Bucketizer with the given arguments.
Constructor Details
#initialize(download_options, from = nil, to = nil) ⇒ Bucketizer
Initializes the Bucketizer with the given arguments.
download_options - options used to download GreenButton data from - represents the start Date for required aggregation to - represents the end date for required aggregation
supported download_options
use_ftp - a Boolean flag to switch between ftp and API modes.
API (HTTP)
subscription_url - the GreenButton subscription url.
access_token - the GreenButton OAuth2 access_token.
FTP
application_id - represents the GreenButton 3rd party application id.
time - used to construct file name. (optional, defaults
to current time)
utility_name - represents the utility name, used to construct the
XML file name.
Raises ArgumentError if download_options are not valid
35 36 37 38 39 40 |
# File 'lib/ce-bucketize/bucketizer.rb', line 35 def initialize(, from=nil, to=nil) = @from = from @to = to end |
Instance Method Details
#daily_consumption_costs(tariff_rules) ⇒ Object
Fetches and aggregates the data based on the given array of TariffRules
tariff_rules - an Array of TariffRule to be applied to the GreenButton
data for this Bucketizer.
Returns a collection of [gb_data_description, date, cost]
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ce-bucketize/bucketizer.rb', line 61 def daily_consumption_costs(tariff_rules) # Validate tariff rules tariff_rules.each { |rule| rule.validate } gb = download_data(@from) gb.map do |gb_data_description| hourly_values = Bucketize::HourlyValues.new(gb_data_description, @from, @to) tariffed_hourly_costs = Bucketize::TariffedHourlyCosts.new(hourly_values, tariff_rules) daily_data(tariffed_hourly_costs, :cost) end end |
#daily_consumption_values ⇒ Object
Fetches and aggregates the consumption data to daily usage values
Returns an array in the form [=> value, date2 => value]
45 46 47 48 49 50 51 52 |
# File 'lib/ce-bucketize/bucketizer.rb', line 45 def daily_consumption_values gb = download_data(@from) gb.map do |gb_data_description| hourly_values = Bucketize::HourlyValues.new(gb_data_description, @from, @to) daily_data(hourly_values, :value) end end |
#daily_data(hourly_data, aggregation_method) ⇒ Object
Internal: aggregates the given hourly data to a daily data.
hourly_data - the hourly data, an Enumerable aggregation_method - the getter method to be called for elements in the
hourly_data. this value will be summed for each day.
Returns a DailyData enumerable.
79 80 81 82 83 84 85 86 |
# File 'lib/ce-bucketize/bucketizer.rb', line 79 def daily_data(hourly_data, aggregation_method ) daily_data = Bucketize::DailyData.new(hourly_data, aggregation_method) # convert the daily_data to hash date => value daily_data.reduce({}) do |all, entry| all[entry[0]] = entry[1] all end end |