log-analyzer 0.4.0 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 866eefaeda010bc822f01b13b9e4d15bf2b7cfbe
4
- data.tar.gz: 5407e578aee2608481dadb66d5e9b4b351d97b4e
3
+ metadata.gz: cd574288f31cafa46f022f902045c6d82f100822
4
+ data.tar.gz: 675f2bcbbd6f6443e4ac27ccb8ed2a397c9b1cc4
5
5
  SHA512:
6
- metadata.gz: 784514f2e132c8558990f4076d197cfe76a16183e91b7f769a67a264a7746f2139a1fc262a3bc218c8325579ffa1bba6601654116a4f1a9d1160951dc3699ac9
7
- data.tar.gz: f97b7d88c249fd621ce9b50f6191c946d8afbd9138d64373fee8c280935f34dca5055b5f3c45673217a0e742dac4656f574908fa9286416f37d082cd252cad08
6
+ metadata.gz: f8981741bedf7ac47c1861b8648d2e647e09cd24219439c3d7913922f5d0bc2c0152c771e6b9e041e2573b0aebd0081a7376e7649ea2273e2c6739bdd1274e94
7
+ data.tar.gz: 28e60e495707d4f39eb33c1e726cefda2dcd0e725a0daad2c529f6251d3e6509a1b134d2a3791c1f87f45aa3cd85256e11739cf53abb504927332b8e6cffcd4b
@@ -0,0 +1,12 @@
1
+ module Log::Analyzer
2
+ class Config
3
+ attr_accessor :log_regexp, :route_regexp, :log_separator
4
+
5
+ def initialize
6
+ @route_regexp = /(GET|POST|DELETE|PATCH|PUT)\s*([^\s]*)\s*([^\s]*)\s*(\{.*\})?\s*(\[.*\])/i
7
+ #@route_regexp = /(GET|POST|DELETE|PATCH|PUT)\s+([^\s]*)\s/i
8
+ @log_regexp = /Started (?<request_method>GET|POST|DELETE|PATCH|PUT) "(?<path_info>[^"]+)".*in (?<response_time>[0-9]+)ms/im
9
+ @log_separator = "\n\n\n"
10
+ end
11
+ end
12
+ end
@@ -1,7 +1,7 @@
1
1
  module Log::Analyzer
2
2
  class Analyze
3
3
  def initialize(files, routes_text)
4
- @files =files.is_a?(Array) ? files : [files]
4
+ @files = Dir.glob(files).select { |f| File.file?(f) }
5
5
  @routes = Routes.new(routes_text)
6
6
  create_endpoints
7
7
  end
@@ -23,14 +23,12 @@ module Log::Analyzer
23
23
  @files.each do |file|
24
24
  log = Log.new(file)
25
25
  log.each_line do |req|
26
- matche = router.serve(req)
27
- next unless matche.any?
26
+ route = router.serve(req)
27
+ next if route.nil?
28
28
 
29
- req.env.delete_if { |key, val| ["REQUEST_METHOD", "PATH_INFO"].include?(key) }
30
- _, _, route = matche.first
29
+ req.env.delete_if { |key, val| ["REQUEST_METHOD", "PATH_INFO", "SCRIPT_NAME", "action_dispatch.request.path_parameters"].include?(key) }
31
30
  endpoint = Endpoint.find_by(method: route.path.request_method, uri_pattern: route.path.uri_pattern)
32
31
  endpoint.count += 1
33
-
34
32
  req.env.each do |key, val|
35
33
  endpoint[key.downcase] << val.to_i
36
34
  end
@@ -3,7 +3,7 @@ module Log::Analyzer
3
3
  attr_accessor :log_regexp, :route_regexp, :log_separator
4
4
 
5
5
  def initialize
6
- @route_regexp = /(GET|POST|DELETE|PATCH|PUT)\s+([^\s]*)\s/i
6
+ @route_regexp = /(GET|POST|DELETE|PATCH|PUT)\s*([^\s]*)\s*[^\s]*\s*(\{.*\})?\s*(\[.*\])?/i
7
7
  @log_regexp = /Started (?<request_method>GET|POST|DELETE|PATCH|PUT) "(?<path_info>[^"]+)".*in (?<response_time>[0-9]+)ms/im
