Checks table in error with consecutive_failures configuration

This commit is contained in:
Colin Darie 2018-07-24 14:57:26 +02:00
parent afcc72ca07
commit d61b6d9c8a
No known key found for this signature in database
GPG Key ID: 4FB865FDBCA4BCC4
5 changed files with 12 additions and 58 deletions

View File

@ -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])

View File

@ -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,

View File

@ -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?

View File

@ -26,7 +26,7 @@
<span class="badge badge-info"><%= t(".kind_labels.#{check.kind}") %></span>
</td>
<td>
<%= check_in_error(check) if check.in_error? %>
<%= check_error(check) if check_in_error?(check) %>
<strong><%= check.domain %></strong>
</td>
<td>

View File

@ -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