21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-04-28 23:10:49 +02:00

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 :kind
has_scope :by_domain 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 def index
@checks = apply_scopes(policy_scope(Check)).order(Hash[*current_sort]).page(params[:page]) @checks = apply_scopes(policy_scope(Check)).order(Hash[*current_sort]).page(params[:page])

View file

@ -29,7 +29,12 @@ module ChecksHelper
end end
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( content_tag(
:span, :span,
Octicons::Octicon.new("alert", class: "ml-1").to_svg.html_safe, Octicons::Octicon.new("alert", class: "ml-1").to_svg.html_safe,

View file

@ -28,8 +28,6 @@
# #
class Check < ApplicationRecord class Check < ApplicationRecord
ERROR_DELAY_DAYS = 3
belongs_to :user belongs_to :user
has_many :logs, class_name: "CheckLog", dependent: :destroy has_many :logs, class_name: "CheckLog", dependent: :destroy
has_many :notifications, validate: true, dependent: :destroy has_many :notifications, validate: true, dependent: :destroy
@ -64,25 +62,14 @@ class Check < ApplicationRecord
scope :kind, ->(kind) { where(kind: kind) } scope :kind, ->(kind) { where(kind: kind) }
scope :by_domain, ->(domain) { where("domain LIKE ?", "%#{domain}%") } scope :by_domain, ->(domain) { where("domain LIKE ?", "%#{domain}%") }
scope :recurrent_failures, -> { scope :consecutive_failures, ->(consecutive) {
interval = "INTERVAL #{ERROR_DELAY_DAYS} DAY" where("consecutive_failures >= ?", consecutive)
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})")
} }
def self.default_sort def self.default_sort
[:domain_expires_at, :asc] [:domain_expires_at, :asc]
end 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 def days_from_last_success
return unless last_success_at.present? return unless last_success_at.present?

View file

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

View file

@ -51,46 +51,6 @@ class CheckTest < ActiveSupport::TestCase
assert_nil notification.sent_at assert_nil notification.sent_at
end 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 test "days_from_last_success without any success" do
check = build(:check) check = build(:check)
assert_nil check.days_from_last_success assert_nil check.days_from_last_success