LifePex/init/import_lines.rb

39 lines
843 B
Ruby
Executable File

#!/usr/bin/env ruby
require "csv"
require "date"
require "sequel"
DB = Sequel.connect ARGV[0]
arr_of_rows = CSV.read(ARGV[1], headers: true)
arr_of_rows.each do |row|
pex_id = -1
name = row["Action"]
amount = row["Points"].to_i
count_by_date = row.map do |key, value|
begin
date = Date.parse(key)
count_at_date = value.to_i
[date, count_at_date]
rescue => err
nil
end
end.compact.to_h
existing_pex = DB[:pexs].where(name: name).all[0]
if existing_pex
pex_id = existing_pex[:id]
puts "found #{name}"
else
puts "added #{name}"
pex_id = DB[:pexs].insert(name: name, amount: amount)
end
count_by_date.each do |key, value|
value.times { DB[:user_pexs].insert(pex_id: pex_id, user_id: ARGV[2] || 1, created_at: key) }
puts "added #{key} #{value} times"
end
end