rake_unused_routes 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8ccf126f687fa1e50d22d96fb33aea9bd475179
4
- data.tar.gz: bdfb2c94efdef7c19f9c06edb2f25e17bce0844c
3
+ metadata.gz: 1a17210f5e8b7f3da8f29b2a9ad27ad0497a09a8
4
+ data.tar.gz: 9dca0d92eefe227571d90ddcaa68223f114b248c
5
5
  SHA512:
6
- metadata.gz: 65cfa2206a7db65743ccdf448a347e267c936b2fda4ccb4ef30777ad01c08fb6a4d8c9c62d05142ca140af2865c2f5cb0f0d1591a70413565a3f966918b49ecf
7
- data.tar.gz: 4b8caa70797290cf773ab19fb1853cc8c7ee6d9acfa3420d26d05fae7df96122f155e4727e9538fb0ae413b3b33fe0dbae86a9167acad0d8b3abad8b69025b3f
6
+ metadata.gz: da5f8d78ce6a174a3e215a4171a9356aa25aafb10497fdf9ba12c5de518d1ea76e1aa70e40c8454f0e7b38f350b80c3cc2494d391b38b79ad0d9f55204aa6797
7
+ data.tar.gz: 9b322bd612de174665527b188be86b3b1c5b7e5dd0f9e00418224c180d995e5d7373614442514567d8b4900b19d58129566c3c6121da9bceb480b865a46264d6
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in rake_unused_routes.gemspec
4
4
  gemspec
