Skip to content

Instantly share code, notes, and snippets.

@da4nik
Created July 20, 2017 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save da4nik/a63e5b50ddf02038fbd3cc365ca3fe5f to your computer and use it in GitHub Desktop.
Save da4nik/a63e5b50ddf02038fbd3cc365ca3fe5f to your computer and use it in GitHub Desktop.
namespace :app do
desc 'Export survey to json'
task export_survey: :environment do
if ENV['SURVEY_ID'].blank?
puts 'You need to provide survey_id in SURVEY_ID env var'
return
end
survey = Surveys::Survey.find_by_obfuscated_id ENV["SURVEY_ID"]
res = {
id: survey.id,
title: survey.title,
description: survey.description,
}
res[:questions] = survey.questions.map do |q|
choices = q.question_choices.map do |c|
{
text: c.text,
icon_id: c.icon_id,
weight: c.weight,
position: c.position
}
end
{
text: q.text,
title: q.title,
question_type: q.question_type,
question_view: q.question_view,
start_value: q.start_value,
end_value: q.end_value,
start_label: q.start_label,
end_label: q.end_label,
default_label: q.default_label,
labels: q.labels,
choices: choices
}
end
File.open("survey.json", "w") do |f|
f.write(res.to_json)
end
end
desc 'Import survey to json'
task import_survey: :environment do
surv = JSON.parse(File.read('survey.json'))
org = ClinicianGroup.find 11
clinician = Clinician.find 1150
survey = Surveys::Survey.create! clinician_group: org,
creator: clinician,
title: surv['title'],
description: surv['description']
surv['questions'].each do |quest|
question = Surveys::Question.create! clinician_group: org,
creator: clinician,
last_updater: clinician,
title: quest['title'],
text: quest['text'],
question_type: quest['question_type'],
question_view: quest['question_view'],
start_value: quest['start_value'],
end_value: quest['end_value'],
start_label: quest['start_label'],
end_label: quest['end_label'],
default_label: quest['default_label'],
labels: quest['labels']
survey.questions << question
quest['choices'].each do |ch|
Surveys::QuestionChoice.create! question: question,
text: ch['text'],
icon_id: ch['icon_id'],
weight: ch['weight'],
position: ch['position']
end
end
puts 'DONE !!!!'
end
namespace :initial do
desc 'Set payment trial period for everyone'
task trial_period_for_offices: :environment do
Office.where(is_test_group: false).update_all trial_period_to: Time.now + configatron.payments.subscription_trial_period
end
desc 'Reset TOS acceptance'
task reset_tos_acceptance: :environment do
Agreement.terms_of_service.delete_all
end
desc 'Assign default pricing plan to every office'
task assign_default_pricing_plan: :environment do
default_pp = Payments::PricingPlan.where(is_default: true).first
if default_pp.blank?
puts 'Unable to find default pricing plan. Please set up one.'
return
end
Office.update_all pricing_plan_id: default_pp.id
end
end
desc 'Update stripe emails and description fields for offices'
task update_stripe_customers: :environment do
NUM_THREADS = 4
queue = Queue.new
threads = []
NUM_THREADS.times do
threads << Thread.new do
while true
office_id = queue.pop
break if office_id.nil?
Office.find(office_id).try(:update_stripe_customer)
end
end
end
Office.real.ids.each {|office_id| queue << office_id }
NUM_THREADS.times { queue << nil }
threads.each {|thread| thread.join }
end
desc 'Download media files of all patients from certain office (OFFICE_ID env variable)'
task export_office_media: :environment do
office_id = ENV['OFFICE_ID']
office = Office.find_by_obfuscated_id(office_id) ||
Office.find_by(id: office_id)
if office.blank?
puts "Office with id=#{ ENV['OFFICE_ID'] } was not found.".red
exit
end
def clinician_proofs(clinician, office, options = {})
# if clinician is not clinician, return nothing
unless clinician.clinician?
Proof.none
end
caring_proof_ids = clinician.created_proofs.ids
clinician_proof_ids = Proof.where(creator_type: ['Clinician', 'User'])
.where(creator_id: clinician.id).ids
shareable_pr_ids = SharingLink.where(shareable_type: 'Proof',
shareable_part: :content,
receiver: clinician)
.uniq
.pluck(:shareable_id)
office_shared_ids = []
if office.present?
office_shared_ids = SharingLink.where(shareable_type: 'Proof',
shareable_part: :content,
receiver: office)
.uniq
.pluck(:shareable_id)
end
proof_ids = (clinician_proof_ids + caring_proof_ids + shareable_pr_ids + office_shared_ids).flatten.uniq
if options[:ids].present?
proof_ids
else
Proof.where(id: proof_ids)
end
end
clinicians = office.clinicians
proof_ids = clinicians.map {|c| clinician_proofs(c, office, ids: true) }
.flatten
.uniq
proofs = Proof.where(id: proof_ids, media_format: parse_proof_types)
export_proofs proofs, "office_media_#{ office_id }"
end
desc "Export patient's media (PATIENT_ID env variable)"
task export_patient_media: :environment do
patient_id = ENV['PATIENT_ID']
patient = Patient.find_by_obfuscated_id(patient_id) ||
Patient.find_by(id: patient_id)
if patient.blank?
puts "Patent with id=#{ patient_id }, not found."
exit
end
export_proofs patient.proofs.where(media_format: parse_proof_types), "patient_media_#{ patient_id }"
end
desc "export users's hipaa library (USER_ID env variable)"
task export_hipaa_media: :environment do
user_id = ENV['USER_ID']
user = User.find_by_obfuscated_id(user_id) ||
User.find_by(id: user_id)
if user.blank?
puts "User with id=#{ user_id } not found."
exit
end
base_path = File.join(Rails.root, 'tmp', "hipaa_media_#{ user_id }")
puts "#{ user.library_items.size } hipaa library items found."
user.library_items.each do |li|
mf = li.media_files.original.first
next if mf.blank?
hipaa_path = File.join(base_path, "item_#{ mf.id }")
export_media_file mf, hipaa_path
end
end
private
def parse_proof_types
ENV['TYPES'].try(:split, ',') || ['photo', 'video', 'report']
end
# base_path - relative path Rails.root + 'tmp' + base_path
def export_proofs(proofs = [], base_path = 'proof_media_export')
return if proofs.blank? || proofs.size == 0
base_path = File.join(Rails.root, 'tmp', base_path)
FileUtils::mkdir_p base_path
puts "Found #{ proofs.size } proof(s) available for export."
index = 0
proofs.each do |proof|
index += 1
puts "[#{index}/#{proofs.size}] Processing proof #{proof.id} / #{ proof.obfuscated_id }"
proof_path = File.join(base_path, "/proof_#{ proof.id }")
proof.captures.each do |capture|
mf = capture.media_files.original.first
next if mf.blank?
capture_path = File.join(proof_path, "/capture_#{capture.id}")
export_media_file mf, capture_path
end
end
end
# path - absolute path to resulting file
def export_media_file(mf, path)
return if mf.blank?
mf.cache! unless File.exist?(mf.local_cache_path)
filename = File.basename(mf.local_cache_path)
FileUtils::mkdir_p path
FileUtils.cp mf.local_cache_path,
File.join(path, filename)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment