Created
July 30, 2013 18:34
-
-
Save cvd/6115543 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
desc 'backfill pre ass data' | |
task ass: :environment do | |
users = User.where{(created_at >= "2012-08-01".to_date) & (created_at <= "2012-09-10".to_date)} | |
.where(type: 'Users::K12Student').select(["users.*", "enrollments.id as enrollment_id"]) | |
.joins(enrollments: :cohort) | |
.includes(:bookmarks) | |
.where("cohorts.curriculum_id = ?", 5) | |
ids = Curriculum.fin_lit.activities.map(&:id) | |
@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; memo } | |
@file = File.open("bm_backfill.csv", "w") | |
def parse_bm(user, bm) | |
pages = bm.pages.select {|k, val| k.in?(@keys) && val.has_key?('data') } | |
if pages.any? | |
pages.each do |key, page| | |
data = page['data'] | |
if page['status'].to_s == '1' | |
begin | |
grade = (Float(data['correct']) / Float(data['total']) * 100).to_i | |
@file.puts CSV.generate_line([user.enrollment_id, bm.user_id, @activities[key].id, grade]) | |
rescue | |
end | |
end | |
end | |
end | |
end | |
begin | |
bar = RakeProgressbar.new(users.count) | |
@file.puts CSV.generate_line ['enrollment_id', 'user_id', 'activity_id', 'grade'] | |
users.each do |user| | |
user.bookmarks.where(activity_id: ids).find_each do |bookmark| | |
begin | |
parse_bm user, bookmark | |
rescue JSON::ParserError | |
#squelch | |
rescue Exception => e | |
puts "Problem BM: #{e.class}" | |
end | |
end | |
bar.inc | |
end | |
bar.finish | |
ensure | |
@file.close | |
end | |
end |
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' | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment