eager_eye 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +35 -0
- data/CHANGELOG.md +45 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +310 -0
- data/Rakefile +12 -0
- data/examples/github_action.yml +75 -0
- data/exe/eager_eye +6 -0
- data/lib/eager_eye/analyzer.rb +78 -0
- data/lib/eager_eye/cli.rb +120 -0
- data/lib/eager_eye/configuration.rb +16 -0
- data/lib/eager_eye/detectors/base.rb +48 -0
- data/lib/eager_eye/detectors/loop_association.rb +124 -0
- data/lib/eager_eye/detectors/missing_counter_cache.rb +73 -0
- data/lib/eager_eye/detectors/serializer_nesting.rb +192 -0
- data/lib/eager_eye/generators/install_generator.rb +41 -0
- data/lib/eager_eye/issue.rb +58 -0
- data/lib/eager_eye/railtie.rb +63 -0
- data/lib/eager_eye/reporters/base.rb +31 -0
- data/lib/eager_eye/reporters/console.rb +96 -0
- data/lib/eager_eye/reporters/json.rb +34 -0
- data/lib/eager_eye/version.rb +5 -0
- data/lib/eager_eye.rb +35 -0
- data/sig/eager_eye.rbs +4 -0
- metadata +103 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: dce399f8b8160530246911ddbd4c9c5a9fb6cad487d409924c6e520993ff37cc
|
|
4
|
+
data.tar.gz: 3be96cbcf42c206f0348c63a91991c1a872addd775d5054be9e3dc44cc8b4f52
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 23923fe65f0f51de105b9a6df93a8a115d9ceb40042dedc9d112ccfbdc48a324935cd48f18306944708ca355cb463caad4b97b994f619dff24d8c6cf475180c0
|
|
7
|
+
data.tar.gz: efc5cfe923e04b55b3e5f2a9a6b201298176e4370a5cac92825bd01649ec528ea978fb378151ea7b74c2a82ce6194a02df6ce8c52018725f86bd83736410b71d
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 3.1
|
|
3
|
+
NewCops: enable
|
|
4
|
+
SuggestExtensions: false
|
|
5
|
+
|
|
6
|
+
Style/StringLiterals:
|
|
7
|
+
EnforcedStyle: double_quotes
|
|
8
|
+
|
|
9
|
+
Style/StringLiteralsInInterpolation:
|
|
10
|
+
EnforcedStyle: double_quotes
|
|
11
|
+
|
|
12
|
+
Style/Documentation:
|
|
13
|
+
Enabled: false
|
|
14
|
+
|
|
15
|
+
Metrics/BlockLength:
|
|
16
|
+
Max: 35
|
|
17
|
+
Exclude:
|
|
18
|
+
- "spec/**/*"
|
|
19
|
+
- "*.gemspec"
|
|
20
|
+
- "lib/eager_eye/railtie.rb"
|
|
21
|
+
|
|
22
|
+
Metrics/ParameterLists:
|
|
23
|
+
Max: 6
|
|
24
|
+
|
|
25
|
+
Metrics/AbcSize:
|
|
26
|
+
Max: 35
|
|
27
|
+
|
|
28
|
+
Metrics/MethodLength:
|
|
29
|
+
Max: 30
|
|
30
|
+
|
|
31
|
+
Metrics/ClassLength:
|
|
32
|
+
Max: 150
|
|
33
|
+
|
|
34
|
+
Layout/FirstArrayElementIndentation:
|
|
35
|
+
EnforcedStyle: consistent
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0] - 2025-12-15
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- **Static Analysis Engine**: AST-based code analysis using the `parser` gem
|
|
15
|
+
- **Three N+1 Detectors**:
|
|
16
|
+
- `LoopAssociation`: Detects association calls inside iteration blocks (each, map, select, etc.)
|
|
17
|
+
- `SerializerNesting`: Detects nested association access in serializer blocks (ActiveModel::Serializer, Blueprinter, Alba)
|
|
18
|
+
- `MissingCounterCache`: Detects `.count`, `.size`, `.length` calls on associations
|
|
19
|
+
- **CLI Interface**: Full-featured command line tool with options:
|
|
20
|
+
- `--format json|console`: Output format selection
|
|
21
|
+
- `--exclude PATTERN`: Exclude files matching glob patterns
|
|
22
|
+
- `--only DETECTORS`: Run specific detectors only
|
|
23
|
+
- `--no-fail`: Exit with 0 even when issues found
|
|
24
|
+
- `--no-color`: Disable colored output
|
|
25
|
+
- `--version`, `--help`: Standard CLI options
|
|
26
|
+
- **Rails Integration**:
|
|
27
|
+
- Railtie with `rake eager_eye:analyze` and `rake eager_eye:json` tasks
|
|
28
|
+
- Install generator: `rails g eager_eye:install`
|
|
29
|
+
- Support for `.eager_eye.yml` configuration file
|
|
30
|
+
- **Reporters**:
|
|
31
|
+
- Console reporter with colored, human-readable output
|
|
32
|
+
- JSON reporter for CI integration
|
|
33
|
+
- **Configuration System**:
|
|
34
|
+
- `excluded_paths`: Glob patterns to exclude from analysis
|
|
35
|
+
- `enabled_detectors`: Select which detectors to run
|
|
36
|
+
- `app_path`: Base path for analysis (default: "app")
|
|
37
|
+
- `fail_on_issues`: Control exit code behavior
|
|
38
|
+
- **GitHub Actions**: CI workflow with Ruby 3.1, 3.2, 3.3 matrix testing
|
|
39
|
+
- **Comprehensive Test Suite**: 140 examples with 95.8% code coverage
|
|
40
|
+
|
|
41
|
+
### Technical Details
|
|
42
|
+
|
|
43
|
+
- Minimum Ruby version: 3.1.0
|
|
44
|
+
- Dependencies: `parser ~> 3.3`, `ast ~> 2.4`
|
|
45
|
+
- License: MIT
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 hamzagedikkaya
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
# EagerEye
|
|
2
|
+
|
|
3
|
+
[](https://github.com/hamzagedikkaya/eager_eye/actions/workflows/main.yml)
|
|
4
|
+
[](https://badge.fury.io/rb/eager_eye)
|
|
5
|
+
[](https://www.ruby-lang.org/)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
**Static analysis tool for detecting N+1 queries in Rails applications.**
|
|
9
|
+
|
|
10
|
+
EagerEye analyzes your Ruby code without running it, using AST (Abstract Syntax Tree) parsing to find potential N+1 query issues before they hit production.
|
|
11
|
+
|
|
12
|
+
## Why EagerEye?
|
|
13
|
+
|
|
14
|
+
Unlike runtime tools like Bullet, EagerEye:
|
|
15
|
+
|
|
16
|
+
- **Runs without executing code** - Works in CI pipelines without a test suite
|
|
17
|
+
- **Catches more patterns** - Detects serializer N+1s and missing counter caches
|
|
18
|
+
- **Proactive detection** - Finds issues at code review time, not after deployment
|
|
19
|
+
|
|
20
|
+
| Feature | EagerEye | Bullet |
|
|
21
|
+
|---------|----------|--------|
|
|
22
|
+
| Detection method | Static analysis | Runtime |
|
|
23
|
+
| Requires test suite | No | Yes |
|
|
24
|
+
| Serializer N+1 detection | Yes | Limited |
|
|
25
|
+
| Counter cache suggestions | Yes | No |
|
|
26
|
+
| CI integration | Native | Requires tests |
|
|
27
|
+
| False positive rate | Higher | Lower |
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
Add to your Gemfile:
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
gem "eager_eye", group: :development
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Then run:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
bundle install
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Or install standalone:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
gem install eager_eye
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Quick Start
|
|
50
|
+
|
|
51
|
+
### CLI Usage
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
# Analyze default app/ directory
|
|
55
|
+
eager_eye
|
|
56
|
+
|
|
57
|
+
# Analyze specific paths
|
|
58
|
+
eager_eye app/controllers app/serializers
|
|
59
|
+
|
|
60
|
+
# Output as JSON (for CI)
|
|
61
|
+
eager_eye --format json
|
|
62
|
+
|
|
63
|
+
# Don't fail on issues (exit 0)
|
|
64
|
+
eager_eye --no-fail
|
|
65
|
+
|
|
66
|
+
# Run specific detectors only
|
|
67
|
+
eager_eye --only loop_association,serializer_nesting
|
|
68
|
+
|
|
69
|
+
# Exclude paths
|
|
70
|
+
eager_eye --exclude "app/legacy/**"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Rails Integration
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
# Generate config file
|
|
77
|
+
rails g eager_eye:install
|
|
78
|
+
|
|
79
|
+
# Run via rake
|
|
80
|
+
rake eager_eye:analyze
|
|
81
|
+
|
|
82
|
+
# JSON output
|
|
83
|
+
rake eager_eye:json
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Detected Issues
|
|
87
|
+
|
|
88
|
+
### 1. Loop Association (N+1 in iterations)
|
|
89
|
+
|
|
90
|
+
Detects association calls inside loops that may cause N+1 queries.
|
|
91
|
+
|
|
92
|
+
```ruby
|
|
93
|
+
# Bad - N+1 query on each iteration
|
|
94
|
+
posts.each do |post|
|
|
95
|
+
post.author.name # Query for each post!
|
|
96
|
+
post.comments.count # Another query for each post!
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Good - Eager load associations
|
|
100
|
+
posts.includes(:author, :comments).each do |post|
|
|
101
|
+
post.author.name # No additional query
|
|
102
|
+
post.comments.count # No additional query
|
|
103
|
+
end
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 2. Serializer Nesting (N+1 in serializers)
|
|
107
|
+
|
|
108
|
+
Detects nested association access in serializer blocks.
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
# Bad - N+1 in serializer
|
|
112
|
+
class PostSerializer < ActiveModel::Serializer
|
|
113
|
+
attribute :author_name do
|
|
114
|
+
object.author.name # Query for each serialized post!
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Good - Eager load in controller
|
|
119
|
+
class PostsController < ApplicationController
|
|
120
|
+
def index
|
|
121
|
+
@posts = Post.includes(:author)
|
|
122
|
+
render json: @posts, each_serializer: PostSerializer
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Supports multiple serializer libraries:
|
|
128
|
+
- ActiveModel::Serializers
|
|
129
|
+
- Blueprinter
|
|
130
|
+
- Alba
|
|
131
|
+
|
|
132
|
+
### 3. Missing Counter Cache
|
|
133
|
+
|
|
134
|
+
Detects `.count` or `.size` calls on associations that could benefit from counter caches.
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
# Bad - COUNT query every time
|
|
138
|
+
post.comments.count
|
|
139
|
+
post.comments.size
|
|
140
|
+
|
|
141
|
+
# Good - Add counter cache
|
|
142
|
+
# In Comment model:
|
|
143
|
+
belongs_to :post, counter_cache: true
|
|
144
|
+
|
|
145
|
+
# Then this is a simple column read:
|
|
146
|
+
post.comments_count
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Configuration
|
|
150
|
+
|
|
151
|
+
### Config File (.eager_eye.yml)
|
|
152
|
+
|
|
153
|
+
```yaml
|
|
154
|
+
# Paths to exclude from analysis (glob patterns)
|
|
155
|
+
excluded_paths:
|
|
156
|
+
- app/serializers/legacy/**
|
|
157
|
+
- lib/tasks/**
|
|
158
|
+
|
|
159
|
+
# Detectors to enable (default: all)
|
|
160
|
+
enabled_detectors:
|
|
161
|
+
- loop_association
|
|
162
|
+
- serializer_nesting
|
|
163
|
+
- missing_counter_cache
|
|
164
|
+
|
|
165
|
+
# Base path to analyze (default: app)
|
|
166
|
+
app_path: app
|
|
167
|
+
|
|
168
|
+
# Exit with error code when issues found (default: true)
|
|
169
|
+
fail_on_issues: true
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Programmatic Configuration
|
|
173
|
+
|
|
174
|
+
```ruby
|
|
175
|
+
EagerEye.configure do |config|
|
|
176
|
+
config.excluded_paths = ["app/legacy/**"]
|
|
177
|
+
config.enabled_detectors = [:loop_association, :serializer_nesting]
|
|
178
|
+
config.app_path = "app"
|
|
179
|
+
config.fail_on_issues = true
|
|
180
|
+
end
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## CI Integration
|
|
184
|
+
|
|
185
|
+
### GitHub Actions
|
|
186
|
+
|
|
187
|
+
```yaml
|
|
188
|
+
name: EagerEye
|
|
189
|
+
on: [pull_request]
|
|
190
|
+
|
|
191
|
+
jobs:
|
|
192
|
+
analyze:
|
|
193
|
+
runs-on: ubuntu-latest
|
|
194
|
+
steps:
|
|
195
|
+
- uses: actions/checkout@v4
|
|
196
|
+
- uses: ruby/setup-ruby@v1
|
|
197
|
+
with:
|
|
198
|
+
ruby-version: "3.3"
|
|
199
|
+
- run: gem install eager_eye
|
|
200
|
+
- run: eager_eye app/ --format json > report.json
|
|
201
|
+
- name: Check results
|
|
202
|
+
run: |
|
|
203
|
+
issues=$(cat report.json | ruby -rjson -e 'puts JSON.parse(STDIN.read)["summary"]["total_issues"]')
|
|
204
|
+
if [ "$issues" -gt 0 ]; then
|
|
205
|
+
echo "::warning::Found $issues potential N+1 issues"
|
|
206
|
+
fi
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
See [examples/github_action.yml](examples/github_action.yml) for a complete example with PR annotations.
|
|
210
|
+
|
|
211
|
+
## CLI Reference
|
|
212
|
+
|
|
213
|
+
```
|
|
214
|
+
Usage: eager_eye [paths] [options]
|
|
215
|
+
|
|
216
|
+
Options:
|
|
217
|
+
-f, --format FORMAT Output format: console, json (default: console)
|
|
218
|
+
-e, --exclude PATTERN Exclude files matching pattern (can be used multiple times)
|
|
219
|
+
-o, --only DETECTORS Run only specified detectors (comma-separated)
|
|
220
|
+
--no-fail Exit with 0 even when issues are found
|
|
221
|
+
--no-color Disable colored output
|
|
222
|
+
-v, --version Show version
|
|
223
|
+
-h, --help Show this help message
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Output Formats
|
|
227
|
+
|
|
228
|
+
### Console (default)
|
|
229
|
+
|
|
230
|
+
```
|
|
231
|
+
EagerEye Analysis Results
|
|
232
|
+
=========================
|
|
233
|
+
|
|
234
|
+
app/controllers/posts_controller.rb
|
|
235
|
+
Line 15: [LoopAssociation] Potential N+1 query: `post.author` called inside iteration
|
|
236
|
+
Suggestion: Consider using `includes(:author)` on the collection before iterating
|
|
237
|
+
|
|
238
|
+
Line 23: [MissingCounterCache] `.count` called on `comments` may cause N+1 queries
|
|
239
|
+
Suggestion: Consider adding `counter_cache: true` to the belongs_to association
|
|
240
|
+
|
|
241
|
+
----------------------------------------
|
|
242
|
+
Total: 2 issues (2 warnings, 0 errors)
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### JSON
|
|
246
|
+
|
|
247
|
+
```json
|
|
248
|
+
{
|
|
249
|
+
"summary": {
|
|
250
|
+
"total_issues": 2,
|
|
251
|
+
"warnings": 2,
|
|
252
|
+
"errors": 0,
|
|
253
|
+
"files_analyzed": 15
|
|
254
|
+
},
|
|
255
|
+
"issues": [
|
|
256
|
+
{
|
|
257
|
+
"detector": "loop_association",
|
|
258
|
+
"file_path": "app/controllers/posts_controller.rb",
|
|
259
|
+
"line_number": 15,
|
|
260
|
+
"message": "Potential N+1 query: `post.author` called inside iteration",
|
|
261
|
+
"severity": "warning",
|
|
262
|
+
"suggestion": "Consider using `includes(:author)` on the collection"
|
|
263
|
+
}
|
|
264
|
+
]
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Limitations
|
|
269
|
+
|
|
270
|
+
EagerEye uses static analysis, which means:
|
|
271
|
+
|
|
272
|
+
- **No runtime context** - Cannot know if associations are already eager loaded elsewhere
|
|
273
|
+
- **Heuristic-based** - Uses naming conventions to identify associations (may have false positives)
|
|
274
|
+
- **Ruby code only** - Does not analyze SQL queries or ActiveRecord internals
|
|
275
|
+
|
|
276
|
+
For best results, use EagerEye alongside runtime tools like Bullet for comprehensive N+1 detection.
|
|
277
|
+
|
|
278
|
+
## Development
|
|
279
|
+
|
|
280
|
+
```bash
|
|
281
|
+
# Setup
|
|
282
|
+
bin/setup
|
|
283
|
+
|
|
284
|
+
# Run tests
|
|
285
|
+
bundle exec rspec
|
|
286
|
+
|
|
287
|
+
# Run linter
|
|
288
|
+
bundle exec rubocop
|
|
289
|
+
|
|
290
|
+
# Interactive console
|
|
291
|
+
bin/console
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## Contributing
|
|
295
|
+
|
|
296
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hamzagedikkaya/eager_eye.
|
|
297
|
+
|
|
298
|
+
1. Fork the repository
|
|
299
|
+
2. Create your feature branch (`git checkout -b feature/my-feature`)
|
|
300
|
+
3. Commit your changes (`git commit -am 'Add my feature'`)
|
|
301
|
+
4. Push to the branch (`git push origin feature/my-feature`)
|
|
302
|
+
5. Create a Pull Request
|
|
303
|
+
|
|
304
|
+
## License
|
|
305
|
+
|
|
306
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
307
|
+
|
|
308
|
+
## Code of Conduct
|
|
309
|
+
|
|
310
|
+
Everyone interacting in the EagerEye project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/hamzagedikkaya/eager_eye/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Example GitHub Action workflow for using EagerEye in your Rails project
|
|
2
|
+
# Copy this file to .github/workflows/eager_eye.yml in your repository
|
|
3
|
+
|
|
4
|
+
name: EagerEye N+1 Analysis
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
pull_request:
|
|
8
|
+
paths:
|
|
9
|
+
- "app/**/*.rb"
|
|
10
|
+
- "lib/**/*.rb"
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
analyze:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
name: N+1 Query Detection
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- name: Checkout code
|
|
19
|
+
uses: actions/checkout@v4
|
|
20
|
+
|
|
21
|
+
- name: Set up Ruby
|
|
22
|
+
uses: ruby/setup-ruby@v1
|
|
23
|
+
with:
|
|
24
|
+
ruby-version: "3.3"
|
|
25
|
+
|
|
26
|
+
- name: Install EagerEye
|
|
27
|
+
run: gem install eager_eye
|
|
28
|
+
|
|
29
|
+
- name: Run EagerEye analysis
|
|
30
|
+
run: eager_eye app/ --format json > eager_eye_report.json
|
|
31
|
+
continue-on-error: true
|
|
32
|
+
|
|
33
|
+
- name: Upload report artifact
|
|
34
|
+
uses: actions/upload-artifact@v4
|
|
35
|
+
with:
|
|
36
|
+
name: eager-eye-report
|
|
37
|
+
path: eager_eye_report.json
|
|
38
|
+
|
|
39
|
+
- name: Check for N+1 issues
|
|
40
|
+
run: |
|
|
41
|
+
if [ -f eager_eye_report.json ]; then
|
|
42
|
+
issues=$(cat eager_eye_report.json | ruby -rjson -e 'puts JSON.parse(STDIN.read)["summary"]["total_issues"]')
|
|
43
|
+
if [ "$issues" -gt 0 ]; then
|
|
44
|
+
echo "::warning::EagerEye detected $issues potential N+1 query issue(s)"
|
|
45
|
+
cat eager_eye_report.json | ruby -rjson -e '
|
|
46
|
+
data = JSON.parse(STDIN.read)
|
|
47
|
+
data["issues"].each do |issue|
|
|
48
|
+
puts "::warning file=#{issue["file_path"]},line=#{issue["line_number"]}::#{issue["message"]}"
|
|
49
|
+
end
|
|
50
|
+
'
|
|
51
|
+
else
|
|
52
|
+
echo "No N+1 query issues detected!"
|
|
53
|
+
fi
|
|
54
|
+
fi
|
|
55
|
+
|
|
56
|
+
# Alternative: Strict mode that fails the build on issues
|
|
57
|
+
analyze-strict:
|
|
58
|
+
runs-on: ubuntu-latest
|
|
59
|
+
name: N+1 Query Detection (Strict)
|
|
60
|
+
if: false # Set to true to enable strict mode
|
|
61
|
+
|
|
62
|
+
steps:
|
|
63
|
+
- name: Checkout code
|
|
64
|
+
uses: actions/checkout@v4
|
|
65
|
+
|
|
66
|
+
- name: Set up Ruby
|
|
67
|
+
uses: ruby/setup-ruby@v1
|
|
68
|
+
with:
|
|
69
|
+
ruby-version: "3.3"
|
|
70
|
+
|
|
71
|
+
- name: Install EagerEye
|
|
72
|
+
run: gem install eager_eye
|
|
73
|
+
|
|
74
|
+
- name: Run EagerEye analysis (strict)
|
|
75
|
+
run: eager_eye app/ --no-color
|