Module: Switchman::ActiveRecord::Calculations
- Defined in:
- lib/switchman/active_record/calculations.rb
Instance Method Summary collapse
- #calculate_simple_average(column_name, distinct) ⇒ Object
-
#execute_grouped_calculation(operation, column_name, distinct) ⇒ Object
See activerecord#execute_grouped_calculation.
- #execute_simple_calculation(operation, column_name, distinct) ⇒ Object
- #ids ⇒ Object
- #pluck(*column_names) ⇒ Object
Instance Method Details
#calculate_simple_average(column_name, distinct) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/switchman/active_record/calculations.rb', line 51 def calculate_simple_average(column_name, distinct) # See activerecord#execute_simple_calculation relation = except(:order) 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_switchman(r["average"], column_name, "average") r["count"] = type_cast_calculated_value_switchman(r["count"], column_name, "count") end result = initial_results.sum { |r| r["average"] * r["count"] } / initial_results.sum do |r| r["count"] end else result = type_cast_calculated_value_switchman(initial_results.first["average"], column_name, "average") end result end |
#execute_grouped_calculation(operation, column_name, distinct) ⇒ Object
See activerecord#execute_grouped_calculation
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/switchman/active_record/calculations.rb', line 74 def execute_grouped_calculation(operation, column_name, distinct) opts = (operation.to_s.downcase, column_name, distinct) relation = build_grouped_calculation_relation(opts) target_shard = Shard.current rows = relation.activate do |rel, shard| calculated_data = klass.connection.select_all(rel) if opts[:association] key_ids = calculated_data.collect { |row| row[opts[:group_aliases].first] } key_records = opts[:association].klass.base_class.where(id: key_ids) key_records = key_records.to_h { |r| [Shard.relative_id_for(r, shard, target_shard), r] } end calculated_data.map do |row| row[opts[:aggregate_alias]] = type_cast_calculated_value_switchman( row[opts[:aggregate_alias]], column_name, opts[:operation] ) row["count"] = row["count"].to_i if opts[:operation] == "average" opts[:group_columns].each do |aliaz, _type, group_column_name| if opts[:associated] && (aliaz == opts[:group_aliases].first) row[aliaz] = key_records[Shard.relative_id_for(row[aliaz], shard, target_shard)] elsif group_column_name && @klass.sharded_column?(group_column_name) row[aliaz] = Shard.relative_id_for(row[aliaz], shard, target_shard) end end row end end compact_grouped_calculation_rows(rows, opts) end |
#execute_simple_calculation(operation, column_name, distinct) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/switchman/active_record/calculations.rb', line 29 def execute_simple_calculation(operation, column_name, distinct) operation = operation.to_s.downcase if operation == "average" result = calculate_simple_average(column_name, distinct) else result = activate do |relation| relation.call_super(:execute_simple_calculation, Calculations, operation, column_name, distinct) end 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 |
#ids ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/switchman/active_record/calculations.rb', line 110 def ids return super unless klass.sharded_primary_key? if loaded? result = records.map do |record| Shard.relative_id_for(record._read_attribute(primary_key), record.shard, Shard.current(klass.connection_class_for_self)) end return @async ? Promise::Complete.new(result) : result end if has_include?(primary_key) relation = apply_join_dependency.group(primary_key) return relation.ids end columns = arel_columns([primary_key]) base_shard = Shard.current(klass.connection_class_for_self) activate do |r| relation = r.spawn relation.select_values = columns result = if relation.where_clause.contradiction? ::ActiveRecord::Result.empty else skip_query_cache_if_necessary do klass.connection.select_all(relation, "#{klass.name} Ids", async: @async) end end result.then do |res| type_cast_pluck_values(res, columns).map { |id| Shard.relative_id_for(id, Shard.current, base_shard) } end end end |
#pluck(*column_names) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/switchman/active_record/calculations.rb', line 6 def pluck(*column_names) target_shard = Shard.current(klass.connection_class_for_self) shard_count = 0 result = activate do |relation, shard| shard_count += 1 results = relation.call_super(:pluck, Calculations, *column_names) if column_names.length > 1 column_names.each_with_index do |column_name, idx| next unless klass.sharded_column?(column_name) results.each do |r| r[idx] = Shard.relative_id_for(r[idx], shard, target_shard) end end elsif klass.sharded_column?(column_names.first.to_s) results = results.map { |r| Shard.relative_id_for(r, shard, target_shard) } end results end result.uniq! if distinct_value && shard_count > 1 result end |