Skip to content

Commit

Permalink
Merged in r2-3054-fix-periodic-initializer (pull request #6958)
Browse files Browse the repository at this point in the history
R2-3054 R2-3050 Minor fix: periodic job and calculate_searchable_identifiers
  • Loading branch information
aespinoza-quoin authored and pnabutovsky committed Oct 16, 2024
2 parents 3747336 + 9c4d8b3 commit b8045de
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
14 changes: 8 additions & 6 deletions app/models/concerns/phonetic_searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ def recalculate_phonetic_tokens
end

def recalculate_searchable_identifiers
return unless filterable_id_fields_to_save.present?
return unless filterable_id_fields_changed?

generate_searchable_identifiers
end

def generate_searchable_identifiers
identifiers_to_save = identifiers_to_update

filterable_id_fields_to_save.each do |field_name|
self.class.filterable_id_fields.each do |field_name|
next if data[field_name].blank? || identifiers_to_save.any? { |identifier| identifier[:field_name] == field_name }

identifiers_to_save << { field_name:, value: data[field_name] }
Expand All @@ -46,8 +50,6 @@ def recalculate_searchable_identifiers
end

def identifiers_to_update
return [] unless filterable_id_fields_to_save.present?

searchable_identifiers.map do |searchable_identifier|
{
field_name: searchable_identifier.field_name,
Expand All @@ -57,8 +59,8 @@ def identifiers_to_update
end
end

def filterable_id_fields_to_save
changes_to_save_for_record.keys & self.class.filterable_id_fields
def filterable_id_fields_changed?
(changes_to_save_for_record.keys & self.class.filterable_id_fields).present?
end

def generate_tokens
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion config/initializers/periodic_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Rails.logger.info('Setting up PeriodicJobs')

jobs = %w[ArchiveBulkExports RecalculateAge]
jobs << OptimizeSolr.to_s if Rails.application.config.solr_enabled
jobs << OptimizeSolr.to_s if Rails.configuration.solr_enabled

jobs.each do |job_name|
next if Delayed::Job.where('handler LIKE :job_class', job_class: "%job_class: #{job_name}%").exists?
Expand Down
21 changes: 11 additions & 10 deletions db/data_migration/v2.11/calculate_searchable_identifiers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# rails r bin/calculate_solr_fields Child,Incident,TracingRequest true file/path.txt

def print_log(message)
message = "[#{DateTime.now.strftime('%m/%d/%Y %H:%M')}]: #{message}"
message = "[#{DateTime.now.strftime('%m/%d/%Y %H:%M:%S')}]: #{message}"
puts message
end

Expand All @@ -28,17 +28,18 @@ def records_to_process(model_class, ids_file_path)
records_to_process(model, file_path).find_in_batches(batch_size: 1000).with_index do |records, batch|
print_log("Process #{model.name} batch #{batch}...")
records.each do |record|
current_size = record.searchable_identifiers.size
record.generate_searchable_identifiers
generated_size = record.searchable_identifiers.size
total = generated_size - current_size
record_info = "record_type: #{model.name}, record_id: #{record.id}"
field_names = record.searchable_identifiers.map(&:field_name)
identifiers_info = "Searchable Identifiers[#{field_names.join(', ')}]"
if save_records
current_size = record.searchable_identifiers.size
record.generate_searchable_identifiers
generated_size = record.searchable_identifiers.size
total = generated_size - current_size
print_log("#{total} Searchable Identifiers generated for record_type: #{model.name}, record_id: #{record.id}")
elsif record.searchable_identifiers.present?
identifiers_size = record.searchable_identifiers.size
print_log("record_type: #{model.name}, record_id: #{record.id} has #{identifiers_size} Searchable Identifiers")
record.save!
print_log("#{total} - #{identifiers_info} generated for #{record_info}")
else
print_log("record_type: #{model.name}, record_id: #{record.id} does not have Searchable Identifiers")
print_log("#{total} - #{identifiers_info} will be generated for #{record_info}")
end
end
end
Expand Down

0 comments on commit b8045de

Please sign in to comment.