-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add Profiler with ProfilerNotifier
- Loading branch information
1 parent
45e453d
commit 3743b27
Showing
8 changed files
with
86 additions
and
17 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,20 @@ | ||
module ValidationErrorReporter | ||
class ProfileFormatter < Formatter | ||
|
||
def output(profile) | ||
"Summary:\n" + | ||
"Model with most errors: #{dump(profile, :rate)}.\n" + | ||
"Model with highest error rate: #{dump(profile, :count)}." | ||
end | ||
|
||
private | ||
|
||
def dump(profile, sort_attribute) | ||
sorted = profile.sort_by {|_, hash| -hash[sort_attribute]}.first | ||
model_name, stats = sorted[0], sorted[1] | ||
|
||
"#{model_name} (#{stats[:total]} entries, #{stats[:count]} errors, #{stats[:rate]}%)" | ||
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,24 @@ | ||
module ValidationErrorReporter | ||
class Profiler | ||
attr_reader :formatter | ||
|
||
def initialize | ||
@profile = Hash.new { |h, k| h[k] = { count: 0, total: 0 } } | ||
@formatter = ProfileFormatter.new | ||
end | ||
|
||
def profile(errors) | ||
errors.each do |error| | ||
@profile[error.model_name][:count] += 1 | ||
@profile[error.model_name][:total] = error.model_total_count | ||
end | ||
|
||
@profile.each_value do |hash| | ||
hash[:rate] = hash[:count].to_f / hash[:total].to_f * 100 | ||
end | ||
|
||
@formatter.output(@profile) | ||
end | ||
|
||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -15,4 +15,10 @@ | |
) | ||
end | ||
end | ||
|
||
describe "#model_name" do | ||
end | ||
|
||
describe "#model_total_count" do | ||
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,18 @@ | ||
require "spec_helper" | ||
|
||
describe ValidationErrorReporter::Profiler do | ||
|
||
describe "#profile" do | ||
it "outputs a string with stats" do | ||
record = double(class: double(count: 2, model_name: double(name: "Project"))) | ||
error = ValidationErrorReporter::Error.new(record) | ||
|
||
expect(subject.profile([error])).to eq( | ||
"Summary:\n" + | ||
"Model with most errors: Project (2 entries, 1 errors, 50.0%).\n" + | ||
"Model with highest error rate: Project (2 entries, 1 errors, 50.0%)." | ||
) | ||
end | ||
end | ||
|
||
end |