Module: Bucketize

Defined in:
lib/ce-bucketize.rb,
lib/ce-bucketize/version.rb,
lib/ce-bucketize/bucketizer.rb,
lib/ce-bucketize/daily_data.rb,
lib/ce-bucketize/hourly_values.rb,
lib/ce-bucketize/model/hour_cost.rb,
lib/ce-bucketize/model/hour_value.rb,
lib/ce-bucketize/model/tariff_rule.rb,
lib/ce-bucketize/tariffed_hourly_costs.rb,
lib/ce-bucketize/tariffed_hourly_values.rb

Overview

Copyright © 2015 TopCoder Inc., All Rights Reserved.

Defined Under Namespace

Classes: Bucketizer, DailyData, HourCost, HourValue, HourlyValues, InvalidTariffError, NoMatchedTariffError, TariffRule, TariffedHourlyCosts, TariffedHourlyValues, TooManyDataMissingError

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.config(options = {}) ⇒ Object

Public: configures the Bucketize module. It must be called before usage.

For example:

Bucketize.config(reg_access_token: "your access token"
                   application_information_url: "http://app_info_url")

Returns nothing.



279
280
281
# File 'lib/ce-bucketize.rb', line 279

def self.config(options = {})
  GreenButton.config(options)
end

.consumption_by_date(date, download_options) ⇒ Object

The consumption is calculated by summing the consumption data in totals of hours groups and summed to daily values.

date - a Date representing the day to which the data will

be aggregated, it must be Time object referring to the first hour
of the required day (required).

download_options - options used to download GreenButton data

Supported download options:

use_ftp         -   a Boolean flag to switch between ftp and API modes.
API (HTTP) options
  access_token  -   the OAuth2 token used by the GreenButton gem to
                    authenticate to the GreenButton custodians.
  subscription_url  - the url used by the GreenButton gem to retrieve the data
FTP options
  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.

Returns an array in the form [=> value, date2 => value]



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ce-bucketize.rb', line 87

def self.consumption_by_date(date, download_options)
  check_first_hour(date)

  first_hour = date
  last_hour = DateTime.new(date.year, date.month, date.day, -1, -1, -1,
                           first_hour.gmt_offset)
  last_hour = Time.new(last_hour.year, last_hour.month, last_hour.day,
                       last_hour.hour, last_hour.min, last_hour.sec,
                       first_hour.gmt_offset) + 1

  Bucketize::Bucketizer
      .new(download_options, first_hour, last_hour).daily_consumption_values
end

.consumption_date_range(date1, date2, download_options) ⇒ Object

daily consumption should be calculated by summing the consumption data into totals for the hour groups. These hour groups are summed to daily values.

date1 - a Time representing the first hour of the start day of the

range to which the data will be aggregated.
It must be in the timezone of the consumer (required).

date2 - a Time representing the last second of the end day of the range

to which the data will be aggregated. (required).
It must be in the timezone of the consumer (required).

download_options - options used to download GreenButton data

Supported download options:

use_ftp         -   a Boolean flag to switch between ftp and API modes.
API (HTTP) options
  access_token  -   the OAuth2 token used by the GreenButton gem to
                    authenticate to the GreenButton custodians.
  subscription_url  - the url used by the GreenButton gem to retrieve the data
FTP options
  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.

Returns an array in the form [=> value, date2 => value]



128
129
130
131
132
133
134
135
136
137
# File 'lib/ce-bucketize.rb', line 128

def self.consumption_date_range(date1, date2, download_options)
  check_first_hour(date1)
  check_last_second(date2)

  first_day = date1
  last_day = date2 + 1

  Bucketize::Bucketizer
      .new(download_options, first_day, last_day).daily_consumption_values
end

.consumption_from_month_start(month, download_options) ⇒ Object

The consumption intervals should be summed by the hours of the day

and summed to daily values.

month - a Time representing the first hour of the first day in the month

to which the data will be aggregated. (required)

download_options - options used to download GreenButton data

Supported download options:

use_ftp         -   a Boolean flag to switch between ftp and API modes.
API (HTTP) options
  access_token  -   the OAuth2 token used by the GreenButton gem to
                    authenticate to the GreenButton custodians.
  subscription_url  - the url used by the GreenButton gem to retrieve the data
FTP options
  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.

Returns an array in the form [=> value, date2 => value]

Raises ArgumentError if month is not the first hour of first day of a month.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ce-bucketize.rb', line 48

def self.consumption_from_month_start(month, download_options)
  check_first_day_of_month(month)

  first_day = month
  last_day = DateTime.new(month.year, month.month, -1, -1, -1, -1,
                          first_day.gmt_offset)
  # convert to time with the same zone offset of the first day
  last_day = Time.new(last_day.year, last_day.month, last_day.day,
                          last_day.hour, last_day.min, last_day.sec,
                          first_day.gmt_offset) + 1
  Bucketize::Bucketizer
      .new(download_options, first_day, last_day)
      .daily_consumption_values
end

.cost_by_date(date, tariff, download_options) ⇒ Object

The consumption is calculated by summing the consumption data in totals of hours groups and summed to daily values. Daily values are multiplied with the appropriate tariff for the day of the week.

date - a Date representing the day to which the data will

be aggregated, it must be Time object referring to the first hour
of the required day (required).

tariff - The tariff rules (json string).

download_options - options used to download GreenButton data

Supported download options:

use_ftp         -   a Boolean flag to switch between ftp and API modes.
API (HTTP) options
  access_token  -   the OAuth2 token used by the GreenButton gem to
                    authenticate to the GreenButton custodians.
  subscription_url  - the url used by the GreenButton gem to retrieve the data
FTP options
  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.

Returns an array in the form [=> cost, date2 => cost]



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/ce-bucketize.rb', line 212

def self.cost_by_date(date, tariff, download_options)
  check_first_hour(date)

  first_hour = date
  last_hour = DateTime.new(date.year, date.month, date.day, -1, -1, -1,
                           first_hour.gmt_offset)
  last_hour = Time.new(last_hour.year, last_hour.month, last_hour.day,
                       last_hour.hour, last_hour.min, last_hour.sec,
                       first_hour.gmt_offset) + 1
  tariff_rules = JSON.parse(tariff, :object_class => Bucketize::TariffRule)

  Bucketize::Bucketizer
      .new(download_options, first_hour, last_hour)
      .daily_consumption_costs(tariff_rules)
end

.cost_date_range(date1, date2, tariff, download_options) ⇒ Object

Daily cost should be calculated by summing the consumption data into totals for the hour groups, and using the appropriate tariff based on the day of the week and hour group.

date1 - a Time representing the first hour of the start day of the

range to which the data will be aggregated.
It must be in the timezone of the consumer (required).

date2 - a Time representing the last second of the end day of the range

to which the data will be aggregated. (required).
It must be in the timezone of the consumer (required).

tariff - The tariff rules (json string).

download_options - options used to download GreenButton data

Supported download options:

use_ftp         -   a Boolean flag to switch between ftp and API modes.
API (HTTP) options
  access_token  -   the OAuth2 token used by the GreenButton gem to
                    authenticate to the GreenButton custodians.
  subscription_url  - the url used by the GreenButton gem to retrieve the data
FTP options
  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.

Returns an array in the form [=> cost, date2 => cost]



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/ce-bucketize.rb', line 257

def self.cost_date_range(date1, date2, tariff, download_options)
  check_first_hour(date1)
  check_last_second(date2)

  first_day = date1
  last_day = date2 + 1

  tariff_rules = JSON.parse(tariff, :object_class => Bucketize::TariffRule)

  Bucketize::Bucketizer
      .new(download_options, first_day, last_day)
      .daily_consumption_costs(tariff_rules)
end

.cost_from_month_start(month, tariff, download_options) ⇒ Object

The consumption intervals should be summed by the hours of the day relevant to the tariff, and summed to daily values. Daily consumption values are multiplied with the appropriate tariff for the day of the week to get costs. Tariff selection varies depending on the consumption total since when the beginning of the month falls

month - a Time representing the first hour of the first day in the month

to which the data will be aggregated. (required)

tariff - The tariff rules (json string).

download_options - options used to download GreenButton data

Supported download options:

use_ftp         -   a Boolean flag to switch between ftp and API modes.
API (HTTP) options
  access_token  -   the OAuth2 token used by the GreenButton gem to
                    authenticate to the GreenButton custodians.
  subscription_url  - the url used by the GreenButton gem to retrieve the data
FTP options
  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.

Returns an array in the form [=> cost, date2 => cost]



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ce-bucketize.rb', line 168

def self.cost_from_month_start(month, tariff, download_options)
  check_first_day_of_month(month)

  first_day = month
  last_day = DateTime.new(month.year, month.month, -1, -1, -1, -1,
                          first_day.gmt_offset)
  # convert to time with the same zone offset of the first day
  last_day = Time.new(last_day.year, last_day.month, last_day.day,
                      last_day.hour, last_day.min, last_day.sec,
                      first_day.gmt_offset) + 1
  tariff_rules = JSON.parse(tariff, :object_class => Bucketize::TariffRule)

  Bucketize::Bucketizer
      .new(download_options, first_day, last_day)
      .daily_consumption_costs(tariff_rules)
end