streamliner 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e80bf0779463447c18974b34cfeef24ba408fd30
4
+ data.tar.gz: 6cc8279b1b3b6cab57bd4cc92378f74b16e3ea4e
5
+ SHA512:
6
+ metadata.gz: d36887d34c83aa4f363b934ff7778aec87eadb998ba74bc0df543d85b3186c9d98b9955dfed4f80119992d3ad28b59656f2ae7899ae2fe25b37e696a88f0bae0
7
+ data.tar.gz: 26c041861d87bd35a2b88a21267e7353788cdd2c3488d23b207d97aefe5023eba7d2e08110c28dbb48c9a8ceb066f63863d215469001a374f2933ec408e9f1b4
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in streamliner.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Greg Baker
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Streamliner
2
+
3
+ Lightweight solution to measure controller usage with the aim of
4
+ identifying unused controllers.
5
+
6
+
7
+ Installation
8
+ ```
9
+ gem 'streamliner'
10
+ ```
11
+
12
+ To generate a report
13
+ ```
14
+ rake streamliner:report
15
+ ```
16
+
17
+ To reset usage statistics:
18
+ ```
19
+ rake streamliner:reset
20
+ ```
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
6
+
7
+ task :console do
8
+ exec "irb -r streamliner -I ./lib"
9
+ end
10
+
11
+
@@ -0,0 +1,11 @@
1
+ require 'streamliner/version'
2
+ require 'streamliner/counter'
3
+ require 'streamliner/report'
4
+
5
+ if defined?(Rails)
6
+ require 'streamliner/railtie'
7
+ require 'streamliner/controller_interceptor'
8
+ end
9
+
10
+ module Streamliner
11
+ end
@@ -0,0 +1,17 @@
1
+ module Streamliner
2
+ module ControllerInterceptor
3
+
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ append_before_action :count_controller
8
+ end
9
+
10
+ def count_controller
11
+ controller = params[:controller]
12
+ cached_count = Rails.cache.fetch(:streamliner_counter)
13
+ updated_count = Streamliner::Counter.new(controller, cached_count).add
14
+ Rails.cache.write(:streamliner_counter, updated_count)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ require 'date'
2
+
3
+ module Streamliner
4
+ class Counter
5
+
6
+ attr_reader :controller, :counter
7
+
8
+ def initialize(controller, counter)
9
+ @controller = controller
10
+ @counter = counter || Hash.new(0)
11
+ ensure_start_date
12
+ end
13
+
14
+ def add
15
+ counter[controller] += 1
16
+ counter
17
+ end
18
+
19
+ private
20
+
21
+ def ensure_start_date
22
+ if counter[:start] == 0
23
+ counter[:start] = DateTime.now
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ require 'rails'
2
+ require 'streamliner/counter'
3
+
4
+ module Streamliner
5
+ class Railtie < Rails::Railtie
6
+ initializer 'streamliner.action_controller' do
7
+ ActiveSupport.on_load(:action_controller) do
8
+ include Streamliner::ControllerInterceptor
9
+ end
10
+ end
11
+
12
+ rake_tasks do
13
+ load 'streamliner/tasks/report.rake'
14
+ load 'streamliner/tasks/reset.rake'
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,80 @@
1
+ module Streamliner
2
+ class Report
3
+ attr_reader :controller_count, :controller_list, :controllers_by_use
4
+ attr_accessor :report
5
+
6
+ def initialize(controller_count, controller_list)
7
+ @controller_count = controller_count
8
+ @controller_list = controller_list
9
+ @report = String.new
10
+ end
11
+
12
+ def generate
13
+ if data_recorded?
14
+ add_start_date
15
+ add_header
16
+ add_usage
17
+ add_unused_code
18
+ else
19
+ no_data_message
20
+ end
21
+ report
22
+ end
23
+
24
+ private
25
+
26
+ def add_start_date
27
+ start = controller_count.delete(:start)
28
+ report << "\nSince #{start.strftime('%e %b %Y %H:%M:%S%p')} the following" +
29
+ " controller usage has occurred:\n\n"
30
+ end
31
+
32
+ def data_recorded?
33
+ !controller_count.nil?
34
+ end
35
+
36
+ def no_data_message
37
+ report << "No controller usage recorded\n"
38
+ end
39
+
40
+ def add_header
41
+ headers = ""
42
+ headers << "Controller".ljust(30)
43
+ headers << "Hits".ljust(10)
44
+ headers << "Percentage use\n"
45
+ report << headers
46
+ report << "-" * headers.chomp.length + "\n\n"
47
+ end
48
+
49
+ def sorted_controllers
50
+ controller_by_use ||= controller_count.sort_by{|k,v| v}.reverse.to_h
51
+ end
52
+
53
+ def add_usage
54
+ sorted_controllers.each do |controller, count|
55
+ report << "#{controller.capitalize}".ljust(30) +
56
+ "#{count}".ljust(10) + "#{usage_percentage(count)}%\n"
57
+ end
58
+ end
59
+
60
+ def usage_percentage count
61
+ sum = controller_count.values.reduce(:+)
62
+ ((count.to_f / sum) * 100).round(2)
63
+ end
64
+
65
+ def add_unused_code
66
+ if unused_controllers.any?
67
+ report << "\nThe following controllers were not used and should be considered for removal:\n\n"
68
+ no_usage = unused_controllers.map {|e| e.capitalize }
69
+ report << (Array.method_defined?(:to_sentence) ? no_usage.to_sentence : no_usage.join(', '))
70
+ else
71
+ report << "\nAll controllers were used and none are suggested for removal."
72
+ end
73
+ end
74
+
75
+ def unused_controllers
76
+ controller_list.map { |e|
77
+ e.to_s.downcase.split('controller')[0] } - sorted_controllers.keys
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,10 @@
1
+ namespace :streamliner do
2
+ desc 'Display count of controller hits'
3
+ task :report => :environment do
4
+ Rails.application.eager_load!
5
+ controller_list = ApplicationController.subclasses
6
+ controller_counter = Rails.cache.fetch(:streamliner_counter)
7
+ report = Streamliner::Report.new(controller_counter, controller_list).generate
8
+ puts report
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ namespace :streamliner do
2
+ desc 'Reset controller usage count'
3
+ task :reset => :environment do
4
+ Rails.cache.delete(:streamliner_counter)
5
+ puts "Controller usage count reset.\n"
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Streamliner
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,93 @@
1
+ require 'streamliner'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
6
+ # this file to always be loaded, without a need to explicitly require it in any
7
+ # files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need
15
+ # it.
16
+ #
17
+ # The `.rspec` file also contains a few flags that are not defaults but that
18
+ # users commonly want.
19
+ #
20
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
21
+ RSpec.configure do |config|
22
+ # rspec-expectations config goes here. You can use an alternate
23
+ # assertion/expectation library such as wrong or the stdlib/minitest
24
+ # assertions if you prefer.
25
+ config.expect_with :rspec do |expectations|
26
+ # This option will default to `true` in RSpec 4. It makes the `description`
27
+ # and `failure_message` of custom matchers include text for helper methods
28
+ # defined using `chain`, e.g.:
29
+ # be_bigger_than(2).and_smaller_than(4).description
30
+ # # => "be bigger than 2 and smaller than 4"
31
+ # ...rather than:
32
+ # # => "be bigger than 2"
33
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
34
+ end
35
+
36
+ # rspec-mocks config goes here. You can use an alternate test double
37
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
38
+ config.mock_with :rspec do |mocks|
39
+ # Prevents you from mocking or stubbing a method that does not exist on
40
+ # a real object. This is generally recommended, and will default to
41
+ # `true` in RSpec 4.
42
+ mocks.verify_partial_doubles = true
43
+ end
44
+
45
+ # The settings below are suggested to provide a good initial experience
46
+ # with RSpec, but feel free to customize to your heart's content.
47
+ =begin
48
+ # These two settings work together to allow you to limit a spec run
49
+ # to individual examples or groups you care about by tagging them with
50
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
51
+ # get run.
52
+ config.filter_run :focus
53
+ config.run_all_when_everything_filtered = true
54
+
55
+ # Limits the available syntax to the non-monkey patched syntax that is
56
+ # recommended. For more details, see:
57
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
58
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
59
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
60
+ config.disable_monkey_patching!
61
+
62
+ # This setting enables warnings. It's recommended, but in some cases may
63
+ # be too noisy due to issues in dependencies.
64
+ config.warnings = true
65
+
66
+ # Many RSpec users commonly either run the entire suite or an individual
67
+ # file, and it's useful to allow more verbose output when running an
68
+ # individual spec file.
69
+ if config.files_to_run.one?
70
+ # Use the documentation formatter for detailed output,
71
+ # unless a formatter has already been configured
72
+ # (e.g. via a command-line flag).
73
+ config.default_formatter = 'doc'
74
+ end
75
+
76
+ # Print the 10 slowest examples and example groups at the
77
+ # end of the spec run, to help surface which specs are running
78
+ # particularly slow.
79
+ config.profile_examples = 10
80
+
81
+ # Run specs in random order to surface order dependencies. If you find an
82
+ # order dependency and want to debug it, you can fix the order by providing
83
+ # the seed, which is printed after each run.
84
+ # --seed 1234
85
+ config.order = :random
86
+
87
+ # Seed global randomization in this process using the `--seed` CLI option.
88
+ # Setting this allows you to use `--seed` to deterministically reproduce
89
+ # test failures related to randomization by passing the same `--seed` value
90
+ # as the one that triggered the failure.
91
+ Kernel.srand config.seed
92
+ =end
93
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ module Streamliner
4
+ describe Counter do
5
+ let(:counter) { Streamliner::Counter }
6
+ let(:params_hash_1) { "home" }
7
+ let(:params_hash_2) { "session" }
8
+
9
+ it 'returns a hash' do
10
+ result = counter.new(params_hash_1, nil).add
11
+ expect(result).to be_a(Hash)
12
+ end
13
+
14
+ it 'counts controllers passed in' do
15
+ result = counter.new(params_hash_1, nil).add
16
+ result = counter.new(params_hash_1, result).add
17
+ result = counter.new(params_hash_2, result).add
18
+ result.delete(:start)
19
+ expect(result).to eq({"home" => 2, "session" => 1})
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ module Streamliner
4
+ describe Report do
5
+ let(:controllers) {
6
+ %w[FirstController SecondController ThirdController, ForthController]
7
+ }
8
+ let(:count) {
9
+ {
10
+ 'first' => 5,
11
+ 'second' => 3,
12
+ 'third' => 1
13
+ }.merge({start: DateTime.parse('2040/1/1')})
14
+ }
15
+
16
+ it 'alerts the user when no usage data has been recorded' do
17
+ report = Report.new(nil, controllers).generate
18
+ expect(report).to eq("No controller usage recorded\n")
19
+ end
20
+
21
+ it 'generates a valid report when there is usage data' do
22
+ report = Report.new(count, controllers).generate
23
+ expect(report).to eq("\nSince 1 Jan 2040 00:00:00AM the following controller usage has occurred:\n\nController Hits Percentage use\n------------------------------------------------------\n\nFirst 5 55.56%\nSecond 3 33.33%\nThird 1 11.11%\n\nThe following controllers were not used and should be considered for removal:\n\nForth")
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'streamliner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "streamliner"
8
+ spec.version = Streamliner::VERSION
9
+ spec.authors = ["Greg Baker"]
10
+ spec.email = ["gba311@gmail.com"]
11
+ spec.summary = %q{Identifies unused controllers in Rails applications.}
12
+ spec.description = %q{Lightweight solution to measure controller usage in Rails applications with the aim of identifying unused controllers.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: streamliner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Greg Baker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Lightweight solution to measure controller usage in Rails applications
56
+ with the aim of identifying unused controllers.
57
+ email:
58
+ - gba311@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/streamliner.rb
70
+ - lib/streamliner/controller_interceptor.rb
71
+ - lib/streamliner/counter.rb
72
+ - lib/streamliner/railtie.rb
73
+ - lib/streamliner/report.rb
74
+ - lib/streamliner/tasks/report.rake
75
+ - lib/streamliner/tasks/reset.rake
76
+ - lib/streamliner/version.rb
77
+ - spec/spec_helper.rb
78
+ - spec/streamliner/counter_spec.rb
79
+ - spec/streamliner/report_spec.rb
80
+ - streamliner.gemspec
81
+ homepage: ''
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.2.2
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Identifies unused controllers in Rails applications.
105
+ test_files:
106
+ - spec/spec_helper.rb
107
+ - spec/streamliner/counter_spec.rb
108
+ - spec/streamliner/report_spec.rb