11class ResultGroup < StudyRelationship
2-
3- has_many :reported_events
4- has_many :milestones
5- has_many :drop_withdrawals
6- has_many :baseline_counts
7- has_many :baseline_measures
8- has_many :outcome_counts
9- has_many :outcome_measurements
10- has_many :outcome_analysis_groups , inverse_of : :result_group
11- has_many :outcome_analyses , :through => :outcome_analysis_groups
2+ belongs_to :outcome , optional : true
123
134 add_mapping do
145 [
156 {
167 table : :result_groups ,
178 root : [ :resultsSection , :baselineCharacteristicsModule , :groups ] ,
18- index : [ :ctgov_group_code , :result_type ] ,
19- unique : true ,
20- columns : [
21- { name : :ctgov_group_code , value : :id } ,
22- { name : :result_type , value : 'Baseline' } ,
23- { name : :title , value : :title } ,
24- { name : :description , value : :description } ,
25- ]
26- } ,
27- {
28- table : :result_groups ,
29- root : [ :resultsSection , :outcomeMeasuresModule , :outcomeMeasures ] ,
30- flatten : [ :groups ] ,
319 index : [ :ctgov_group_code , :result_type ] ,
3210 unique : true ,
3311 columns : [
3412 { name : :ctgov_group_code , value : :id } ,
35- { name : :result_type , value : 'Outcome ' } ,
13+ { name : :result_type , value : 'Baseline ' } ,
3614 { name : :title , value : :title } ,
3715 { name : :description , value : :description } ,
3816 ]
@@ -53,6 +31,8 @@ class ResultGroup < StudyRelationship
5331 table : :result_groups ,
5432 root : [ :resultsSection , :adverseEventsModule , :eventGroups ] ,
5533 index : [ :ctgov_group_code , :result_type ] ,
34+ # outcomes should be loaded first to avoid double loading of reported events
35+ requires : :outcomes , # TODO: review - this shouldn't be necessary
5636 unique : true ,
5737 columns : [
5838 { name : :ctgov_group_code , value : :id } ,
@@ -63,4 +43,61 @@ class ResultGroup < StudyRelationship
6343 }
6444 ]
6545 end
46+
47+ def self . handle_outcome_result_groups_ids ( nct_ids )
48+ Rails . logger . info "Handling Results Group IDs for Outcome Models"
49+ result_groups = fetch_outcome_groups_for ( nct_ids )
50+ set_results_group_ids_for ( OutcomeCount , nct_ids , result_groups )
51+ set_results_group_ids_for ( OutcomeMeasurement , nct_ids , result_groups )
52+ set_outcome_analysis_group_ids ( nct_ids , result_groups )
53+ end
54+
55+
56+ private
57+
58+ # TODO: add test for private methods
59+
60+ def self . set_outcome_analysis_group_ids ( nct_ids , result_groups )
61+ Rails . logger . info "Setting Outcome Analysis Group IDs"
62+ analyses = OutcomeAnalysis . where ( nct_id : nct_ids ) . pluck ( :id , :outcome_id ) . to_h
63+ groups = OutcomeAnalysisGroup . where ( nct_id : nct_ids )
64+
65+ updates = groups . map do | group |
66+ outcome_id = analyses [ group . outcome_analysis_id ]
67+ next unless outcome_id
68+
69+ key = [ group . nct_id , group . ctgov_group_code , outcome_id ]
70+ result_group_id = result_groups [ key ] &.id
71+ { id : group . id , result_group_id : result_group_id } if result_group_id
72+ end . compact
73+
74+ bulk_update ( OutcomeAnalysisGroup , updates )
75+ end
76+
77+ def self . set_results_group_ids_for ( model , nct_ids , result_groups )
78+ Rails . logger . info "Setting Result Group IDs for #{ model } "
79+
80+ records = model . where ( nct_id : nct_ids ) . select ( :id , :nct_id , :ctgov_group_code , :outcome_id )
81+ updates = records . map do |record |
82+ key = [ record . nct_id , record . ctgov_group_code , record . outcome_id ]
83+ result_group_id = result_groups [ key ] &.id
84+ { id : record . id , result_group_id : result_group_id } if result_group_id
85+ end . compact
86+
87+ bulk_update ( model , updates )
88+ end
89+
90+
91+ # TODO: consider adding index for nct_id and result_type
92+ def self . fetch_outcome_groups_for ( nct_ids )
93+ where ( nct_id : nct_ids , result_type : "Outcome" )
94+ . select ( :id , :nct_id , :ctgov_group_code , :outcome_id )
95+ . index_by do |group |
96+ [ group . nct_id , group . ctgov_group_code , group . outcome_id ]
97+ end
98+ end
99+
100+ def self . bulk_update ( model , updates )
101+ model . import updates , on_duplicate_key_update : { conflict_target : [ :id ] , columns : [ :result_group_id ] }
102+ end
66103end
0 commit comments