2024-02-26 22:17:39 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ProblemCheckTracker < ActiveRecord::Base
|
2024-05-22 21:29:08 -04:00
|
|
|
validates :identifier, presence: true, uniqueness: { scope: :target }
|
2024-02-26 22:17:39 -05:00
|
|
|
validates :blips, presence: true, numericality: { greater_than_or_equal_to: 0 }
|
|
|
|
|
2024-05-22 21:29:08 -04:00
|
|
|
scope :failing, -> { where("last_problem_at = last_run_at") }
|
|
|
|
scope :passing, -> { where("last_success_at = last_run_at") }
|
|
|
|
|
2024-10-20 22:45:09 -04:00
|
|
|
def self.[](identifier, target = ProblemCheck::NO_TARGET)
|
2024-05-22 21:29:08 -04:00
|
|
|
find_or_create_by(identifier:, target:)
|
2024-02-26 22:17:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def ready_to_run?
|
|
|
|
next_run_at.blank? || next_run_at.past?
|
|
|
|
end
|
|
|
|
|
2024-05-22 21:29:08 -04:00
|
|
|
def failing?
|
|
|
|
last_problem_at == last_run_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def passing?
|
|
|
|
last_success_at == last_run_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def problem!(next_run_at: nil, details: {})
|
2024-02-26 22:17:39 -05:00
|
|
|
now = Time.current
|
|
|
|
|
2024-05-22 21:29:08 -04:00
|
|
|
update!(blips: blips + 1, details:, last_run_at: now, last_problem_at: now, next_run_at:)
|
|
|
|
|
|
|
|
sound_the_alarm if sound_the_alarm?
|
2024-02-26 22:17:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def no_problem!(next_run_at: nil)
|
2024-09-17 02:43:34 -04:00
|
|
|
reset
|
|
|
|
silence_the_alarm
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset(next_run_at: nil)
|
2024-02-26 22:17:39 -05:00
|
|
|
now = Time.current
|
|
|
|
|
|
|
|
update!(blips: 0, last_run_at: now, last_success_at: now, next_run_at:)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check
|
2024-09-17 22:11:52 -04:00
|
|
|
check = ProblemCheck[identifier]
|
|
|
|
|
|
|
|
return check if check.present?
|
|
|
|
|
|
|
|
silence_the_alarm
|
|
|
|
destroy
|
|
|
|
|
|
|
|
nil
|
2024-02-26 22:17:39 -05:00
|
|
|
end
|
2024-03-26 22:07:56 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def sound_the_alarm?
|
2024-05-22 21:29:08 -04:00
|
|
|
failing? && blips > check.max_blips
|
|
|
|
end
|
|
|
|
|
|
|
|
def sound_the_alarm
|
|
|
|
admin_notice.create_with(
|
|
|
|
priority: check.priority,
|
2024-10-21 23:17:27 -04:00
|
|
|
details: details.merge(target: target || ProblemCheck::NO_TARGET),
|
2024-05-22 21:29:08 -04:00
|
|
|
).find_or_create_by(identifier:)
|
|
|
|
end
|
|
|
|
|
|
|
|
def silence_the_alarm
|
|
|
|
admin_notice.where(identifier:).delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
def admin_notice
|
2024-10-20 22:45:09 -04:00
|
|
|
AdminNotice.problem.where("details->>'target' = ?", target || ProblemCheck::NO_TARGET)
|
2024-03-26 22:07:56 -04:00
|
|
|
end
|
2024-02-26 22:17:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: problem_check_trackers
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
|
|
|
# identifier :string not null
|
|
|
|
# blips :integer default(0), not null
|
|
|
|
# last_run_at :datetime
|
|
|
|
# next_run_at :datetime
|
|
|
|
# last_success_at :datetime
|
|
|
|
# last_problem_at :datetime
|
2024-05-22 21:29:08 -04:00
|
|
|
# details :json
|
2024-10-20 22:45:09 -04:00
|
|
|
# target :string default("__NULL__")
|
2024-02-26 22:17:39 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2024-05-22 21:29:08 -04:00
|
|
|
# index_problem_check_trackers_on_identifier_and_target (identifier,target) UNIQUE
|
2024-02-26 22:17:39 -05:00
|
|
|
#
|