1
- def patient_data_report ( organisation_id )
2
- organisation = Organisation . find ( organisation_id )
3
- patients = Patient . where ( organisation :)
4
- patient_ids = patients . pluck ( :id )
1
+ # frozen_string_literal: true
5
2
6
- # Audits for NHS number added later - process in chunks
7
- nhs_added_ids = [ ]
8
- nhs_from_pds_ids = [ ]
9
- nhs_manually_added_ids = [ ]
10
-
11
- patient_ids . each_slice ( 500 ) do |patient_chunk |
12
- Audited ::Audit
13
- . where ( auditable_type : 'Patient' , auditable_id : patient_chunk )
14
- . where . not ( action : 'create' )
15
- . each do |audit |
16
- changes = audit . audited_changes
17
- nhs_change = changes [ 'nhs_number' ]
18
-
19
- # Check if NHS number was added (nil -> value)
20
- if nhs_change &.is_a? ( Array ) && nhs_change [ 0 ] . nil? && !nhs_change [ 1 ] . nil?
21
- nhs_added_ids << audit . auditable_id
22
-
23
- # Check if it was from PDS (has both nhs_number and updated_from_pds_at changes)
24
- if changes . key? ( 'updated_from_pds_at' )
25
- nhs_from_pds_ids << audit . auditable_id
26
- else
27
- nhs_manually_added_ids << audit . auditable_id
3
+ module PDSExperiments
4
+ class PDSBackground
5
+ def patient_data_report ( organisation_id )
6
+ organisation = Organisation . find ( organisation_id )
7
+ patients = Patient . where ( organisation :)
8
+ patient_ids = patients . pluck ( :id )
9
+
10
+ # Audits for NHS number added later - process in chunks
11
+ nhs_added_ids = [ ]
12
+ nhs_from_pds_ids = [ ]
13
+ nhs_manually_added_ids = [ ]
14
+
15
+ patient_ids . each_slice ( 500 ) do |patient_chunk |
16
+ Audited ::Audit
17
+ . where ( auditable_type : "Patient" , auditable_id : patient_chunk )
18
+ . where . not ( action : "create" )
19
+ . find_each do |audit |
20
+ changes = audit . audited_changes
21
+ nhs_change = changes [ "nhs_number" ]
22
+
23
+ # Check if NHS number was added (nil -> value)
24
+ unless nhs_change . is_a? ( Array ) && nhs_change [ 0 ] . nil? &&
25
+ !nhs_change [ 1 ] . nil?
26
+ next
27
+ end
28
+ nhs_added_ids << audit . auditable_id
29
+
30
+ # Check if it was from PDS (has both nhs_number and updated_from_pds_at changes)
31
+ if changes . key? ( "updated_from_pds_at" )
32
+ nhs_from_pds_ids << audit . auditable_id
33
+ else
34
+ nhs_manually_added_ids << audit . auditable_id
35
+ end
28
36
end
29
- end
30
37
end
31
- end
32
-
33
- nhs_added_ids . uniq!
34
- nhs_from_pds_ids . uniq!
35
- nhs_manually_added_ids . uniq!
36
38
37
- total_patients = patients . count
39
+ nhs_added_ids . uniq!
40
+ nhs_from_pds_ids . uniq!
41
+ nhs_manually_added_ids . uniq!
42
+
43
+ total_patients = patients . count
38
44
39
- # NHS Number stats
40
- with_nhs_count = patients . where . not ( nhs_number : nil ) . count
41
- uploaded_with_nhs_count = patients . where . not ( nhs_number : nil ) . where . not ( id : nhs_added_ids ) . count
42
- nhs_added_later_count = nhs_added_ids . size
43
- nhs_from_pds_count = nhs_from_pds_ids . size
44
- nhs_manually_added_count = nhs_manually_added_ids . size
45
+ # NHS Number stats
46
+ with_nhs_count = patients . where . not ( nhs_number : nil ) . count
47
+ uploaded_with_nhs_count =
48
+ patients . where . not ( nhs_number : nil ) . where . not ( id : nhs_added_ids ) . count
49
+ nhs_added_later_count = nhs_added_ids . size
50
+ nhs_from_pds_count = nhs_from_pds_ids . size
51
+ nhs_manually_added_count = nhs_manually_added_ids . size
45
52
46
- # Gender stats
47
- without_gender_count = patients . not_known . count
48
- with_gender_count = total_patients - without_gender_count
53
+ # Gender stats
54
+ without_gender_count = patients . not_known . count
55
+ with_gender_count = total_patients - without_gender_count
49
56
50
- # Consent stats
51
- total_consents = Consent . where ( organisation_id :) . count
52
- auto_matched_consents = Consent . where ( organisation_id :, recorded_by_user_id : nil ) . count
53
- manually_recorded_consents = total_consents - auto_matched_consents
57
+ # Consent stats
58
+ total_consents = Consent . where ( organisation_id :) . count
59
+ auto_matched_consents =
60
+ Consent . where ( organisation_id :, recorded_by_user_id : nil ) . count
61
+ manually_recorded_consents = total_consents - auto_matched_consents
54
62
55
- puts <<~REPORT
56
- Patient Data Report
57
- ==========================
63
+ puts <<~REPORT
64
+ Patient Data Report
65
+ ==========================
58
66
59
- Organisation: #{ organisation . name } (ID: #{ organisation . id } )
67
+ Organisation: #{ organisation . name } (ID: #{ organisation . id } )
60
68
61
- NHS Number Statistics:
62
- ---------------------------
63
- Total Patients: #{ total_patients . to_s . rjust ( 6 ) } (100.0%)
64
- With NHS Number: #{ with_nhs_count . to_s . rjust ( 6 ) } (#{ ( ( with_nhs_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
65
- - Uploaded with NHS: #{ uploaded_with_nhs_count . to_s . rjust ( 6 ) } (#{ ( ( uploaded_with_nhs_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
66
- - Added NHS Later: #{ nhs_added_later_count . to_s . rjust ( 6 ) } (#{ ( ( nhs_added_later_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
67
- • From PDS: #{ nhs_from_pds_count . to_s . rjust ( 6 ) } (#{ ( ( nhs_from_pds_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
68
- • Manually Added: #{ nhs_manually_added_count . to_s . rjust ( 6 ) } (#{ ( ( nhs_manually_added_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
69
- Without NHS Number: #{ ( total_patients - with_nhs_count ) . to_s . rjust ( 6 ) } (#{ ( ( ( total_patients - with_nhs_count ) . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
69
+ NHS Number Statistics:
70
+ ---------------------------
71
+ Total Patients: #{ total_patients . to_s . rjust ( 6 ) } (100.0%)
72
+ With NHS Number: #{ with_nhs_count . to_s . rjust ( 6 ) } (#{ ( ( with_nhs_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
73
+ - Uploaded with NHS: #{ uploaded_with_nhs_count . to_s . rjust ( 6 ) } (#{ ( ( uploaded_with_nhs_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
74
+ - Added NHS Later: #{ nhs_added_later_count . to_s . rjust ( 6 ) } (#{ ( ( nhs_added_later_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
75
+ • From PDS: #{ nhs_from_pds_count . to_s . rjust ( 6 ) } (#{ ( ( nhs_from_pds_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
76
+ • Manually Added: #{ nhs_manually_added_count . to_s . rjust ( 6 ) } (#{ ( ( nhs_manually_added_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
77
+ Without NHS Number: #{ ( total_patients - with_nhs_count ) . to_s . rjust ( 6 ) } (#{ ( ( ( total_patients - with_nhs_count ) . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
70
78
71
- Gender Statistics:
72
- ---------------------------
73
- With Gender Code: #{ with_gender_count . to_s . rjust ( 6 ) } (#{ ( ( with_gender_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
74
- Without Gender Code: #{ without_gender_count . to_s . rjust ( 6 ) } (#{ ( ( without_gender_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
79
+ Gender Statistics:
80
+ ---------------------------
81
+ With Gender Code: #{ with_gender_count . to_s . rjust ( 6 ) } (#{ ( ( with_gender_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
82
+ Without Gender Code: #{ without_gender_count . to_s . rjust ( 6 ) } (#{ ( ( without_gender_count . to_f / total_patients ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) } %)
75
83
76
- Consent Statistics:
77
- ---------------------------
78
- Total Consents: #{ total_consents . to_s . rjust ( 6 ) } (100.0%)
79
- Auto-matched Consents: #{ auto_matched_consents . to_s . rjust ( 6 ) } (#{ total_consents > 0 ? ( ( auto_matched_consents . to_f / total_consents ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) : '0.0' . rjust ( 5 ) } %)
80
- Manually Recorded: #{ manually_recorded_consents . to_s . rjust ( 6 ) } (#{ total_consents > 0 ? ( ( manually_recorded_consents . to_f / total_consents ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) : '0.0' . rjust ( 5 ) } %)
81
- REPORT
82
- end
84
+ Consent Statistics:
85
+ ---------------------------
86
+ Total Consents: #{ total_consents . to_s . rjust ( 6 ) } (100.0%)
87
+ Auto-matched Consents: #{ auto_matched_consents . to_s . rjust ( 6 ) } (#{ total_consents . positive? ? ( ( auto_matched_consents . to_f / total_consents ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) : "0.0" . rjust ( 5 ) } %)
88
+ Manually Recorded: #{ manually_recorded_consents . to_s . rjust ( 6 ) } (#{ total_consents . positive? ? ( ( manually_recorded_consents . to_f / total_consents ) * 100 ) . round ( 1 ) . to_s . rjust ( 5 ) : "0.0" . rjust ( 5 ) } %)
89
+ REPORT
90
+ end
91
+ end
92
+ end
0 commit comments