8
8
  @log_separator = "\n\n\n"
9
9
  end
@@ -2,8 +2,14 @@ module Log::Analyzer
2
2
  class Pattern < ActionDispatch::Journey::Path::Pattern
3
3
  attr_accessor :request_method, :uri_pattern
4
4
 
5
- def self.from_string(request_method, uri_pattern)
6
- new(ActionDispatch::Journey::Router::Strexp.build(uri_pattern, {}, ["/.?"], true), request_method, uri_pattern)
5
+ def self.from_string(request_method, uri_pattern, requirements)
6
+ requirements = if requirements.nil? || requirements.empty?
7
+ {}
8
+ else
9
+ eval(requirements)
10
+ end
11
+ requirements ||= {}
12
+ new(ActionDispatch::Journey::Router::Strexp.build(uri_pattern, requirements, ["/.?"], true), request_method, uri_pattern)
7
13
  end
8
14
 
9
15
  def initialize(strexp, request_method, uri_pattern)
@@ -0,0 +1,64 @@
1
+ module Log::Analyzer::Rails
2
+ class RoutesInspector < ::ActionDispatch::Routing::RoutesInspector
3
+ private
4
+ def collect_routes(routes)
5
+ routes.collect do |route|
6
+ ::ActionDispatch::Routing::RouteWrapper.new(route)
7
+ end.reject do |route|
8
+ route.internal?
9
+ end.collect do |route|
10
+ collect_engine_routes(route)
11
+ constraints = collect_constraints(route)
12
+
13
+ { name: route.name,
14
+ verb: route.verb,
15
+ path: route.path,
16
+ reqs: route.reqs,
17
+ regexp: route.json_regexp,
18
+ constraints: constraints }
19
+ end
20
+ end
21
+
22
+ def collect_constraints(route)
23
+ return [] unless route.app.respond_to?(:constraints)
24
+
25
+ route.app.constraints.collect do |constraint|
26
+ case
27
+ when constraint.respond_to?(:matches?)
28
+ constraint.class
29
+ when constraint.respond_to?(:call)
30
+ constraint.source.match(/constraints: (.*)/) do |md|
31
+ md[1]
32
+ end
33
+ else
34
+ nil
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ class ConsoleFormatter < ::ActionDispatch::Routing::ConsoleFormatter
41
+ private
42
+ def draw_section(routes)
43
+ header_lengths = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action'].map(&:length)
44
+ name_width, verb_width, path_width, reqs_width = widths(routes).zip(header_lengths).map(&:max)
45
+
46
+ routes.map do |r|
47
+ "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs].ljust(reqs_width)} #{r[:constraints]}"
48
+ end
49
+ end
50
+
51
+ def draw_header(routes)
52
+ name_width, verb_width, path_width, reqs_width = widths(routes)
53
+
54
+ "#{"Prefix".rjust(name_width)} #{"Verb".ljust(verb_width)} #{"URI Pattern".ljust(path_width)} #{"Controller#Action".ljust(reqs_width)} Constraints"
55
+ end
56
+
57
+ def widths(routes)
58
+ [routes.map { |r| r[:name].length }.max || 0,
59
+ routes.map { |r| r[:verb].length }.max || 0,
60
+ routes.map { |r| r[:path].length }.max || 0,
61
+ routes.map { |r| r[:reqs].length }.max || 0]
62
+ end
63
+ end
64
+ end
@@ -1,11 +1,12 @@
1
1
  namespace :log_analyzer do
2
2
  desc 'Analyze the endpoint from the log'
3
3
  task analyze: :environment do
4
+ require 'log-analyzer/rails/inspector'
4
5
  require 'log-analyzer/cli'
5
6
 
6
7
  all_routes = Rails.application.routes.routes
7
- inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
8
- routing_text = inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER'])
8
+ inspector = Log::Analyzer::Rails::RoutesInspector.new(all_routes)
9
+ routing_text = inspector.format(Log::Analyzer::Rails::ConsoleFormatter.new, ENV['CONTROLLER'])
9
10
  analyze = Log::Analyzer::Analyze.new(Rails.root.join("log/#{Rails.env}.log"), routing_text)
10
11
  Log::Analyzer::CLI::Report.new(analyze.sort).run
11
12
  end
@@ -1,7 +1,29 @@
1
1
  module Log::Analyzer
2
2
  class Router < ActionDispatch::Journey::Router
3
3
  def serve(req)
4
- find_routes(req)
4
+ find_routes(req).each do |match, parameters, route|
5
+ set_params = req.path_parameters
6
+ path_info = req.path_info
7
+ script_name = req.script_name
8
+
9
+ unless route.path.anchored
10
+ req.script_name = (script_name.to_s + match.to_s).chomp('/')
11
+ req.path_info = match.post_match
12
+ req.path_info = "/" + req.path_info unless req.path_info.start_with? "/"
13
+ end
14
+
15
+ req.path_parameters = set_params.merge parameters
16
+ unless route.app.matches?(req)
17
+ req.script_name = script_name
18
+ req.path_info = path_info
19
+ req.path_parameters = set_params
20
+ next
21
+ end
22
+
23
+ return route
24
+ end
25
+
26
+ nil
5
27
  end
6
28
  end
7
29
  end
@@ -5,9 +5,18 @@ module Log::Analyzer
5
5
  class Routes < ActionDispatch::Journey::Routes
6
6
  def initialize(routes_text)
7
7
  super()
8
- routes_text.scan(regexp) do |request_method, request_path|
9
- path_pattern = Pattern.from_string(request_method, request_path)
10
- add_route(nil, path_pattern, {request_method: /^#{request_method}$/}, {}, {})
8
+ routes_text.scan(regexp) do |request_method, request_path, requirements, constraints|
9
+ constraints = if constraints.nil? || constraints.empty?
10
+ []
11
+ else
12
+ eval(constraints)
13
+ end.map do |constraint|
14
+ constraint.is_a?(String) ? eval(constraint) : constraint
15
+ end
16
+ app = ActionDispatch::Routing::Mapper::Constraints.new(nil, constraints, true)
17
+
18
+ path_pattern = Pattern.from_string(request_method, request_path, requirements)
19
+ add_route(app, path_pattern, {request_method: /^#{request_method}$/}, {}, {})
11
20
  end
12
21
  end
13
22
 
@@ -1,5 +1,5 @@
1
1
  module Log
2
2
  module Analyzer
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log-analyzer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shiro16
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-06 00:00:00.000000000 Z
11
+ date: 2016-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -142,6 +142,7 @@ files:
142
142
  - bin/setup
143
143
  - exe/log-analyzer
144
144
  - lib/log-analyzer.rb
145
+ - lib/log-analyzer/1
145
146
  - lib/log-analyzer/analyze.rb
146
147
  - lib/log-analyzer/cli.rb
147
148
  - lib/log-analyzer/command/log-analyzer.rb
@@ -149,6 +150,7 @@ files:
149
150
  - lib/log-analyzer/endpoint.rb
150
151
  - lib/log-analyzer/log.rb
151
152
  - lib/log-analyzer/pattern.rb
153
+ - lib/log-analyzer/rails/inspector.rb
152
154
  - lib/log-analyzer/rails/railtie.rb
153
155
  - lib/log-analyzer/rails/tasks.rake
154
156
  - lib/log-analyzer/router.rb
@@ -175,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
177
  version: '0'
176
178
  requirements: []
177
179
  rubyforge_project:
178
- rubygems_version: 2.4.5.1
180
+ rubygems_version: 2.4.5
179
181
  signing_key:
180
182
  specification_version: 4
181
183
  summary: Analyze the performance of each endpoint from the routing file