route_tracker_arsh 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ae7c11b9eccf7b7252838339a71dc568ab56066b46644c0d07a08304d04ff94f
4
+ data.tar.gz: '059f525633c107fb4b9c0306ca762ab6eff286e41344c5809b50100d20f3e491'
5
+ SHA512:
6
+ metadata.gz: 94547dd940dbff733918178248110addd57b1593cd5a8a8dd08629eb09ca8fb2388e24709809f6f48cc80d9e3b501c7f6c57cb79977630485266b346b4973b66
7
+ data.tar.gz: 1038606f9169d765b916659eb2ceed47ab130c4558062fa5e604b54a1cffaf7c48394692841659c7473fb2ebdb71424e810263a1f02652dba517415e9af57ab1
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright TODO: Write your name
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Routetracker
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "routetracker"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install routetracker
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module Routetracker
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,45 @@
1
+ module Routetracker
2
+ class DashboardController < ApplicationController
3
+ def index
4
+ @routes = []
5
+
6
+ # Pre-fetch all hits to minimize queries
7
+ # Grouping by controller#action allows us to aggregate multiple hits (e.g. different verbs for same action if applicable)
8
+ all_hits = Routetracker::RouteHit.all
9
+ hits_map = all_hits.group_by { |h| "#{h.controller}##{h.action}" }
10
+
11
+ Rails.application.routes.routes.each do |route|
12
+ # wrapper = ActionDispatch::Routing::RouteWrapper.new(route)
13
+ # next if wrapper.internal? # internal? might not exist on raw route
14
+
15
+ # requirements gives us the controller/action
16
+ reqs = route.requirements
17
+ controller = reqs[:controller]
18
+ action = reqs[:action]
19
+
20
+ next unless controller && action
21
+
22
+ # Normalize data
23
+ verb = route.verb.to_s
24
+ path = route.path.spec.to_s
25
+ # Remove (.:format) for cleaner display
26
+ path = path.sub('(.:format)', '')
27
+
28
+ key = "#{controller}##{action}"
29
+ related_hits = hits_map[key] || []
30
+
31
+ @routes << {
32
+ verb: verb,
33
+ path: path,
34
+ controller: controller,
35
+ action: action,
36
+ hit_count: related_hits.sum(&:hit_count),
37
+ last_hit_at: related_hits.map(&:last_hit_at).compact.max
38
+ }
39
+ end
40
+
41
+ # Sort: Never hit first, then alphabetical
42
+ @routes.sort_by! { |r| [r[:hit_count] == 0 ? 0 : 1, r[:controller], r[:action]] }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,4 @@
1
+ module Routetracker
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Routetracker
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module Routetracker
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Routetracker
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ module Routetracker
2
+ class RouteHit < ApplicationRecord
3
+ end
4
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Routetracker</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= yield :head %>
9
+
10
+ <%= stylesheet_link_tag "routetracker/application", media: "all" %>
11
+ </head>
12
+ <body>
13
+
14
+ <%= yield %>
15
+
16
+ </body>
17
+ </html>
@@ -0,0 +1,44 @@
1
+ <h1>Route Usage Tracker</h1>
2
+
3
+ <div style="margin-bottom: 20px;">
4
+ <strong>Stats:</strong> <%= @routes.count %> Total Routes |
5
+ <%= @routes.count { |r| r[:hit_count] == 0 } %> potentially unused.
6
+ </div>
7
+
8
+ <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse; width: 100%; font-family: monospace;">
9
+ <thead>
10
+ <tr style="background-color: #f0f0f0; text-align: left;">
11
+ <th>Verb</th>
12
+ <th>Path</th>
13
+ <th>Controller#Action</th>
14
+ <th>Hits</th>
15
+ <th>Last Accessed</th>
16
+ <th>Status</th>
17
+ </tr>
18
+ </thead>
19
+ <tbody>
20
+ <% @routes.each do |route| %>
21
+ <% is_unused = route[:hit_count] == 0 %>
22
+ <tr style="<%= is_unused ? 'background-color: #fff0f0; color: #d00;' : '' %>">
23
+ <td><%= route[:verb].presence || 'ALL' %></td>
24
+ <td><%= route[:path] %></td>
25
+ <td><%= route[:controller] %>#<%= route[:action] %></td>
26
+ <td><%= route[:hit_count] %></td>
27
+ <td>
28
+ <% if route[:last_hit_at] %>
29
+ <span title="<%= route[:last_hit_at] %>"><%= time_ago_in_words(route[:last_hit_at]) %> ago</span>
30
+ <% else %>
31
+ <span style="color: #999;">Never</span>
32
+ <% end %>
33
+ </td>
34
+ <td>
35
+ <% if is_unused %>
36
+ <strong>UNUSED</strong>
37
+ <% else %>
38
+ <span style="color: green;">Active</span>
39
+ <% end %>
40
+ </td>
41
+ </tr>
42
+ <% end %>
43
+ </tbody>
44
+ </table>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Routetracker::Engine.routes.draw do
2
+ root to: "dashboard#index"
3
+ end
@@ -0,0 +1,15 @@
1
+ class CreateRoutetrackerRouteHits < ActiveRecord::Migration[8.1]
2
+ def change
3
+ create_table :routetracker_route_hits do |t|
4
+ t.string :verb
5
+ t.string :path
6
+ t.string :controller
7
+ t.string :action
8
+ t.datetime :last_hit_at
9
+ t.integer :hit_count, default: 0
10
+
11
+ t.timestamps
12
+ end
13
+ add_index :routetracker_route_hits, [:controller, :action, :verb], name: 'index_routetracker_hits_on_ids', unique: true
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require_relative "middleware"
2
+
3
+ module Routetracker
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace Routetracker
6
+
7
+ initializer "routetracker.add_middleware" do |app|
8
+ app.middleware.use Routetracker::Middleware
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,61 @@
1
+ module Routetracker
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ status, headers, body = @app.call(env)
9
+
10
+ begin
11
+ record_hit(env)
12
+ rescue => e
13
+ # Failsafe: Don't break the app if tracking fails
14
+ # Using a raw logger if Rails logger isn't available, but it should be.
15
+ (Rails.logger || Logger.new($stdout)).error "[Routetracker] Error: #{e.message}"
16
+ end
17
+
18
+ [status, headers, body]
19
+ end
20
+
21
+ private
22
+
23
+ def record_hit(env)
24
+ # We rely on path_parameters which are set by the router
25
+ params = env['action_dispatch.request.path_parameters']
26
+ return unless params
27
+
28
+ controller = params[:controller]
29
+ action = params[:action]
30
+ return unless controller && action
31
+
32
+ request = ActionDispatch::Request.new(env)
33
+ verb = request.request_method
34
+ path = request.path
35
+
36
+ # Basic retry logic for race conditions on creation
37
+ retries = 0
38
+ begin
39
+ hit = Routetracker::RouteHit.find_or_create_by(
40
+ controller: controller,
41
+ action: action,
42
+ verb: verb
43
+ )
44
+
45
+ # Atomically increment and update timestamp/path
46
+ # We update 'path' to the latest one seen (sample)
47
+ hit.with_lock do
48
+ hit.hit_count += 1
49
+ hit.last_hit_at = Time.current
50
+ hit.path = path # Update to latest sample path
51
+ hit.save
52
+ end
53
+
54
+ rescue ActiveRecord::RecordNotUnique
55
+ if (retries += 1) < 3
56
+ retry
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module Routetracker
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "routetracker/version"
2
+ require "routetracker/engine"
3
+
4
+ module Routetracker
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :routetracker do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: route_tracker_arsh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - RouteTracker Contributor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-12-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 8.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 8.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: RouteTracker provides middleware to log when routes are accessed and
42
+ a dashboard to visualize usage stats, helping you safely delete dead code.
43
+ email:
44
+ - contributor@example.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - app/assets/stylesheets/routetracker/application.css
53
+ - app/controllers/routetracker/application_controller.rb
54
+ - app/controllers/routetracker/dashboard_controller.rb
55
+ - app/helpers/routetracker/application_helper.rb
56
+ - app/jobs/routetracker/application_job.rb
57
+ - app/mailers/routetracker/application_mailer.rb
58
+ - app/models/routetracker/application_record.rb
59
+ - app/models/routetracker/route_hit.rb
60
+ - app/views/layouts/routetracker/application.html.erb
61
+ - app/views/routetracker/dashboard/index.html.erb
62
+ - config/routes.rb
63
+ - db/migrate/20251231173342_create_routetracker_route_hits.rb
64
+ - lib/routetracker.rb
65
+ - lib/routetracker/engine.rb
66
+ - lib/routetracker/middleware.rb
67
+ - lib/routetracker/version.rb
68
+ - lib/tasks/routetracker_tasks.rake
69
+ homepage: https://github.com/example/routetracker
70
+ licenses:
71
+ - MIT
72
+ metadata:
73
+ homepage_uri: https://github.com/example/routetracker
74
+ source_code_uri: https://github.com/example/routetracker
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.5.16
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Track unused routes in your Rails application.
94
+ test_files: []