Module: Switchman::ActiveRecord::Calculations
- Defined in:
- lib/switchman/active_record/calculations.rb
Class Method Summary collapse
Instance Method Summary collapse
- #calculate_average(column_name, distinct) ⇒ Object
-
#execute_simple_calculation_with_sharding(operation, column_name, distinct) ⇒ Object
TODO: grouped calculations.
- #pluck_with_sharding(column_name) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
4 5 6 7 8 |
# File 'lib/switchman/active_record/calculations.rb', line 4 def self.included(klass) %w{execute_simple_calculation pluck}.each do |method| klass.alias_method_chain(method, :sharding) end end |
Instance Method Details
#calculate_average(column_name, distinct) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/switchman/active_record/calculations.rb', line 42 def calculate_average(column_name, distinct) # See activerecord#execute_simple_calculation relation = reorder(nil) column = aggregate_column(column_name) relation.select_values = [operation_over_aggregate_column(column, "average", distinct).as("average"), operation_over_aggregate_column(column, "count", distinct).as("count")] initial_results = relation.activate{ |rel| @klass.connection.select_all(rel) } if initial_results.is_a?(Array) initial_results.each do |r| r["average"] = type_cast_calculated_value(r["average"], nil, "average") r["count"] = type_cast_calculated_value(r["count"], nil, "count") end result = initial_results.map{|r| r["average"] * r["count"]}.sum / initial_results.map{|r| r["count"]}.sum else result = type_cast_calculated_value(initial_results["average"], nil, "average") end result end |
#execute_simple_calculation_with_sharding(operation, column_name, distinct) ⇒ Object
TODO: grouped calculations
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/switchman/active_record/calculations.rb', line 22 def execute_simple_calculation_with_sharding(operation, column_name, distinct) operation = operation.to_s.downcase if operation == "average" result = calculate_average(column_name, distinct) else result = self.activate{ |relation| relation.send(:execute_simple_calculation_without_sharding, operation, column_name, distinct) } if result.is_a?(Array) case operation when "count", "sum" result = result.sum when "minimum" result = result.min when "maximum" result = result.max end end end result end |
#pluck_with_sharding(column_name) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/switchman/active_record/calculations.rb', line 10 def pluck_with_sharding(column_name) target_shard = Shard.current(klass.shard_category) self.activate do |relation, shard| results = relation.pluck_without_sharding(column_name) if klass.sharded_column?(column_name.to_s) results = results.map{|result| Shard.relative_id_for(result, shard, target_shard)} end results end end |