Last active
June 7, 2016 21:52
-
-
Save SFEley/456d186a87017c2cc66d533f17af5a70 to your computer and use it in GitHub Desktop.
Update all customers with average_order_value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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