5
+ gem "simplecov"
6
+ gem "codeclimate-test-reporter", "~> 1.0.0"
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
+ [![CircleCI](https://circleci.com/gh/viewthespace/rake_unused_routes.svg?style=svg)](https://circleci.com/gh/viewthespace/rake_unused_routes)
2
+ [![Code Climate](https://codeclimate.com/repos/585f1177508ac60076005e46/badges/bfd930692c43034fec1f/gpa.svg)](https://codeclimate.com/repos/585f1177508ac60076005e46/feed)
3
+ [![Test Coverage](https://codeclimate.com/repos/585f1177508ac60076005e46/badges/bfd930692c43034fec1f/coverage.svg)](https://codeclimate.com/repos/585f1177508ac60076005e46/coverage)
4
+
1
5
  # RakeUnusedRoutes
2
6
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rake_unused_routes`. To experiment with that code, run `bin/console` for an interactive prompt.
7
+ Identify unused routes within your rails routes.rb file by reading in a csv file of all controllers hit within a given time period. The csv file can most easily be generated with New Relic.
4
8
 
5
- TODO: Delete this and the text above, and describe your gem
9
+ This gem is inspired by [newrelic_route_check](https://github.com/livingsocial/newrelic_route_check)
6
10
 
7
11
  ## Installation
8
12
 
@@ -16,13 +20,62 @@ And then execute:
16
20
 
17
21
  $ bundle
18
22
 
19
- Or install it yourself as:
20
-
21
- $ gem install rake_unused_routes
22
23
 
23
24
  ## Usage
24
25
 
25
- TODO: Write usage instructions here
26
+ In order to see unused routes, we need an export of all controller actions that are still in use. An easy way of generating the export is with either the basic NewRelic RPM or NewRelic Insights.
27
+
28
+ ### From a NewRelic RPM Web Transactions CSV
29
+
30
+ #### Generate the CSV
31
+
32
+ The easiest way to generate a CSV file is by exporting all controllers from the all transactions page within newrelic. To get there:
33
+
34
+ 1. Navigate to Transactions
35
+ 1. Select "Last 7 Days ending now" within time picker
36
+ 1. Pick type Web
37
+ 1. Click "Show all transactions table"
38
+ 1. Click "Export as CSV"
39
+
40
+ ![New Relic Transactions](https://vts-monosnap.s3.amazonaws.com/Transactions_-_VTS_Production_-_New_Relic_2016-12-22_17-48-43__eprql.png)
41
+
42
+ Copy exported file to $RAILS_ROOT/tmp/controller_summary.csv
43
+
44
+ #### Generate unused_routes
45
+
46
+ Run:
47
+
48
+ ```
49
+ rake unused_routes
50
+ ```
51
+ ### From a NewRelic Insights export
52
+
53
+ The advantage of using NewRelic Insights export is that dependening on your retention policies, Insights can generate a report for a much great length of time than 7 days. Our current NewRelic policies allow for pulling usage data from 9 weeks ago.
54
+
55
+ #### Generate the CSV
56
+
57
+ Navigate to Insights and run the following query:
58
+
59
+ ```sql
60
+ SELECT count(*)
61
+ from Transaction
62
+ where transactionSubType ='Controller'
63
+ facet name
64
+ since 9 weeks ago
65
+ limit 1000
66
+ ```
67
+
68
+ Export the results to a csv file:
69
+
70
+ ![Insight Export to CSV](https://vts-monosnap.s3.amazonaws.com/Insights_Home_2016-12-26_15-06-52__icat6.png)
71
+
72
+ Copy the results to $RAILS_ROOT/tmp/insights_controllers.csv .
73
+
74
+ Then run:
75
+
76
+ ```
77
+ rake unused_routes:from_insights
78
+ ```
26
79
 
27
80
  ## Development
28
81
 
@@ -32,7 +85,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
85
 
33
86
  ## Contributing
34
87
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rake_unused_routes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/viewthespace/rake_unused_routes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
89
 
37
90
 
38
91
  ## License
@@ -1,4 +1,5 @@
1
1
  require "rake_unused_routes/version"
2
+ require "rake_unused_routes/insights_formatter"
2
3
  require 'csv'
3
4
 
4
5
  class RakeUnusedRoutes
@@ -9,14 +10,14 @@ class RakeUnusedRoutes
9
10
  end
10
11
  end
11
12
 
12
- def initialize app, used_routes: []
13
+ def initialize app, used_actions: []
13
14
  @app = app
14
- @used_routes = used_routes
15
+ @used_actions = used_actions
15
16
  end
16
17
 
17
18
  def unused_routes
18
19
  routes.reject do |route|
19
- @used_routes.include?( newrelic_formatting(route) )
20
+ @used_actions.include?( newrelic_formatting(route) )
20
21
  end
21
22
  end
22
23
 
@@ -43,7 +44,7 @@ class RakeUnusedRoutes
43
44
  end
44
45
 
45
46
  def routes
46
- Rails.application.routes.routes.select{ |route| route.requirements[:controller] }.
47
+ @app.routes.routes.select{ |route| route.requirements[:controller] }.
47
48
  reject {|r| r.requirements[:controller].starts_with? 'rails/' }
48
49
  end
49
50
 
@@ -0,0 +1,43 @@
1
+ class RakeUnusedRoutes
2
+
3
+ class InsightsFormatter
4
+
5
+ class << self
6
+
7
+ def convert_from_insights!
8
+ FileUtils.mkdir_p tmp_dir_path
9
+ File.open("#{tmp_dir_path}/controller_summary.csv", 'w+') do |file|
10
+ file.write csv_file
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def csv_file
17
+ CSV.generate do |csv|
18
+ csv << [ 'Action' ]
19
+ actions_from_insights.each{ |action| csv << [ action ] }
20
+ end
21
+ end
22
+
23
+ def actions_from_insights
24
+ CSV.read(insights_csv_file_path).
25
+ drop(1).map(&:first).reject{|r| r=~/Middleware/ }.
26
+ map{ |r| r.sub('Controller/', '') }.
27
+ map{|r| "#{r.split('/')[0..-2].
28
+ map{|r| r.camelize}.join('::')}Controller##{r.split('/').last}" }.sort
29
+ end
30
+
31
+ def tmp_dir_path
32
+ './tmp'
33
+ end
34
+
35
+ def insights_csv_file_path
36
+ ENV.fetch('INSIGHTS_CONTROLLERS', './tmp/insights_controllers.csv')
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -1,3 +1,3 @@
1
1
  class RakeUnusedRoutes
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,7 +1,18 @@
1
+ namespace :unused_routes do
2
+
3
+ desc 'Generates unused_routes from insights'
4
+ task :from_insights do
5
+ RakeUnusedRoutes::InsightsFormatter.convert_from_insights!
6
+ Rake::Task['unused_routes'].invoke
7
+ end
8
+
9
+ end
10
+
1
11
  desc 'Prints out unused routes and unreachable action methods'
2
12
  task :unused_routes => :environment do
3
- unused_actions =
13
+ used_actions =
4
14
  RakeUnusedRoutes.actions_from_newrelic ENV.fetch('CONTROLLER_SUMMARY', './tmp/controller_summary.csv')
5
- puts RakeUnusedRoutes.new(unused_actions).formatted_unused_routes
15
+ puts RakeUnusedRoutes.new(Rails.application, used_actions: used_actions).formatted_unused_routes
6
16
  end
7
17
 
18
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rake_unused_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karl Baum
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-22 00:00:00.000000000 Z
11
+ date: 2016-12-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -99,6 +99,7 @@ files:
99
99
  - bin/console
100
100
  - bin/setup
101
101
  - lib/rake_unused_routes.rb
102
+ - lib/rake_unused_routes/insights_formatter.rb
102
103
  - lib/rake_unused_routes/version.rb
103
104
  - lib/tasks/unused_routes.rake
104
105
  - rake_unused_routes.gemspec