Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Segmentor -> export users with activity by months
- require 'csv'
- file_path = 'tmp/segmentor_user_payments_activity.csv'
- batch_size = 1000
- casino_name = 's5_css'
- start_date = '2020-01-01'.to_date
- end_date = Date.current
- all_months = (start_date..end_date).map { |date| date.strftime('%Y-%m') }.uniq
- headers = ['user_id'] + all_months
- CSV.open(file_path, 'w', write_headers: true, headers: headers) do |csv|
- User.where(cn: casino_name).where.not(ac: []).batch_size(batch_size).no_timeout.each do |user|
- month_payments_data = Hash.new(0)
- user.activity.each do |ac|
- next unless ac['date']
- month = Date.parse(ac['date']).strftime('%Y-%m')
- month_payments_data[month] += ac['deposits_sum'].to_i + ac['cashouts_sum'].to_i
- end
- row = all_months.map { |month| month_payments_data[month] }
- next if row.sum.zero?
- csv << [user.user_id] + row
- end
- end
- # Platform -> export users with inconsistencies by months
- file_path_to_read = 'private/files/segmentor_user_payments_activity.csv'
- file_path_to_write = 'private/files/user_payments_activity_report.csv'
- batch_size = 1000
- start_date = '2020-01-01'.to_date
- end_date = Date.current
- all_months = (start_date..end_date).map { |date| date.strftime('%Y-%m') }.uniq
- headers = ['user_id'] + all_months
- CSV.open(file_path_to_write, 'w', write_headers: true, headers: headers) do |csv|
- CSV.foreach(file_path_to_read, headers: true).each_slice(batch_size) do |batch|
- batch_data = Hash.new { |h, user_id| h[user_id] = {} }
- batch.each do |row|
- next unless (user_id = row['user_id'])
- batch_data[user_id.to_s] = row.to_h.except('user_id')
- end
- User.where(id: batch_data.keys).includes(:deposits, :cashouts).each do |user|
- prepared_data = { 'user_id' => user.id }
- all_months.each do |month|
- from = Date.strptime(month, '%Y-%m').beginning_of_month
- to = Date.strptime(month, '%Y-%m').end_of_month
- deposits_sum = user.deposits.select do |p|
- p.state == 'aasm_succeeded' && p.finished_at.present? && p.finished_at >= from && p.finished_at <= to
- end.sum(&:amount_cents)
- cashouts_sum = user.cashouts.select do |p|
- p.state == 'aasm_succeeded' && p.finished_at.present? && p.finished_at >= from && p.finished_at <= to
- end.sum(&:amount_cents)
- payments_sum = deposits_sum + cashouts_sum
- payments_sum_from_file = batch_data[user.id.to_s]&.[](month).to_i
- prepared_data[month] = payments_sum_from_file == payments_sum ? 'OK' : payments_sum
- end
- next unless all_months.any? { |month| prepared_data[month] != 'OK' }
- csv << headers.map { |header| prepared_data[header] }
- end
- end
- end
- # Platform -> sync of users payments activity
- file_path = 'private/files/user_payments_activity_report.csv'
- batch_size = 1000
- start_date = '2020-01-01'.to_date
- end_date = Date.current
- all_months = (start_date..end_date).map { |date| date.strftime('%Y-%m') }.uniq
- CSV.foreach(file_path, headers: true).each_slice(batch_size) do |batch|
- batch_data = Hash.new { |h, month| h[month] = [] }
- batch.each do |row|
- next unless (user_id = row['user_id'])
- all_months.each do |month|
- payments_sum = row[month]
- batch_data[month] << user_id if payments_sum != 'OK'
- end
- end
- batch_data.each do |month, user_ids|
- next if user_ids.empty?
- date = Date.strptime(month, '%Y-%m')
- from = date.beginning_of_month
- to = date.end_of_month >= Date.current ? 1.day.ago.to_date : date.end_of_month
- ids = user_ids.uniq
- Segmentor::DateRangeExporters::PaymentsActivityExporter.call(date_from: from, date_to: to, ids: ids)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement