Created
August 28, 2015 14:58
-
-
Save marten/2dbf7c52e9bf52574e5c 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
require 'csv' | |
#prod | |
project = Project.find(593) | |
zoo_user = User.find(1) | |
subject_set = SubjectSet.find(681) | |
#staging | |
# project = Project.find(450) | |
# zoo_user = User.find(27) | |
# subject_set = SubjectSet.find(2725) | |
#cam's dev box | |
# subject_set = SubjectSet.find(2435) | |
if project && zoo_user && subject_set | |
rows = CSV.foreach("#{Rails.root}/hhmi-23k.csv", headers: true).to_a | |
rows.in_groups_of(500) do |group| | |
subjects = [] | |
failed_subjects = [] | |
group.each do |row| | |
header, external_src = row.delete("FileName") | |
ext = external_src.split(".").last | |
metadata = row.to_hash | |
content_type = case ext | |
when /jpg/i | |
"image/jpeg" | |
else | |
nil | |
end | |
if external_src.nil? || content_type.nil? || metadata.empty? | |
raise "No src / content type from file" | |
end | |
location = Medium.where(type: 'subject_location', external_link: true, src: external_src, content_type: content_type).first | |
if location | |
subject = location.linked | |
else | |
subject = Subject.new(project_id: project.id, upload_user_id: zoo_user.id, metadata: metadata) | |
location_params = { content_type: content_type, external_link: true, src: external_src, metadata: { index: 0 }} | |
subject.locations.build(location_params) | |
end | |
if subject.save | |
subjects << subject | |
else | |
failed_subjects << subject | |
end | |
end | |
#cleanup if failure | |
unless failed_subjects.empty? | |
# subjects.map(&:destroy) | |
puts "failed to create some fo the subjects..." | |
#check these here | |
binding.pry | |
end | |
#now link to the subject sets | |
subject_ids_to_link = SetMemberSubject.where(subject_set_id: subject_set.id).where.not(subject_id: subjects.map(&:id)).pluck(:subject_id) | |
if Subject.where(id: subject_ids_to_link).count != subject_ids_to_link.size | |
raise "Error: check the subject set and all the subjects exist." | |
end | |
new_sms_values = subject_ids_to_link.map do |subject_id| | |
[ subject_set.id, subject_id, rand ] | |
end | |
sset_import_cols = %w(subject_set_id subject_id random) | |
SetMemberSubject.import sset_import_cols, new_sms_values, validate: false | |
SubjectSet.reset_counters(subject_set.id, :set_member_subjects) | |
puts "Created #{subjects.size} subjects" | |
end | |
else | |
puts "fail - couldn't find a project, user or subject set to link to" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment