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}") %>