-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
144 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
## master (unreleased) | ||
|
||
- Allow to use custom reporters | ||
|
||
```ruby | ||
class MyReporter | ||
def report(title, created_records) | ||
# do actual reporting | ||
end | ||
end | ||
|
||
ColumnsTrace.reporter = MyReporter.new | ||
``` | ||
|
||
`ColumnsTrace.logger=` setting is removed in favor of using `ColumnsTrace.reporter=`. | ||
If you configured custom logger, you should now configure it via: | ||
|
||
```ruby | ||
logger = ActiveSupport::Logger.new("/my/custom/logger.log") | ||
ColumnsTrace.reporter = ColumnsTrace::LogReporter.new(logger) | ||
``` | ||
|
||
## 0.1.0 (2023-10-04) | ||
|
||
- First release |
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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module ColumnsTrace | ||
# Class that is used to store metadata about created ActiveRecord records. | ||
class CreatedRecord | ||
attr_reader :model, :record, :backtrace | ||
|
||
def initialize(record, backtrace) | ||
@model = record.class | ||
@record = record | ||
@backtrace = backtrace | ||
end | ||
|
||
def accessed_fields | ||
record.accessed_fields | ||
end | ||
|
||
def unused_fields | ||
# We need to store this into local variable, because `record.attributes` | ||
# will access all attributes. | ||
accessed = accessed_fields | ||
record.attributes.keys - accessed | ||
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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module ColumnsTrace | ||
# Reporter that reports into the provided logger. | ||
class LogReporter | ||
def initialize(logger) | ||
@logger = logger | ||
end | ||
|
||
# Main reporter's method | ||
# | ||
# @param title [String] title of the reporting, e.g. controller action etc | ||
# @param created_records [Array<ColumnsTrace::CreatedRecord>] items that hold | ||
# metadata about created records | ||
# | ||
def report(title, created_records) | ||
lines = [] | ||
|
||
created_records.group_by(&:model).each do |model, grouped_created_records| | ||
lines.concat(lines_for_created_records(model, grouped_created_records)) | ||
end | ||
|
||
if lines.any? | ||
@logger.info("#{title}\n#{lines.join("\n")}") | ||
end | ||
end | ||
|
||
private | ||
def lines_for_created_records(model, created_records) | ||
lines = [] | ||
|
||
created_records.group_by(&:backtrace).each do |backtrace, grouped_created_records| | ||
accessed = accessed_fields(grouped_created_records) | ||
unused = grouped_created_records.first.unused_fields - accessed | ||
unused.reject! { |column| ColumnsTrace.ignored_column?(model, column) } | ||
|
||
if unused.any? | ||
records_text = "record".pluralize(grouped_created_records.size) | ||
lines << <<-MSG | ||
#{grouped_created_records.size} #{model.name} #{records_text}: unused columns - #{format_columns(unused)}; used columns - #{format_columns(accessed)} | ||
↳ #{backtrace.join("\n ")} | ||
MSG | ||
end | ||
end | ||
|
||
lines | ||
end | ||
|
||
def accessed_fields(created_records) | ||
created_records.map(&:accessed_fields).flatten.uniq | ||
end | ||
|
||
def format_columns(columns) | ||
columns.map(&:inspect).join(", ") | ||
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
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 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