Skip to content

Instantly share code, notes, and snippets.

@SFEley
Last active June 7, 2016 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SFEley/456d186a87017c2cc66d533f17af5a70 to your computer and use it in GitHub Desktop.
Save SFEley/456d186a87017c2cc66d533f17af5a70 to your computer and use it in GitHub Desktop.
Update all customers with average_order_value
include RedisCache
updater = Customer.mongo_client['customers']
start = 5000
lastid, lastaov = nil, nil
while (aovs = redis.zrange 'average_order_values', start, start + 999, withscores: true).present?
redis.pipelined do
aovs.each do |id, aov|
lastid, lastaov = id, aov
updater.find_one_and_update({ _id: BSON::ObjectId.from_string(id) }, { '$set' => { average_order_value: aov } })
redis.zrem 'average_order_values', id
end
end
remaining = redis.zcard 'average_order_values'
puts "#{remaining} - #{lastid} - $#{lastaov}"
end
include RedisCache
count = 0
scores = []
Customer.where(:total_orders.gt => 0).exists(average_order_value: false).only(:total_orders, :total_revenue).each do |customer|
aov = customer.total_revenue.to_f / customer.total_orders.to_i
scores << [aov, customer.id]
count += 1
if count % 1000 == 0
puts count
redis.zadd 'average_order_values', scores
scores.clear
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment