Skip to content

Instantly share code, notes, and snippets.

@cvd
Created July 30, 2013 18:33
Show Gist options
  • Save cvd/6115532 to your computer and use it in GitHub Desktop.
Save cvd/6115532 to your computer and use it in GitHub Desktop.
require 'csv'
output = File.open('create_aps_flume.sql', 'w')
begin
CSV.foreach('bm_backfill.csv') do |row|
enrollment_id, user_id, activity_id, grade = row
sql = <<-SQL
INSERT INTO "activity_progresses" ("activity_id", "attempt", "created_at", "deleted_at", "enrollment_id", "grade", "passed", "status", "updated_at")
VALUES (#{activity_id}, 1, '2013-07-15 20:37:14', NULL, #{enrollment_id}, #{grade}, TRUE, NULL, '2013-07-15 20:37:14');
SQL
# results = ActiveRecord::Base.connection.execute sql
output.puts sql #results.as_json.first["id"]
end
ensure
output.close
end
require 'csv'
desc 'import scores from flume output'
task import_scores: :environment do
keys = ["aa010", "ba010", "ca010", "da010", "ia010", "ga010", "fa010", "ha010", "ea010"]
acts = Activity.where(curriculum_id: 5, assessment_key: keys)
activities = acts.inject({}) {|memo, act| memo[act.assessment_key] = act.id; memo }
outfile = File.open('flume_users_outfile.csv', 'w')
curr = Curriculum.fin_lit
%x[wc -l results-final.csv] =~ /(\d+)/
count = $1.to_i
bar = RakeProgressbar.new(count)
begin
CSV.foreach('results-final.csv', headers: false) do |row|
begin
user_id, ass_key, grade = row
next if user_id.blank?
activity_id = activities[ass_key]
u = User.find(user_id)
if u && enr = u.enrollment_for_curriculum(curr)
outfile.puts CSV.generate_line [enr.id, user_id, activity_id, grade]
end
bar.inc
rescue ActiveRecord::RecordNotFound
bar.inc
end
end
ensure
bar.finish
outfile.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment