blazer.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. require "csv"
  2. require "yaml"
  3. require "chartkick"
  4. require "blazer/version"
  5. require "blazer/data_source"
  6. require "blazer/engine"
  7. require "blazer/tasks"
  8. module Blazer
  9. class << self
  10. attr_accessor :audit
  11. attr_reader :time_zone
  12. attr_accessor :user_name
  13. attr_accessor :user_class
  14. attr_accessor :user_method
  15. attr_accessor :before_action
  16. attr_accessor :from_email
  17. attr_accessor :cache
  18. attr_accessor :transform_statement
  19. end
  20. self.audit = true
  21. self.user_name = :name
  22. TIMEOUT_MESSAGE = "Query timed out :("
  23. def self.time_zone=(time_zone)
  24. @time_zone = time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone[time_zone.to_s]
  25. end
  26. def self.settings
  27. @settings ||= begin
  28. path = Rails.root.join("config", "blazer.yml").to_s
  29. if File.exist?(path)
  30. YAML.load(ERB.new(File.read(path)).result)
  31. else
  32. {}
  33. end
  34. end
  35. end
  36. def self.data_sources
  37. @data_sources ||= begin
  38. ds = Hash[
  39. settings["data_sources"].map do |id, s|
  40. [id, Blazer::DataSource.new(id, s)]
  41. end
  42. ]
  43. ds.default = ds.values.first
  44. ds
  45. end
  46. end
  47. def self.run_checks
  48. Blazer::Check.includes(:query).find_each do |check|
  49. rows = nil
  50. error = nil
  51. tries = 0
  52. # try 3 times on timeout errors
  53. while tries < 3
  54. rows, error, cached_at = data_sources[check.query.data_source].run_statement(check.query.statement, refresh_cache: true)
  55. if error == Blazer::TIMEOUT_MESSAGE
  56. Rails.logger.info "[blazer timeout] query=#{check.query.name}"
  57. tries += 1
  58. sleep(10)
  59. else
  60. break
  61. end
  62. end
  63. check.update_state(rows, error)
  64. # TODO use proper logfmt
  65. Rails.logger.info "[blazer check] query=#{check.query.name} state=#{check.state} rows=#{rows.try(:size)} error=#{error}"
  66. end
  67. end
  68. def self.send_failing_checks
  69. emails = {}
  70. Blazer::Check.includes(:query).where(state: %w[failing error]).find_each do |check|
  71. check.split_emails.each do |email|
  72. (emails[email] ||= []) << check
  73. end
  74. end
  75. emails.each do |email, checks|
  76. Blazer::CheckMailer.failing_checks(email, checks).deliver_later
  77. end
  78. end
  79. end