Skip to content

Commit

Permalink
Added threshold check for each rule from config.yml file
Browse files Browse the repository at this point in the history
  • Loading branch information
makaroni4 committed May 12, 2015
1 parent 17f42b3 commit 6533cb2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/sandi_meter/cli.rb
Expand Up @@ -82,7 +82,7 @@ class << self
def execute
cli = CommandParser.new
cli.parse_options

cli.config[:output_path] ||= File.expand_path(File.join(cli.config[:path], 'sandi_meter'))

if cli.config[:graph]
Expand Down Expand Up @@ -136,7 +136,7 @@ def execute
config = if File.exists?(config_file_path)
YAML.load(File.read(config_file_path))
else
{ threshold: 90 }
{ thresholds: [90, 90, 90, 90] }
end

if RulesChecker.new(data, config).ok?
Expand Down
10 changes: 8 additions & 2 deletions lib/sandi_meter/rules_checker.rb
Expand Up @@ -10,12 +10,18 @@ def initialize(data, config)
end

def ok?
@rules.reduce(:+) / 4 > @config[:threshold]
if @config[:threshold]
puts "DEPRECATION WARNING: sandi_meter threshold will be deprecated. Set thresholds for each rule in sandi_meter config.yml"

@rules.reduce(:+) / 4 > @config[:threshold]
elsif @config[:thresholds]
@rules.each_with_index.map { |percentage, index| percentage >= @config[:thresholds][index].to_f }.reduce(:&)
end
end

private
def percentage(amount, total)
total > 0 ? (amount / total.to_f)*100 : 100
total > 0 ? (amount / total.to_f) * 100 : 100
end
end
end
6 changes: 5 additions & 1 deletion sandi_meter/config.yml
@@ -1,2 +1,6 @@
---
:threshold: 80
:thresholds:
- 80
- 50
- 90
- 90
2 changes: 1 addition & 1 deletion spec/cli_spec.rb
Expand Up @@ -17,7 +17,7 @@
FakeFS.deactivate!
end

describe '#execute' do
describe '#execute', silent_cli: true do
before do
@original_argv = ARGV
ARGV.clear
Expand Down
4 changes: 2 additions & 2 deletions spec/rules_checker_spec.rb
Expand Up @@ -13,11 +13,11 @@

describe "#ok?" do
it "returns false with 100 threshold" do
expect(SandiMeter::RulesChecker.new(conditions, {threshold: 100})).to_not be_ok
expect(SandiMeter::RulesChecker.new(conditions, {thresholds: [100, 100, 100, 100]})).to_not be_ok
end

it "returns true with threshold less than 100" do
expect(SandiMeter::RulesChecker.new(conditions, {threshold: 50})).to be_ok
expect(SandiMeter::RulesChecker.new(conditions, {thresholds: [50, 100, 100, 100]})).to be_ok
end
end
end
6 changes: 4 additions & 2 deletions spec/test_helper.rb
Expand Up @@ -6,12 +6,14 @@
RSpec.configure do |config|
original_stderr = $stderr
original_stdout = $stdout
config.before(:all) do

config.before(:all, silent_cli: true) do
# Redirect stderr and stdout
$stderr = File.open(File::NULL, "w")
$stdout = File.open(File::NULL, "w")
end
config.after(:all) do

config.after(:all, silent_cli: true) do
$stderr = original_stderr
$stdout = original_stdout
end
Expand Down

0 comments on commit 6533cb2

Please sign in to comment.