Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def clinic_data(admin_id)
- return [] if admin_id.presence.nil?
- return [] if admin_id.to_i == 0
- admin = ::Health::ClinicAdmin.find(admin_id)
- patients = admin.patients
- practitioner_ids = admin.practitioners.ids + [admin_id]
- row = [patients.size]
- ages = { '<6' => 0, '6-11' => 0, '12-17' =>0, '18-64' => 0, '>65' => 0 }
- patients.find_each do |patient|
- age = patient.details.age
- age_key = if age < 6
- '<6'
- elsif age.in?(6..11)
- '6-11'
- elsif age.in?(12..17)
- '12-17'
- elsif age.in?(18..64)
- '18-64'
- else age >= 65
- '>65'
- end
- ages[age_key] = ages[age_key] + 1
- end
- row += ages.values
- trials = admin.trials
- batches = Batch.where(trial_id: trials.ids)
- c_batches_ids = batches.assessment_type_cognitive.ids
- q_batched_ids = batches.assessment_type_questionnaire.ids
- c_report_count = BatchSession.status_complete.where(batch_id: c_batches_ids, cfp: false).select(:id).count
- q_report_count = BatchSession.status_complete.where(batch_id: q_batched_ids, cfp: false).select(:id).count
- protocols = ::Health::Protocol.where(owner_id: practitioner_ids)
- protocol_only_c_ids = []
- protocol_only_q_ids = []
- protocol_c_and_q_ids = []
- protocols.find_each do |protocol|
- assessments = protocol.assessments
- if assessments.assessment_type_questionnaire.any? && assessments.assessment_type_cognitive.any?
- protocol_c_and_q_ids << protocol.id
- elsif assessments.assessment_type_cognitive.any?
- protocol_only_c_ids << protocol.id
- elsif assessments.assessment_type_questionnaire.any?
- protocol_only_q_ids << protocol.id
- end
- end
- completed_c_protocol_sessions = ::Health::ProtocolSession.status_complete.where(protocol_id: protocol_only_c_ids).count
- completed_q_protocol_sessions = ::Health::ProtocolSession.status_complete.where(protocol_id: protocol_only_q_ids).count
- completed_c_and_q_protocol_sessions = ::Health::ProtocolSession.status_complete.where(protocol_id: protocol_c_and_q_ids).count
- row += [c_report_count, q_report_count]
- row += [completed_c_protocol_sessions, completed_c_and_q_protocol_sessions, completed_q_protocol_sessions]
- row
- end
- titles = 'Admin User ID (provided)
- Account name (provided)
- Vertical (provided)
- Total number of patients created
- Number of patients created below 6 years of age
- Number of patients created between 6 to 11 years of age
- Number of patients created older than 11 years and younger than 18 years
- Number of patients created ages 18 years and older and younger than 65 years
- Number of patients created ages 65 years and older
- Number of completed cognitive assessments
- Number of completed questionnaire assessments
- Number of completed protocols with just cognitive tasks
- Number of completed protocols with cognitive tasks and questionnaires
- Number of completed protocols with just questionnaires
- '.split("\n")
- mf = MediaFile.find_by(name: 'CS-1367')
- rows = CSV.parse(mf.content, headers: true)
- p_bar = ProgressBar.new(rows.size)
- processed_ids = []
- csv_file = "tmp/CS-1367.csv"
- if File.exist?(csv_file)
- CSV.foreach(csv_file, headers: true) { |row| processed_ids << "#{row['Admin User ID (provided)']} #{row['Account name (provided)']} #{row['Vertical (provided)']}" }
- end
- CSV.open(csv_file, 'a+') do |csv|
- if processed_ids.empty?
- csv << titles
- end
- rows.each do |row|
- value = "#{row["Admin User ID"]} #{row['Account Name']} #{row['Vertical']}"
- next if processed_ids.include?(value)
- admin_id = nil
- admin_id = row["Admin User ID"] if row["Admin User ID"].presence
- p_bar.puts(admin_id)
- csv_row = [admin_id, row['Account Name'], row['Vertical']] + clinic_data(admin_id)
- csv << csv_row
- csv.flush
- p_bar.puts(csv_row)
- p_bar.increment!
- end
- nil
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement