Sha256: 3b3d43ff617ef931379c397ade134a74bc3db5b611c10607b3cc2a55fa9f5330

Contents?: true

Size: 1.75 KB

Versions: 1

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(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

1 entries across 1 versions & 1 rubygems

Version Path
malawi_hiv_program_reports-1.0.2 app/services/malawi_hiv_program_reports/clinic/patients_alive_and_on_treatment.rb