Class: PeriodicCalculations::Query
- Inherits:
-
Object
- Object
- PeriodicCalculations::Query
- Defined in:
- lib/periodic_calculations/query.rb
Constant Summary collapse
- INTERVAL_UNIT =
[:day, :week, :month, :year]
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(relation, query_options) ⇒ Array<Array>
constructor
Builds a periodic operation query with PostgresSQL window functions.
- #to_sql ⇒ Object
Constructor Details
#initialize(relation, query_options) ⇒ Array<Array>
Builds a periodic operation query with PostgresSQL window functions
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/periodic_calculations/query.rb', line 13 def initialize(relation, ) @relation = relation @target_column = .target_column = . @operation = .operation.upcase @window_function = .cumulative ? "ORDER" : "PARTITION" @binds = { :unit => .interval_unit, :interval => "1 #{query_options.interval_unit.upcase}", :start => .window_start, :end => .window_end, :offset => "#{query_options.timezone_offset} seconds" } end |
Instance Method Details
#execute ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/periodic_calculations/query.rb', line 28 def execute ActiveRecord::Base.connection_pool.with_connection do |connection| connection.execute(sanitized_sql).map do |elem| [Date.parse(elem["frame"]).to_time, elem["result"].to_i] end end end |
#to_sql ⇒ Object
36 37 38 |
# File 'lib/periodic_calculations/query.rb', line 36 def to_sql sanitized_sql end |