Skip to content

Commit

Permalink
WIP: Add Profiler with ProfilerNotifier
Browse files Browse the repository at this point in the history
  • Loading branch information
olejrosendahl committed Aug 28, 2015
1 parent 45e453d commit 3743b27
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/validation_error_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module ValidationErrorReporter
autoload :Formatter, "validation_error_reporter/formatter"
autoload :Reporter, "validation_error_reporter/reporter"
autoload :Notifier, "validation_error_reporter/notifier"
autoload :Profiler, "validation_error_reporter/profiler"
autoload :Error, "validation_error_reporter/error"
autoload :ProfileFormatter, "validation_error_reporter/profile_formatter"
end

Mail.defaults do
Expand Down
8 changes: 8 additions & 0 deletions lib/validation_error_reporter/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ def initialize(record)
@record = record
end

def model_name
record.class.model_name.name
end

def model_total_count
record.class.count
end

def to_s
"#{record.class.model_name.human} #{record.public_send(record.class.primary_key)}:\n" +
record.errors.full_messages.map {|e| " #{e}"}.join("\n")
Expand Down
20 changes: 20 additions & 0 deletions lib/validation_error_reporter/profile_formatter.rb
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
24 changes: 24 additions & 0 deletions lib/validation_error_reporter/profiler.rb
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
14 changes: 0 additions & 14 deletions lib/validation_error_reporter/profiler_notifier.rb

This file was deleted.

11 changes: 8 additions & 3 deletions lib/validation_error_reporter/reporter.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module ValidationErrorReporter

class Reporter
attr_reader :notifiers
attr_reader :notifiers, :profiler

def initialize
def initialize(profiler = false)
@notifiers = []
@notifiers << Notifier.new
@profiler = profiler ? Profiler.new : profiler
end

def report(errors)
Expand All @@ -14,6 +14,11 @@ def report(errors)
notifier.notify(error.to_s)
end
end

if profiler
profile = profiler.profile(errors)
notifiers.each { |notifier| notifier.notify(profile.to_s) }
end
end

end
Expand Down
6 changes: 6 additions & 0 deletions spec/validation_error_reporter/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@
)
end
end

describe "#model_name" do
end

describe "#model_total_count" do
end
end
18 changes: 18 additions & 0 deletions spec/validation_error_reporter/profiler_spec.rb
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

0 comments on commit 3743b27

Please sign in to comment.