Module: ScraperUtils::LogUtils
- Defined in:
- lib/scraper_utils/log_utils.rb
Overview
Utilities for logging scraper execution details and outcomes
Constant Summary collapse
- SUMMARY_TABLE =
"scrape_summary"- LOG_TABLE =
"scrape_log"- LOG_RETENTION_DAYS =
30
Class Method Summary collapse
- .cleanup_old_records(force: false) ⇒ Object
-
.extract_meaningful_backtrace(error) ⇒ Object
Extracts meaningful backtrace - 3 lines from ruby/gem and max 6 in total.
-
.log(message, authority = nil) ⇒ void
Logs a message, automatically prefixing with authority name if in a fiber.
-
.log_scraping_run(start_time, attempt, authorities, exceptions) ⇒ void
Log details about a scraping run for one or more authorities
DataQualityMonitor.statsis checked for :saved and :unprocessed entries. -
.report_on_results(authorities, exceptions) ⇒ void
Report on the results
DataQualityMonitor.statsis checked for :saved and :unprocessed entries. - .save_log_record(record) ⇒ Object
- .save_summary_record(start_time, attempt, duration, successful, interrupted, failed) ⇒ Object
Class Method Details
.cleanup_old_records(force: false) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/scraper_utils/log_utils.rb', line 195 def self.cleanup_old_records(force: false) cutoff = (Date.today - LOG_RETENTION_DAYS).to_s return if !force && @last_cutoff == cutoff @last_cutoff = cutoff [SUMMARY_TABLE, LOG_TABLE].each do |table| ScraperWiki.sqliteexecute( "DELETE FROM #{table} WHERE date(run_at) < date(?)", [cutoff] ) end end |
.extract_meaningful_backtrace(error) ⇒ Object
Extracts meaningful backtrace - 3 lines from ruby/gem and max 6 in total
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/scraper_utils/log_utils.rb', line 210 def self.extract_meaningful_backtrace(error) return nil unless error.respond_to?(:backtrace) && error&.backtrace lines = [] error.backtrace.each do |line| lines << line if lines.length < 2 || !line.include?("/vendor/") break if lines.length >= 6 end lines.empty? ? nil : lines.join("\n") end |
.log(message, authority = nil) ⇒ void
This method returns an undefined value.
Logs a message, automatically prefixing with authority name if in a fiber
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/scraper_utils/log_utils.rb', line 16 def self.log(, = nil) ||= Scheduler. $stderr.flush if puts "[#{}] #{}" else puts end $stdout.flush end |
.log_scraping_run(start_time, attempt, authorities, exceptions) ⇒ void
This method returns an undefined value.
Log details about a scraping run for one or more authorities DataQualityMonitor.stats is checked for :saved and :unprocessed entries
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/scraper_utils/log_utils.rb', line 34 def self.log_scraping_run(start_time, attempt, , exceptions) raise ArgumentError, "Invalid start time" unless start_time.is_a?(Time) raise ArgumentError, "Authorities must be a non-empty array" if .empty? end_time = Time.now duration = (end_time - start_time).round(1) successful = [] failed = [] interrupted = [] .each do || stats = ScraperUtils::DataQualityMonitor.stats&.fetch(, nil) || {} exception = exceptions[] status = if stats[:saved]&.positive? exception ? :interrupted : :successful else :failed end case status when :successful successful << when :interrupted interrupted << else failed << end record = { "run_at" => start_time.iso8601, "attempt" => attempt, "authority_label" => .to_s, "records_saved" => stats[:saved] || 0, "unprocessable_records" => stats[:unprocessed] || 0, "status" => status.to_s, "error_message" => exception&., "error_class" => exception&.class&.to_s, "error_backtrace" => extract_meaningful_backtrace(exception) } save_log_record(record) end # Save summary record for the entire run save_summary_record( start_time, attempt, duration, successful, interrupted, failed ) cleanup_old_records end |
.report_on_results(authorities, exceptions) ⇒ void
This method returns an undefined value.
Report on the results DataQualityMonitor.stats is checked for :saved and :unprocessed entries
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/scraper_utils/log_utils.rb', line 96 def self.report_on_results(, exceptions) if ENV["MORPH_EXPECT_BAD"] expect_bad = ENV["MORPH_EXPECT_BAD"].split(",").map(&:strip).map(&:to_sym) end expect_bad ||= [] $stderr.flush puts "MORPH_EXPECT_BAD=#{ENV.fetch('MORPH_EXPECT_BAD', nil)}" # Print summary table puts "\nScraping Summary:" summary_format = "%-20s %6s %6s %s" puts format(summary_format, 'Authority', 'OK', 'Bad', 'Exception') puts format(summary_format, "-" * 20, "-" * 6, "-" * 6, "-" * 50) .each do || stats = ScraperUtils::DataQualityMonitor.stats&.fetch(, {}) || {} ok_records = stats[:saved] || 0 bad_records = stats[:unprocessed] || 0 expect_bad_prefix = expect_bad.include?() ? "[EXPECT BAD] " : "" exception_msg = if exceptions[] "#{exceptions[].class} - #{exceptions[].}" else "-" end puts format(summary_format, .to_s, ok_records, bad_records, "#{expect_bad_prefix}#{exception_msg}".slice(0, 70)) end puts errors = [] # Check for authorities that were expected to be bad but are now working unexpected_working = expect_bad.select do || stats = ScraperUtils::DataQualityMonitor.stats&.fetch(, {}) || {} stats[:saved]&.positive? && !exceptions[] end if unexpected_working.any? errors << "WARNING: Remove #{unexpected_working.join(',')} from MORPH_EXPECT_BAD as it now works!" end # Check for authorities with unexpected errors unexpected_errors = .select { || exceptions[] } .reject { || expect_bad.include?() } if unexpected_errors.any? errors << "ERROR: Unexpected errors in: #{unexpected_errors.join(',')} " \ "(Add to MORPH_EXPECT_BAD?)" unexpected_errors.each do || error = exceptions[] errors << " #{}: #{error.class} - #{error.}" end end $stdout.flush if errors.any? errors << "See earlier output for details" raise errors.join("\n") end puts "Exiting with OK status!" end |
.save_log_record(record) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/scraper_utils/log_utils.rb', line 165 def self.save_log_record(record) ScraperWiki.save_sqlite( %w[authority_label run_at], record, LOG_TABLE ) end |
.save_summary_record(start_time, attempt, duration, successful, interrupted, failed) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/scraper_utils/log_utils.rb', line 173 def self.save_summary_record(start_time, attempt, duration, successful, interrupted, failed) summary = { "run_at" => start_time.iso8601, "attempt" => attempt, "duration" => duration, "successful" => successful.join(","), "failed" => failed.join(","), "interrupted" => interrupted.join(","), "successful_count" => successful.size, "interrupted_count" => interrupted.size, "failed_count" => failed.size, "public_ip" => ScraperUtils::MechanizeUtils.public_ip } ScraperWiki.save_sqlite( ["run_at"], summary, SUMMARY_TABLE ) end |