Class: ArAggregateByInterval::QueryRunner

Inherits:
Object
  • Object
show all
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: [Symbol], # i.e.: :created_at

  from: [Date, DateTime, Time, ActiveSupport::TimeWithZone],
  to: [:optional, Date, DateTime, Time, ActiveSupport::TimeWithZone],

  aggregate_column: [:optional, Symbol, NilClass], # required when using sum (as opposed to count)

  normalize_dates: [:optional, TrueClass, FalseClass]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ar_model, hash_args) ⇒ QueryRunner

Returns a new instance of QueryRunner.



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
# File 'lib/ar_aggregate_by_interval/query_runner.rb', line 26

def initialize(ar_model, hash_args)

  validate_args!(hash_args)

  @ar_model = ar_model

  @from = hash_args[:from]
  @to = hash_args[:to] || Time.zone.try(:now) || Time.now

  # by default, change dates to beginning and end of interval
  # e.g. beginning and end of {day,week,month}
  if hash_args[:normalize_dates] != false
    @from = normalize_from(@from, hash_args[:interval])
    @to = normalize_to(@to, hash_args[:interval])
  end

  @db_vendor_select =
    Utils.select_for_grouping_column(hash_args[:group_by_column])[hash_args[:interval]]

  @aggregate_function = hash_args[:aggregate_function]
  @aggregate_column = hash_args[:aggregate_column]
  @group_by_column = hash_args[:group_by_column]

  @interval = hash_args[:interval]
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



24
25
26
# File 'lib/ar_aggregate_by_interval/query_runner.rb', line 24

def from
  @from
end

#intervalObject (readonly)

Returns the value of attribute interval.



24
25
26
# File 'lib/ar_aggregate_by_interval/query_runner.rb', line 24

def interval
  @interval
end

#toObject (readonly)

Returns the value of attribute to.



24
25
26
# File 'lib/ar_aggregate_by_interval/query_runner.rb', line 24

def to
  @to
end

#valuesObject (readonly)

Returns the value of attribute values.



24
25
26
# File 'lib/ar_aggregate_by_interval/query_runner.rb', line 24

def values
  @values
end

#values_and_datesObject (readonly)

Returns the value of attribute values_and_dates.



24
25
26
# File 'lib/ar_aggregate_by_interval/query_runner.rb', line 24

def values_and_dates
  @values_and_dates
end

Instance Method Details

#run_queryObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ar_aggregate_by_interval/query_runner.rb', line 52

def run_query
  # actually run query
  array_of_pairs = ActiveRecord::Base.connection.select_rows(to_sql)

  # workaround ActiveRecord's automatic casting to Date objects
  # (ideally we could return raw values straight from ActiveRecord to avoid this expensive N)
  array_of_pairs.collect! do |date_val_pair|
    date_val_pair.collect(&:to_s)
  end

  # convert the array of key/values to a hash
  Hash[array_of_pairs]
end