Sha256: 8e55ef8bb32fbd9f38271560191c49693bb6dc172b39eae671a3b45a26657099

Contents?: true

Size: 1.75 KB

Versions: 30

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

# Returns all patients alive and on treatment within a given
# time period (Tx Curr).
module MalawiHivProgramReports
  module Clinic
    class PatientsAliveAndOnTreatment
      attr_reader :start_date, :end_date

      include MalawiHivProgramReports::Utils::ConcurrencyUtils

      def initialize(start_date:, end_date:, outcomes_definition: 'moh', rebuild_outcomes: true, **kwargs)
        @start_date = start_date
        @end_date = end_date
        @rebuild_outcomes = rebuild_outcomes
        @outcomes_definition = outcomes_definition
        @occupation = kwargs[:occupation]
        @location = kwargs[:location]
      end

      ##
      # Repopulates temp_patient_outcomes and temp_earliest_start_date tables.
      #
      # The data in temp_patient_outcomes can be used to quickly find patients
      # with various outcomes.
      def refresh_outcomes_table
        logger.debug('Initialising cohort temporary tables...')
        MalawiHivProgramReports::Moh::CohortBuilder.new(outcomes_definition: @outcomes_definition, location: @location)
                                                   .init_temporary_tables(@start_date, @end_date, @occupation)
      end

      def find_report
        query.map(&:patient_id)
      end

      def query
        with_lock(Moh::Cohort::LOCK_FILE) do
          refresh_outcomes_table if @rebuild_outcomes || !outcomes_table_exists?

          ::Patient.find_by_sql <<~SQL
            SELECT patient_id FROM temp_patient_outcomes WHERE cum_outcome LIKE 'On antiretrovirals'
          SQL
        end
      end

      private

      def outcomes_table_exists?
        ActiveRecord::Base.connection.table_exists?(:temp_patient_outcomes)
      end

      def logger
        Rails.logger
      end
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
malawi_hiv_program_reports-1.1.18 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.17 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.16 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.15 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.14 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.13 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.12 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.11 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.10 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.9 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.8 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.7 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.6 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.5 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.4 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.3 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.2 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.1 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.1.0 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb
malawi_hiv_program_reports-1.0.27 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb