Browse Source
* Add tasks to just count and surface invalid LettingsLog and SalesLog * Refactor InvalidLogsHelper module structure * Refactor InvalidLogsHelper for improved logging and module structure * Update logging level in InvalidLogsHelper from debug to info for better visibility * Reorganise includes in invalid_logs.rakepull/3064/head^2 v0.5.12
2 changed files with 44 additions and 0 deletions
@ -0,0 +1,23 @@ |
|||||||
|
module InvalidLogsHelper |
||||||
|
def count_and_display_invalid_logs(model, log_type, year) |
||||||
|
invalid_logs = fetch_invalid_logs(model, year) |
||||||
|
Rails.logger.info "Number of invalid #{log_type} for year #{year}: #{invalid_logs.size}" |
||||||
|
Rails.logger.info "Invalid #{log_type} IDs: #{invalid_logs.map(&:id).join(', ')}" |
||||||
|
end |
||||||
|
|
||||||
|
def surface_invalid_logs(model, log_type, year) |
||||||
|
invalid_logs = fetch_invalid_logs(model, year) |
||||||
|
if invalid_logs.any? |
||||||
|
invalid_logs.each do |log| |
||||||
|
Rails.logger.info "#{log_type} ID: #{log.id}" |
||||||
|
log.errors.full_messages.each { |message| Rails.logger.info " - #{message}" } |
||||||
|
end |
||||||
|
else |
||||||
|
Rails.logger.info "No invalid #{log_type} found for year #{year}." |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def fetch_invalid_logs(model, year) |
||||||
|
model.filter_by_year(year).reject(&:valid?) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,21 @@ |
|||||||
|
namespace :logs do |
||||||
|
desc "Count the number of invalid LettingsLog and SalesLog for a given year" |
||||||
|
task :count_invalid, [:year] => :environment do |_task, args| |
||||||
|
include CollectionTimeHelper |
||||||
|
include InvalidLogsHelper |
||||||
|
|
||||||
|
year = args[:year] || current_collection_year |
||||||
|
count_and_display_invalid_logs(LettingsLog, "LettingsLog", year) |
||||||
|
count_and_display_invalid_logs(SalesLog, "SalesLog", year) |
||||||
|
end |
||||||
|
|
||||||
|
desc "Surface all invalid logs and output their error messages for a given year" |
||||||
|
task :surface_invalid, [:year] => :environment do |_task, args| |
||||||
|
include CollectionTimeHelper |
||||||
|
include InvalidLogsHelper |
||||||
|
|
||||||
|
year = args[:year] || current_collection_year |
||||||
|
surface_invalid_logs(LettingsLog, "LettingsLog", year) |
||||||
|
surface_invalid_logs(SalesLog, "SalesLog", year) |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue