From d61b6d9c8a8df831012315e042c8a163f52b8e3d Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Tue, 24 Jul 2018 14:57:26 +0200 Subject: [PATCH] Checks table in error with consecutive_failures configuration --- app/controllers/checks_controller.rb | 4 ++- app/helpers/checks_helper.rb | 7 ++++- app/models/check.rb | 17 ++---------- app/views/checks/_table.html.erb | 2 +- test/models/check_test.rb | 40 ---------------------------- 5 files changed, 12 insertions(+), 58 deletions(-) diff --git a/app/controllers/checks_controller.rb b/app/controllers/checks_controller.rb index d252411..cc12afd 100644 --- a/app/controllers/checks_controller.rb +++ b/app/controllers/checks_controller.rb @@ -6,7 +6,9 @@ class ChecksController < ApplicationController has_scope :kind has_scope :by_domain - has_scope :recurrent_failures, type: :boolean + has_scope :recurrent_failures, type: :boolean do |_controller, scope, _value| + scope.consecutive_failures(Rails.configuration.chexpire.interface.consecutive_failures_as_error) + end def index @checks = apply_scopes(policy_scope(Check)).order(Hash[*current_sort]).page(params[:page]) diff --git a/app/helpers/checks_helper.rb b/app/helpers/checks_helper.rb index f779644..beeea2e 100644 --- a/app/helpers/checks_helper.rb +++ b/app/helpers/checks_helper.rb @@ -29,7 +29,12 @@ module ChecksHelper end end - def check_in_error(check) + def check_in_error?(check) + check.consecutive_failures >= + Rails.configuration.chexpire.interface.consecutive_failures_as_error + end + + def check_error(check) content_tag( :span, Octicons::Octicon.new("alert", class: "ml-1").to_svg.html_safe, diff --git a/app/models/check.rb b/app/models/check.rb index 2afdea0..8e1cb6f 100644 --- a/app/models/check.rb +++ b/app/models/check.rb @@ -28,8 +28,6 @@ # class Check < ApplicationRecord - ERROR_DELAY_DAYS = 3 - belongs_to :user has_many :logs, class_name: "CheckLog", dependent: :destroy has_many :notifications, validate: true, dependent: :destroy @@ -64,25 +62,14 @@ class Check < ApplicationRecord scope :kind, ->(kind) { where(kind: kind) } scope :by_domain, ->(domain) { where("domain LIKE ?", "%#{domain}%") } - scope :recurrent_failures, -> { - interval = "INTERVAL #{ERROR_DELAY_DAYS} DAY" - where("last_run_at IS NOT NULL AND created_at <= DATE_SUB(NOW(), #{interval})") - .where("last_success_at IS NULL OR last_success_at <= DATE_SUB(last_run_at, #{interval})") + scope :consecutive_failures, ->(consecutive) { + where("consecutive_failures >= ?", consecutive) } def self.default_sort [:domain_expires_at, :asc] end - def in_error? - return false if created_at > ERROR_DELAY_DAYS.days.ago - return false if last_run_at.nil? - return true if last_success_at.nil? - return false if last_run_at == last_success_at - - last_success_at < ERROR_DELAY_DAYS.days.ago - end - def days_from_last_success return unless last_success_at.present? diff --git a/app/views/checks/_table.html.erb b/app/views/checks/_table.html.erb index f4020fa..3808d01 100644 --- a/app/views/checks/_table.html.erb +++ b/app/views/checks/_table.html.erb @@ -26,7 +26,7 @@ <%= t(".kind_labels.#{check.kind}") %> - <%= check_in_error(check) if check.in_error? %> + <%= check_error(check) if check_in_error?(check) %> <%= check.domain %> diff --git a/test/models/check_test.rb b/test/models/check_test.rb index 1fee4e8..be81d13 100644 --- a/test/models/check_test.rb +++ b/test/models/check_test.rb @@ -51,46 +51,6 @@ class CheckTest < ActiveSupport::TestCase assert_nil notification.sent_at end - test "in_error? for recently added" do - check = build(:check, created_at: 1.day.ago) - refute check.in_error? - - check = build(:check, created_at: 1.day.ago, last_run_at: 3.minutes.ago) - refute check.in_error? - - check = build(:check, created_at: 1.day.ago, last_success_at: 1.hour.ago) - refute check.in_error? - end - - test "in_error? for never success check, with at least 1 run" do - check = build(:check, created_at: 3.weeks.ago, last_run_at: 1.day.ago) - assert check.in_error? - - check = build(:check, created_at: 3.weeks.ago, last_run_at: 4.days.ago) - assert check.in_error? - end - - test "in_error? ignore check without run" do - check = build(:check, created_at: 3.weeks.ago) - refute check.in_error? - end - - test "in_error? for last success a few days ago" do - check = build(:check, created_at: 3.weeks.ago, - last_success_at: 10.days.ago, last_run_at: 1.day.ago) - assert check.in_error? - - check = build(:check, created_at: 3.weeks.ago, - last_success_at: 1.days.ago, last_run_at: 1.day.ago) - refute check.in_error? - end - - test "in_error? when last check occured a few days ago without error" do - check = build(:check, created_at: 3.weeks.ago, - last_success_at: 10.days.ago, last_run_at: 10.days.ago) - refute check.in_error? - end - test "days_from_last_success without any success" do check = build(:check) assert_nil check.days_from_last_success