Class: MalawiHivProgramReports::Moh::CumulativeCohort

Inherits:
Object
  • Object
show all
Includes:
Utils::CommonSqlQueryUtils
Defined in:
app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb

Overview

This is the Cumulative Cohort Builder class rubocop:disable Metrics/ClassLength

Constant Summary collapse

LOCK_FILE =
'art_service/reports/cumulative_cohort.lock'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::CommonSqlQueryUtils

#current_occupation_query, #external_client_query, #occupation_filter, #partition_by_site, #process_occupation

Constructor Details

#initialize(start_date:, end_date:, **kwargs) ⇒ CumulativeCohort

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb', line 17

def initialize(start_date:, end_date:, **kwargs)
  @start_date = ActiveRecord::Base.connection.quote(start_date)
  @end_date = ActiveRecord::Base.connection.quote(end_date)
  @rebuild = kwargs[:rebuild]&.casecmp?('true')
  locations = kwargs[:locations]
  @locations = locations.present? ? locations.split(',') : []
  @definition = kwargs[:definition] || 'pepfar'
  @iteration = Concurrent::AtomicFixnum.new(0)
  @start_time = Time.now
  prepare_redis
  definition = @definition.downcase
  raise ArgumentError, "Invalid outcomes definition: #{definition}" unless %w[moh pepfar].include?(definition)
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



12
13
14
# File 'app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb', line 12

def definition
  @definition
end

#end_dateObject (readonly)

Returns the value of attribute end_date.



12
13
14
# File 'app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb', line 12

def end_date
  @end_date
end

#iterationObject (readonly)

Returns the value of attribute iteration.



12
13
14
# File 'app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb', line 12

def iteration
  @iteration
end

#locationsObject (readonly)

Returns the value of attribute locations.



12
13
14
# File 'app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb', line 12

def locations
  @locations
end

#outcomeObject (readonly)

Returns the value of attribute outcome.



12
13
14
# File 'app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb', line 12

def outcome
  @outcome
end

#rebuildObject (readonly)

Returns the value of attribute rebuild.



12
13
14
# File 'app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb', line 12

def rebuild
  @rebuild
end

#redisObject (readonly)

Returns the value of attribute redis.



12
13
14
# File 'app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb', line 12

def redis
  @redis
end

#start_dateObject (readonly)

Returns the value of attribute start_date.



12
13
14
# File 'app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb', line 12

def start_date
  @start_date
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



12
13
14
# File 'app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb', line 12

def start_time
  @start_time
end

Instance Method Details

#find_reportObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/malawi_hiv_program_reports/moh/cumulative_cohort.rb', line 31

def find_report
  handle_tables
  clear_cohort_status unless rebuild
  process_thread(locations:)
  end_time = Time.now
  time_in_minutes = ((end_time - start_time) / 60).round(2)
  handle_failed
  time_taken_for_failed = ((Time.now - end_time) / 60).round(2)
  Rails.logger.info("Cumulative Cohort report took #{time_in_minutes} minutes to generate for these locations: #{locations}")
  redis.set('cumulative_time_taken', time_in_minutes)
  redis.set('cumulative_status', 'completed')
  { cohort_time: time_in_minutes, processing_failed: time_taken_for_failed }
end