-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This makes it easier to implement a class of check which compares a value with a threshold.
- Loading branch information
1 parent
6e16859
commit f1a2519
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module GovukHealthcheck | ||
class ThresholdCheck | ||
def status | ||
if value >= critical_threshold | ||
:critical | ||
elsif value >= warning_threshold | ||
:warning | ||
else | ||
:ok | ||
end | ||
end | ||
|
||
def message | ||
if value >= critical_threshold | ||
"#{value} is above the critical threshold (#{critical_threshold})" | ||
elsif value >= warning_threshold | ||
"#{value} is above the warning threshold (#{warning_threshold})" | ||
else | ||
"#{value} is below the critical and warning thresholds" | ||
end | ||
end | ||
|
||
def details | ||
{ | ||
value: value, | ||
total: total, | ||
thresholds: { | ||
critical: critical_threshold, | ||
warning: warning_threshold, | ||
}, | ||
} | ||
end | ||
|
||
def value | ||
raise "This method must be overridden to be the check value." | ||
end | ||
|
||
def total | ||
nil # This method can be overriden to provide the total for the check. | ||
end | ||
|
||
def critical_threshold | ||
raise "This method must be overriden to be the critical threshold." | ||
end | ||
|
||
def warning_threshold | ||
raise "This method must be overriden to be the warning threshold." | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require "spec_helper" | ||
require "govuk_app_config/govuk_healthcheck" | ||
require_relative "shared_interface" | ||
|
||
RSpec.describe GovukHealthcheck::ThresholdCheck do | ||
context "an ok check" do | ||
subject { TestThresholdCheck.new(0, 10, 20) } | ||
|
||
it_behaves_like "a healthcheck" | ||
|
||
its(:status) { is_expected.to eq(:ok) } | ||
its(:message) { is_expected.to match(/below the critical and warning thresholds/) } | ||
its(:details) do | ||
is_expected.to match( | ||
hash_including(value: 0, thresholds: { warning: 10, critical: 20 }) | ||
) | ||
end | ||
end | ||
|
||
context "a warning check" do | ||
subject { TestThresholdCheck.new(11, 10, 20) } | ||
|
||
it_behaves_like "a healthcheck" | ||
|
||
its(:status) { is_expected.to eq(:warning) } | ||
its(:message) { is_expected.to match(/above the warning threshold/) } | ||
its(:details) do | ||
is_expected.to match( | ||
hash_including(value: 11, thresholds: { warning: 10, critical: 20 }) | ||
) | ||
end | ||
end | ||
|
||
context "a critical check" do | ||
subject { TestThresholdCheck.new(21, 10, 20) } | ||
|
||
it_behaves_like "a healthcheck" | ||
|
||
its(:status) { is_expected.to eq(:critical) } | ||
its(:message) { is_expected.to match(/above the critical threshold/) } | ||
its(:details) do | ||
is_expected.to match( | ||
hash_including(value: 21, thresholds: { warning: 10, critical: 20 }) | ||
) | ||
end | ||
end | ||
|
||
context "with a total" do | ||
subject { TestThresholdCheck.new(0, 10, 20, 40) } | ||
|
||
it_behaves_like "a healthcheck" | ||
|
||
its(:details) { is_expected.to match(hash_including(total: 40)) } | ||
end | ||
|
||
class TestThresholdCheck < GovukHealthcheck::ThresholdCheck | ||
def initialize(value, warning_threshold, critical_threshold, total = nil, name = :test) | ||
@value = value | ||
@warning_threshold = warning_threshold | ||
@critical_threshold = critical_threshold | ||
@total = total | ||
@name = name | ||
end | ||
|
||
attr_reader :value, :warning_threshold, :critical_threshold, :total, :name | ||
end | ||
end |