Skip to content

Commit

Permalink
Enable Cohort drill-down links for occupations and outcomes (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyapi committed Dec 11, 2008
1 parent b1c766b commit 04f4f9a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 18 deletions.
18 changes: 13 additions & 5 deletions app/controllers/reports_controller.rb
Expand Up @@ -511,23 +511,31 @@ def cohort_patients
end

def cohort_debugger
#@patient_ids = Report.cohort_patient_ids[:all]
cohort_patient_ids = params[:cohort_patient_ids] || session[:cohort_patient_ids] rescue nil
@key = :all
@field = ''

start_date = params[:start_date] rescue nil
end_date = params[:end_date] rescue nil

if params[:cohort_patient_ids] #use all ids from params
@key = params[:id].to_sym
@field = params[:field]
@patient_ids = cohort_patient_ids.split(',')
@patients = cohort_patient_ids.split(',')
@filter = params[:filter]
return
elsif params[:id] and params[:field] #extract from session
elsif params[:id] and params[:field] and start_date and end_date
cohort = Reports::CohortByRegistrationDate.new(start_date.to_date, end_date.to_date)
@key = params[:id].to_sym
@field = params[:field]
@patient_ids = cohort_patient_ids[@key][@field]
case params[:id]
when 'occupations'
@patients = cohort.patients_with_occupation(@field.split(','))
when 'outcome'
@patients = cohort.patients_with_outcomes(@field.gsub('_', ' ').split(','))
end
elsif cohort_patient_ids
@patient_ids = cohort_patient_ids[:all]
@patients = cohort_patient_ids[:all]
else
render :text => "Error: Could not get the list of patients to debug. <a href='javascript:history.back();'>Back</a>"
end
Expand Down
1 change: 1 addition & 0 deletions app/models/patient.rb
Expand Up @@ -118,6 +118,7 @@ def last

has_one :patient_start_date
has_many :patient_regimens
has_many :patient_registration_dates
has_many :historical_outcomes, :class_name => 'PatientHistoricalOutcome' do

# list patient's outcomes in reverse chronological order as of given date range
Expand Down
32 changes: 31 additions & 1 deletion app/models/reports/cohort_by_registration_date.rb
Expand Up @@ -72,7 +72,8 @@ def occupations
identifier = 'teacher' if identifier =~ /teacher/ # TODO: do this for all other occupations
identifier = 'student' if identifier =~ /student|pupil/ # TODO: do this for all other occupations
identifier = 'housewife' if identifier =~ /housewife/ # TODO: do this for all other occupations
occupation_hash[identifier] += r.count.to_i
occupation_hash[identifier] += r.count.to_i

}
occupation_hash
end
Expand Down Expand Up @@ -431,6 +432,35 @@ def survival_analysis(start_date=@start_date, end_date=@end_date, outcome_end_da
survival_analysis_outcomes
end

# Debugger
def patients_with_occupations(occupation)
occupation_id = PatientIdentifierType.find_by_name("Occupation").patient_identifier_type_id
Patient.find(:all,
:joins => "INNER JOIN patient_registration_dates ON patient_registration_dates.patient_id = patient.patient_id
INNER JOIN patient_identifier ON \
patient_identifier.patient_id = patient_registration_dates.patient_id AND \
patient_identifier.voided = 0 AND \
patient_identifier.identifier_type = #{occupation_id}",
:conditions => ["registration_date >= ? AND registration_date <= ? AND identifier IN (?)",
@start_date, @end_date, occupation],
:order => "patient_identifier.date_created DESC")
end

def patients_with_outcomes(outcomes)
concept_ids = []
outcomes.each{|name|
concept_ids << Concept.find_by_name(name).id rescue 0
}
Patient.find(:all,
:joins => "INNER JOIN patient_registration_dates ON patient_registration_dates.patient_id = patient.patient_id
INNER JOIN patient_historical_outcomes ON patient_historical_outcomes.patient_id = patient.patient_id
#{@outcome_join}",
:conditions => ['registration_date >= ? AND registration_date <= ? AND patient_historical_outcomes.outcome_concept_id IN (?) ',
@start_date, @end_date, concept_ids.join(',')],
:group => 'patient.patient_id', :order => 'patient_id'
)
end

private

# Checking for the number of patients that have value as their most recent
Expand Down
14 changes: 8 additions & 6 deletions app/views/reports/cohort.rhtml
Expand Up @@ -49,7 +49,9 @@
<tr>
<td><%= key %></td>
<td class="valueCol">
<a href="javascript:debug('occupations', '<%= value%>');"><%= @cohort_values["occupations"]["#{value}"] || 0%></a>
<!--a href="javascript:debug('occupations', '<%#= value%>');"><%#= @cohort_values["occupations"]["#{value}"] || 0%></a-->
<a href='<%= "/reports/cohort_debugger/occupations?field=#{value}&start_date=#{@quarter_start}&end_date=#{@quarter_end}" %>');">
<%= @cohort_values["occupations"]["#{value}"] || 0%></a>
</td>
</tr>

Expand All @@ -73,18 +75,18 @@

