Created
April 18, 2017 13:44
-
-
Save defp/251753932eff9afebacc435fa49d884b to your computer and use it in GitHub Desktop.
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
require 'csv' | |
headers = [:pid, :total_price, :nickname, :last_online_at, :created_at] | |
orders = {} | |
Order.where(app_id: 2, status: Order.statuses['shipped']).find_each(batch_size: 2000) do |o| | |
profile = o.game_profile | |
pid = o.game_profile_internal_key | |
puts pid | |
if orders[pid] | |
orders[pid][:total_price] = orders[pid][:total_price] + o.total_price | |
else | |
orders[pid] = { | |
pid: o.game_profile_internal_key, | |
total_price: o.total_price, | |
nickname: profile.try(:nickname), | |
last_online_at: profile.try(:last_online_at), | |
created_at: profile.try(:created_at) | |
} | |
end | |
end | |
puts "##########{orders.size}" | |
CSV.open('data.csv', 'w', write_headers: true, headers: headers) do |csv| | |
orders.each do |k, o| | |
csv << [o[:pid], o[:total_price], o[:nickname], o[:last_online_at], o[:created_at]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment