From f6b4909fff6cfd4088149c8e031652e3de6cc19c Mon Sep 17 00:00:00 2001 From: David Campbell <102170536+davidcam-src@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:29:15 -0500 Subject: [PATCH] HYC-1990 - Download Stat Remediation Utility (#1131) * utility for remediating download stats * error handling * update logging * default value * update initial count --- ...x_hyc_download_stats_work_id_mismatch.rake | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 lib/tasks/fix_hyc_download_stats_work_id_mismatch.rake diff --git a/lib/tasks/fix_hyc_download_stats_work_id_mismatch.rake b/lib/tasks/fix_hyc_download_stats_work_id_mismatch.rake new file mode 100644 index 000000000..fdaed7a65 --- /dev/null +++ b/lib/tasks/fix_hyc_download_stats_work_id_mismatch.rake @@ -0,0 +1,25 @@ +# frozen_string_literal: true +desc 'Fix hyc_download_stats work_id mismatch' +task fix_hyc_download_stats_work_id_mismatch: :environment do + puts 'Starting to fix hyc_download_stats work_id mismatches...' + # Retrieve where work_id is equal to fileset_id + download_stats = HycDownloadStat.where('work_id = fileset_id') + initial_count = download_stats.count + + updated = 0 + download_stats.each do |download_stat| + # Retrieve the work data for the fileset id + work_data = WorkUtilsHelper.fetch_work_data_by_fileset_id(download_stat.fileset_id) + + begin + # Update the download stat with the work id + download_stat.update!(work_id: work_data[:work_id] || 'Unknown') + updated += 1 + rescue StandardError => e + # Log any errors encountered during the update + puts "Failed to update HycDownloadStat ID #{download_stat.id}: #{e.message}" + end + end + puts 'Completed fixing hyc_download_stats work_id mismatches.' + puts "Successfully updated #{updated} records out of #{initial_count} attempted." +end