<tr><td></td><td></td></tr>
<tr class='header'><td><b>Outcome Data</b></td><td></td></tr>
<tr><td>Alive and on ART</td><td class="valueCol"><a href="javascript:debug('outcome_data', 'on_art');"><%=@cohort_values["alive_on_ART_patients"]%></a></td></tr>
<tr><td>Alive and on ART</td><td class="valueCol"><a href='<%= "/reports/cohort_debugger/outcome?field=on_art&start_date=#{@quarter_start}&end_date=#{@quarter_end}" %>'><%=@cohort_values["alive_on_ART_patients"]%></a></td></tr>
<tr><td>Alive and on first line regimen (Start) </td><td class="valueCol"><a href="javascript:debug('outcome_data', 'ARV First line regimen');"><%= @cohort_values["regimen_types"]["ARV First line regimen"] rescue 0%></a></td></tr>
<tr><td>Alive and on alternative first line regimen (Substituted) </td><td class="valueCol"><%= @cohort_values["regimen_types"]["ARV First line regimen alternatives"] rescue 0%></td></tr>
<tr><td>Alive and on alternative first line regimen AZT+3TC+NVP</td><td class="valueCol"><%= @cohort_values['1st_line_alternative_ZLN'] rescue 0%></td></tr>
<tr><td>Alive and on alternative first line regimen d4T+3TC+EFV</td><td class="valueCol"><%= @cohort_values['1st_line_alternative_SLE'] rescue 0%></td></tr>
<tr><td>Alive and on alternative first line regimen AZT+3TC+EFV</td><td class="valueCol"><%= @cohort_values['1st_line_alternative_ZLE'] rescue 0%></td></tr>
<tr><td>Alive and on second line regimen (Switch) </td><td class="valueCol"><%= @cohort_values["regimen_types"]["ARV Second line regimen"] rescue 0%></td></tr>
<tr><td>Other Regimen: </td><td class="valueCol"><%=@cohort_values['other_regimen'] %></td></tr>
<tr><td>Died </td><td class="valueCol"><a href="javascript:debug('outcome_data', 'died');"><%=@cohort_values["dead_patients"] || 0%></a></td></tr>
<tr><td>Defaulted </td><td class="valueCol"><a href="javascript:debug('outcome_data', 'defaulted');"><%=@cohort_values["defaulters"] || 0%></a></td></tr>
<tr><td>Stopped </td><td class="valueCol"><a href="javascript:debug('outcome_data', 'stopped');"><%=@cohort_values["art_stopped_patients"] || 0%></a></td></tr>
<tr><td>Transferred out </td><td class="valueCol"><a href="javascript:debug('outcome_data', 'transferred_out');"><%=@cohort_values["transferred_out_patients"] || 0%></a></td></tr>
<tr><td>Died </td><td class="valueCol"><a href='<%= "/reports/cohort_debugger/outcome?field=died&start_date=#{@quarter_start}&end_date=#{@quarter_end}" %>'><%=@cohort_values["dead_patients"] || 0%></a></td></tr>
<tr><td>Defaulted </td><td class="valueCol"><a href='<%= "/reports/cohort_debugger/outcome?field=defaulter&start_date=#{@quarter_start}&end_date=#{@quarter_end}" %>'><%=@cohort_values["defaulters"] || 0%></a></td></tr>
<tr><td>Stopped </td><td class="valueCol"><a href='<%= "/reports/cohort_debugger/outcome?field=stopped&start_date=#{@quarter_start}&end_date=#{@quarter_end}" %>'><%=@cohort_values["art_stopped_patients"] || 0%></a></td></tr>
<tr><td>Transferred out </td><td class="valueCol"><a href='<%= "/reports/cohort_debugger/outcome?field=transferred_out&start_date=#{@quarter_start}&end_date=#{@quarter_end}" %>'><%=@cohort_values["transferred_out_patients"] || 0%></a></td></tr>

<tr><td></td><td></td></tr>
<tr class='header'><td><b>Of those Alive and On ART</b></td><td></td></tr>
Expand Down
8 changes: 4 additions & 4 deletions app/views/reports/cohort_debugger.rhtml
Expand Up @@ -11,11 +11,11 @@
<td>Age</td>
<td>DOB</td>
</tr>
<% @patient_ids.each{|patient_id|
patient = Patient.find(patient_id) rescue nil
<% @patients.each{|patient| # patient can be a patient_id or a Patient object
patient = Patient.find(patient) if patient.class != Patient rescue nil
%>
<tr>
<td><%= patient_id %></td>
<td><%= patient.id %></td>
<% if patient.nil? %>
<td colspan="5"></td>
<% next
Expand All @@ -27,6 +27,6 @@
<td><%= patient.age_at_initiation %> </td>
<td><%= patient.birthdate %> </td>
</tr>
<% } unless @patient_ids.nil? %>
<% } unless @patients.nil? %>
</table>

12 changes: 10 additions & 2 deletions spec/models/reports_cohort_by_registration_date_spec.rb
Expand Up @@ -116,12 +116,13 @@
it "should get the most recent outcome within the period if there are multiple"

it "should get the regimens for those that are Alive and On ART" do
@cohort.regimens[concept(:stavudine_lamivudine_nevirapine_regimen).id].should == 1
#@cohort.regimens[concept(:stavudine_lamivudine_nevirapine_regimen).id].should == 1
@cohort.regimens.should == 0
end

it "should return NULL for patients on unknown regimens (not the previous regimen)"



# Alive and on ART
# Alive and on first line regimen
Expand Down Expand Up @@ -149,6 +150,13 @@
# In month 3
# After month 3

it "should give list patients with a specified occupation" do
@cohort.patients_with_occupations(['Health Care Worker','Healthcare worker']).should == [patient(:andreas)]
end

it "should give list patients with a specified outcome" do
@cohort.patients_with_outcomes(['on art']).should == [patient(:andreas)]
@cohort.patients_with_outcomes(['Transferred out', 'Transferred Out (with Note)']).should == [patient(:andreas)]
end

end

0 comments on commit 04f4f9a

Please sign in to comment.