Advertisement
aspeiro

Sync of user payments activity

Jan 24th, 2025 (edited)
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.70 KB | History | 0 0
  1. # Segmentor -> export users with activity by months
  2. require 'csv'
  3.  
  4. file_path = 'tmp/segmentor_user_payments_activity.csv'
  5. batch_size = 1000
  6. casino_name = 's5_css'
  7.  
  8. start_date = '2020-01-01'.to_date
  9. end_date = Date.current
  10. all_months = (start_date..end_date).map { |date| date.strftime('%Y-%m') }.uniq
  11.  
  12. headers = ['user_id'] + all_months
  13.  
  14. CSV.open(file_path, 'w', write_headers: true, headers: headers) do |csv|
  15.   User.where(cn: casino_name).where.not(ac: []).batch_size(batch_size).no_timeout.each do |user|
  16.     month_payments_data = Hash.new(0)
  17.  
  18.     user.activity.each do |ac|
  19.       next unless ac['date']
  20.  
  21.       month = Date.parse(ac['date']).strftime('%Y-%m')
  22.       month_payments_data[month] += ac['deposits_sum'].to_i + ac['cashouts_sum'].to_i
  23.     end
  24.  
  25.     row = all_months.map { |month| month_payments_data[month] }
  26.  
  27.     next if row.sum.zero?
  28.  
  29.     csv << [user.user_id] + row
  30.   end
  31. end
  32.  
  33. # Platform -> export users with inconsistencies by months
  34. file_path_to_read  = 'private/files/segmentor_user_payments_activity.csv'
  35. file_path_to_write = 'private/files/user_payments_activity_report.csv'
  36. batch_size = 1000
  37.  
  38. start_date = '2020-01-01'.to_date
  39. end_date = Date.current
  40. all_months = (start_date..end_date).map { |date| date.strftime('%Y-%m') }.uniq
  41.  
  42. headers = ['user_id'] + all_months
  43.  
  44. CSV.open(file_path_to_write, 'w', write_headers: true, headers: headers) do |csv|
  45.   CSV.foreach(file_path_to_read, headers: true).each_slice(batch_size) do |batch|
  46.     batch_data = Hash.new { |h, user_id| h[user_id] = {} }
  47.  
  48.     batch.each do |row|
  49.       next unless (user_id = row['user_id'])
  50.  
  51.       batch_data[user_id.to_s] = row.to_h.except('user_id')
  52.     end
  53.  
  54.     User.where(id: batch_data.keys).includes(:deposits, :cashouts).each do |user|
  55.       prepared_data = { 'user_id' => user.id }
  56.  
  57.       all_months.each do |month|
  58.         from = Date.strptime(month, '%Y-%m').beginning_of_month
  59.         to = Date.strptime(month, '%Y-%m').end_of_month
  60.  
  61.         deposits_sum = user.deposits.select do |p|
  62.           p.state == 'aasm_succeeded' && p.finished_at.present? && p.finished_at >= from && p.finished_at <= to
  63.         end.sum(&:amount_cents)
  64.  
  65.         cashouts_sum = user.cashouts.select do |p|
  66.           p.state == 'aasm_succeeded' && p.finished_at.present? && p.finished_at >= from && p.finished_at <= to
  67.         end.sum(&:amount_cents)
  68.  
  69.         payments_sum = deposits_sum + cashouts_sum
  70.         payments_sum_from_file = batch_data[user.id.to_s]&.[](month).to_i
  71.  
  72.         prepared_data[month] = payments_sum_from_file == payments_sum ? 'OK' : payments_sum
  73.       end
  74.  
  75.       next unless all_months.any? { |month| prepared_data[month] != 'OK' }
  76.  
  77.       csv << headers.map { |header| prepared_data[header] }
  78.     end
  79.   end
  80. end
  81.  
  82. # Platform -> sync of users payments activity
  83. file_path  = 'private/files/user_payments_activity_report.csv'
  84. batch_size = 1000
  85.  
  86. start_date = '2020-01-01'.to_date
  87. end_date = Date.current
  88. all_months = (start_date..end_date).map { |date| date.strftime('%Y-%m') }.uniq
  89.  
  90. CSV.foreach(file_path, headers: true).each_slice(batch_size) do |batch|
  91.   batch_data = Hash.new { |h, month| h[month] = [] }
  92.  
  93.   batch.each do |row|
  94.     next unless (user_id = row['user_id'])
  95.  
  96.     all_months.each do |month|
  97.       payments_sum = row[month]
  98.       batch_data[month] << user_id if payments_sum != 'OK'
  99.     end
  100.   end
  101.  
  102.   batch_data.each do |month, user_ids|
  103.     next if user_ids.empty?
  104.  
  105.     date = Date.strptime(month, '%Y-%m')
  106.     from = date.beginning_of_month
  107.     to = date.end_of_month >= Date.current ? 1.day.ago.to_date : date.end_of_month
  108.     ids = user_ids.uniq
  109.  
  110.     Segmentor::DateRangeExporters::PaymentsActivityExporter.call(date_from: from, date_to: to, ids: ids)
  111.   end
  112. end
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement