diff --git a/app/models/problem_check_tracker.rb b/app/models/problem_check_tracker.rb index 20b74f52fac..4afc92fb9bf 100644 --- a/app/models/problem_check_tracker.rb +++ b/app/models/problem_check_tracker.rb @@ -28,6 +28,8 @@ class ProblemCheckTracker < ActiveRecord::Base update!(blips: blips + 1, details:, last_run_at: now, last_problem_at: now, next_run_at:) + update_notice_details(details) + sound_the_alarm if sound_the_alarm? end @@ -55,6 +57,18 @@ class ProblemCheckTracker < ActiveRecord::Base private + def resolved_target + self.target || ProblemCheck::NO_TARGET + end + + def details_with_target(details) + details.merge(target: resolved_target) + end + + def update_notice_details(details) + admin_notice.where(identifier:).update_all(details: details_with_target(details)) + end + def sound_the_alarm? failing? && blips > check.max_blips end @@ -62,7 +76,7 @@ class ProblemCheckTracker < ActiveRecord::Base def sound_the_alarm admin_notice.create_with( priority: check.priority, - details: details.merge(target: target || ProblemCheck::NO_TARGET), + details: details_with_target(self.details), ).find_or_create_by(identifier:) end @@ -71,7 +85,7 @@ class ProblemCheckTracker < ActiveRecord::Base end def admin_notice - AdminNotice.problem.where("details->>'target' = ?", target || ProblemCheck::NO_TARGET) + AdminNotice.problem.where("details->>'target' = ?", resolved_target) end end diff --git a/spec/models/problem_check_tracker_spec.rb b/spec/models/problem_check_tracker_spec.rb index dadeca7bb6f..d58495b3509 100644 --- a/spec/models/problem_check_tracker_spec.rb +++ b/spec/models/problem_check_tracker_spec.rb @@ -146,6 +146,42 @@ RSpec.describe ProblemCheckTracker do end end + context "when the details of the problem change but the problem remains" do + let(:blips) { 1 } + + it "updates the notice" do + original_details = { + themes_list: + "", + base_path: "", + } + + expect do problem_tracker.problem!(details: original_details) end.to change { + AdminNotice.problem.count + }.by(1) + + admin_notice = AdminNotice.problem.find_by(identifier: "twitter_login") + + expect( + admin_notice.details.merge(target: problem_tracker.target).with_indifferent_access, + ).to eq(original_details.merge(target: problem_tracker.target).with_indifferent_access) + + new_details = { + themes_list: "", + base_path: "", + } + expect do problem_tracker.problem!(details: new_details) end.not_to change { + AdminNotice.problem.count + } + + admin_notice.reload + + expect( + admin_notice.details.merge(target: problem_tracker.target).with_indifferent_access, + ).to eq(new_details.merge(target: problem_tracker.target).with_indifferent_access) + end + end + context "when there's an alarm sounding for multi-target trackers" do let(:blips) { 1 }