Advertisement
andrey_zavyalov

Untitled

Feb 22nd, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. def clinic_data(admin_id)
  2. return [] if admin_id.presence.nil?
  3. return [] if admin_id.to_i == 0
  4.  
  5. admin = ::Health::ClinicAdmin.find(admin_id)
  6. patients = admin.patients
  7. practitioner_ids = admin.practitioners.ids + [admin_id]
  8. row = [patients.size]
  9.  
  10. ages = { '<6' => 0, '6-11' => 0, '12-17' =>0, '18-64' => 0, '>65' => 0 }
  11. patients.find_each do |patient|
  12. age = patient.details.age
  13. age_key = if age < 6
  14. '<6'
  15. elsif age.in?(6..11)
  16. '6-11'
  17. elsif age.in?(12..17)
  18. '12-17'
  19. elsif age.in?(18..64)
  20. '18-64'
  21. else age >= 65
  22. '>65'
  23. end
  24.  
  25. ages[age_key] = ages[age_key] + 1
  26. end
  27. row += ages.values
  28.  
  29. trials = admin.trials
  30. batches = Batch.where(trial_id: trials.ids)
  31. c_batches_ids = batches.assessment_type_cognitive.ids
  32. q_batched_ids = batches.assessment_type_questionnaire.ids
  33.  
  34. c_report_count = BatchSession.status_complete.where(batch_id: c_batches_ids, cfp: false).select(:id).count
  35. q_report_count = BatchSession.status_complete.where(batch_id: q_batched_ids, cfp: false).select(:id).count
  36.  
  37. protocols = ::Health::Protocol.where(owner_id: practitioner_ids)
  38.  
  39. protocol_only_c_ids = []
  40. protocol_only_q_ids = []
  41. protocol_c_and_q_ids = []
  42. protocols.find_each do |protocol|
  43. assessments = protocol.assessments
  44. if assessments.assessment_type_questionnaire.any? && assessments.assessment_type_cognitive.any?
  45. protocol_c_and_q_ids << protocol.id
  46. elsif assessments.assessment_type_cognitive.any?
  47. protocol_only_c_ids << protocol.id
  48. elsif assessments.assessment_type_questionnaire.any?
  49. protocol_only_q_ids << protocol.id
  50. end
  51. end
  52.  
  53. completed_c_protocol_sessions = ::Health::ProtocolSession.status_complete.where(protocol_id: protocol_only_c_ids).count
  54. completed_q_protocol_sessions = ::Health::ProtocolSession.status_complete.where(protocol_id: protocol_only_q_ids).count
  55. completed_c_and_q_protocol_sessions = ::Health::ProtocolSession.status_complete.where(protocol_id: protocol_c_and_q_ids).count
  56.  
  57. row += [c_report_count, q_report_count]
  58. row += [completed_c_protocol_sessions, completed_c_and_q_protocol_sessions, completed_q_protocol_sessions]
  59. row
  60. end
  61.  
  62. titles = 'Admin User ID (provided)
  63. Account name (provided)
  64. Vertical (provided)
  65. Total number of patients created
  66. Number of patients created below 6 years of age
  67. Number of patients created between 6 to 11 years of age
  68. Number of patients created older than 11 years and younger than 18 years
  69. Number of patients created ages 18 years and older and younger than 65 years
  70. Number of patients created ages 65 years and older
  71. Number of completed cognitive assessments
  72. Number of completed questionnaire assessments
  73. Number of completed protocols with just cognitive tasks
  74. Number of completed protocols with cognitive tasks and questionnaires
  75. Number of completed protocols with just questionnaires
  76. '.split("\n")
  77.  
  78. mf = MediaFile.find_by(name: 'CS-1367')
  79. rows = CSV.parse(mf.content, headers: true)
  80. p_bar = ProgressBar.new(rows.size)
  81.  
  82. processed_ids = []
  83. csv_file = "tmp/CS-1367.csv"
  84. if File.exist?(csv_file)
  85. CSV.foreach(csv_file, headers: true) { |row| processed_ids << "#{row['Admin User ID (provided)']} #{row['Account name (provided)']} #{row['Vertical (provided)']}" }
  86. end
  87.  
  88. CSV.open(csv_file, 'a+') do |csv|
  89. if processed_ids.empty?
  90. csv << titles
  91. end
  92. rows.each do |row|
  93. @row = row
  94. row['Account Name'] = 'Seçmen Danişmanlik Limited Şirketi' if row['Admin User ID'] == '880808'
  95.  
  96. p_bar.increment!
  97. value = "#{row["Admin User ID"]} #{row['Account Name']} #{row['Vertical']}"
  98. next if processed_ids.include?(value)
  99.  
  100. admin_id = nil
  101. admin_id = row["Admin User ID"] if row["Admin User ID"].presence
  102. p_bar.puts(admin_id)
  103. csv_row = [admin_id, row['Account Name'], row['Vertical']] + clinic_data(admin_id)
  104. csv << csv_row
  105. csv.flush
  106. p_bar.puts(csv_row)
  107. end
  108. nil
  109. end
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement