Class: ArAggregateByInterval::QueryRunner
- Inherits:
-
Object
- Object
- ArAggregateByInterval::QueryRunner
- Defined in:
- lib/ar_aggregate_by_interval/query_runner.rb
Constant Summary collapse
- VALID_HASH_ARGS =
{ aggregate_function: [String], # sum, count interval: [String], # daily, weekly, monthly group_by_column: [String], # i.e.: created_at from: [Date, DateTime, Time, ActiveSupport::TimeWithZone], to: [:optional, Date, DateTime, Time, ActiveSupport::TimeWithZone], aggregate_column: [:optional, String, NilClass] # required when using sum (as opposed to count) }
Instance Attribute Summary collapse
-
#values ⇒ Object
readonly
Returns the value of attribute values.
-
#values_and_dates ⇒ Object
readonly
Returns the value of attribute values_and_dates.
Instance Method Summary collapse
-
#initialize(ar_model, hash_args) ⇒ QueryRunner
constructor
A new instance of QueryRunner.
Constructor Details
#initialize(ar_model, hash_args) ⇒ QueryRunner
Returns a new instance of QueryRunner.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/ar_aggregate_by_interval/query_runner.rb', line 24 def initialize(ar_model, hash_args) validate_args!(hash_args) from = normalize_from(hash_args[:from], hash_args[:interval]) to = normalize_to(hash_args[:to] || Time.zone.try(:now) || Time.now, hash_args[:interval]) db_vendor_select_for_date_function = Utils.select_for_grouping_column(hash_args[:group_by_column])[hash_args[:interval]] ar_result = ar_model. select("#{hash_args[:aggregate_function]}(#{hash_args[:aggregate_column] || '*'}) as totalchunked__"). select("#{db_vendor_select_for_date_function} as datechunk__"). group('datechunk__'). where(["#{hash_args[:group_by_column]} >= ? and #{hash_args[:group_by_column]} <= ?", from, to]) # fill the gaps of the sql results agg_int = QueryResult.new({ ar_result: ar_result, ar_result_select_col_mapping: {'datechunk__' => 'totalchunked__'}, from: from, to: to, interval: hash_args[:interval] }) @values_and_dates = agg_int.values_and_dates @values = @values_and_dates.collect { |hash| hash[:value] } end |
Instance Attribute Details
#values ⇒ Object (readonly)
Returns the value of attribute values.
22 23 24 |
# File 'lib/ar_aggregate_by_interval/query_runner.rb', line 22 def values @values end |
#values_and_dates ⇒ Object (readonly)
Returns the value of attribute values_and_dates.
22 23 24 |
# File 'lib/ar_aggregate_by_interval/query_runner.rb', line 22 def values_and_dates @values_and_dates end |