log-analyzer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eccd209a81035144e2f86390c024ab6cea0f64fc
4
+ data.tar.gz: d0614f2ae83b0bf049da0ac37e5a99dd9c2ff4e2
5
+ SHA512:
6
+ metadata.gz: 3a70d4208ae639505149d6c9712ffcb7124d86259a9e406f46cdb8857aa2a65e122b9593abf4f6189622fc573dc3a0a577b09cce18cf6a0d34edaeafef6dfaec
7
+ data.tar.gz: c38689ba60da004b6f63210f18b8bfa8b6bbef8035b5d5fbbe589a34d97d70920ffac30105352cc6b476c146019b17bc1a8de3f3129f0ac362b564fdccb27ac2
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1
5
+ - 2.2
6
+ - 2.3.0
7
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in log-analyzer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 shiro16
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,47 @@
1
+ # Log::Analyzer
2
+
3
+ [![Build Status](https://travis-ci.org/shiro16/log-analyzer.svg)](https://travis-ci.org/shiro16/log-analyzer)
4
+
5
+ Analyze the performance of each endpoint from the routing file
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'log-analyzer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install log-analyzer
22
+
23
+ ## Usage
24
+
25
+ Rails Application:
26
+
27
+ $ bundle exec rake log_analyzer:analyze
28
+
29
+ Other Framework Application:
30
+
31
+ $ log-analyzer -r routing_file <file ...>
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/log-analyzer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
42
+
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
47
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "log-analyzer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require "pry"
10
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/exe/log-analyzer ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems' unless defined?(gem)
3
+ here = File.dirname(__FILE__)
4
+ $LOAD_PATH << File.expand_path(File.join(here, '..', 'lib'))
5
+ require 'log-analyzer/command/log-analyzer'
@@ -0,0 +1,41 @@
1
+ module Log::Analyzer
2
+ class Analyze
3
+ def initialize(files, routes_text)
4
+ @files =files.is_a?(Array) ? files : [files]
5
+ @routes = Routes.new(routes_text)
6
+ create_endpoints
7
+ end
8
+
9
+ def result
10
+ @result ||= matche
11
+ end
12
+
13
+ def sort
14
+ result.sort{ |a, b| b.count <=> a.count }
15
+ end
16
+
17
+ private
18
+ def router
19
+ @router ||= Router.new(@routes)
20
+ end
21
+
22
+ def matche
23
+ @files.each do |file|
24
+ log = Log.new(file)
25
+ log.each_line do |req|
26
+ matche = router.serve(req)
27
+ next unless matche.any?
28
+
29
+ _, _, route = matche.first
30
+ endpoint = Endpoint.find_by(method: route.path.request_method, uri_pattern: route.path.uri_pattern)
31
+ endpoint.count += 1
32
+ end
33
+ end
34
+ Endpoint.all
35
+ end
36
+
37
+ def create_endpoints
38
+ @routes.each { |route| Endpoint.create(method: route.path.request_method, uri_pattern: route.path.uri_pattern) }
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,36 @@
1
+ require 'command_line_reporter'
2
+
3
+ module Log::Analyzer
4
+ class CLI
5
+ def self.run(files, options)
6
+ routing_text = File.read(options[:r])
7
+ analyze = Analyze.new(files, routing_text)
8
+ Report.new(analyze.sort).run
9
+ end
10
+
11
+ class Report
12
+ include CommandLineReporter
13
+
14
+ def initialize(endpoints)
15
+ @endpoints = endpoints
16
+ end
17
+
18
+ def run
19
+ table(width: :auto, border: true) do
20
+ row header: true, color: 'red' do
21
+ column('method')
22
+ column('endpoint')
23
+ column('count')
24
+ end
25
+ @endpoints.each do |endpoint|
26
+ row color: 'red' do
27
+ column(endpoint.method)
28
+ column(endpoint.uri_pattern)
29
+ column(endpoint.count)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,29 @@
1
+ require 'optparse'
2
+ require 'log-analyzer'
3
+ require 'log-analyzer/cli'
4
+
5
+ op = OptionParser.new
6
+
7
+ opts = {}
8
+
9
+ op.banner = 'Usage: log-analyzer [options] <file ...>'
10
+
11
+ op.on('-h', '--help', 'output usage information') do
12
+ puts op
13
+ exit
14
+ end
15
+
16
+ op.on('-V', '--version', 'output the version number') do |v|
17
+ puts Log::Analyzer::VERSION
18
+ exit
19
+ end
20
+
21
+ op.on('-r VALUE', 'routing format file') { |v| opts[:r] = v }
22
+
23
+ op.parse!(ARGV)
24
+
25
+ if ARGV.empty?
26
+ puts op
27
+ else
28
+ Log::Analyzer::CLI.run(ARGV, opts)
29
+ end
@@ -0,0 +1,28 @@
1
+ module Log::Analyzer
2
+ class Endpoint
3
+ attr_accessor :method, :uri_pattern, :count, :values
4
+
5
+ def initialize(method: "", uri_pattern: "", values: Hash.new(0))
6
+ @method = method
7
+ @uri_pattern = uri_pattern
8
+ @count = 0
9
+ @values = values
10
+ end
11
+
12
+ def self.create(method: "", uri_pattern: "")
13
+ endpoints["#{method}:#{uri_pattern}"] = self.new(method: method, uri_pattern: uri_pattern)
14
+ end
15
+
16
+ def self.find_by(method: "", uri_pattern: "")
17
+ endpoints["#{method}:#{uri_pattern}"]
18
+ end
19
+
20
+ def self.all
21
+ @endpoints.values
22
+ end
23
+
24
+ def self.endpoints
25
+ @endpoints ||= {}
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ module Log::Analyzer
2
+ class Log
3
+ LOG_REGEXP = /Started (?<request_method>GET|POST|DELETE|PATCH|PUT) "(?<path_info>[^"]+)"/i
4
+
5
+ def initialize(path)
6
+ @file = File.open(path)
7
+ end
8
+
9
+ def each_line(&block)
10
+ @file.each_line do |line|
11
+ next unless md = line.match(LOG_REGEXP)
12
+ env = md.names.inject({}) do |hash, name|
13
+ hash[name.upcase] = md[name]
14
+ hash
15
+ end
16
+ block.call(ActionDispatch::Request.new(env))
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,15 @@
1
+ module Log::Analyzer
2
+ class Pattern < ActionDispatch::Journey::Path::Pattern
3
+ attr_accessor :request_method, :uri_pattern
4
+
5
+ def self.from_string(request_method, uri_pattern)
6
+ new(ActionDispatch::Journey::Router::Strexp.build(uri_pattern, {}, ["/.?"], true), request_method, uri_pattern)
7
+ end
8
+
9
+ def initialize(strexp, request_method, uri_pattern)
10
+ @request_method = request_method
11
+ @uri_pattern = uri_pattern
12
+ super(strexp)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module Log::Analyzer
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ rake_tasks do
5
+ load 'log-analyzer/rails/tasks.rake'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ namespace :log_analyzer do
2
+ desc 'Analyze the endpoint from the log'
3
+ task analyze: :environment do
4
+ require 'log-analyzer/cli'
5
+
6
+ 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'])
9
+ analyze = Log::Analyzer::Analyze.new(Rails.root.join("log/#{Rails.env}.log"), routing_text)
10
+ Log::Analyzer::CLI::Report.new(analyze.sort).run
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ module Log::Analyzer
2
+ class Router < ActionDispatch::Journey::Router
3
+ def serve(req)
4
+ find_routes(req)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,16 @@
1
+ require 'action_dispatch/journey'
2
+ require 'action_dispatch'
3
+
4
+ module Log::Analyzer
5
+ class Routes < ActionDispatch::Journey::Routes
6
+ ROUTE_REGEXP = /(GET|POST|DELETE|PATCH|PUT)\s+([^\s]*)\s/i
7
+
8
+ def initialize(routes_text)
9
+ super()
10
+ routes_text.scan(ROUTE_REGEXP) do |request_method, request_path|
11
+ path_pattern = Pattern.from_string(request_method, request_path)
12
+ add_route(nil, path_pattern, {request_method: /^#{request_method}$/}, {}, {})
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Log
2
+ module Analyzer
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ require 'log-analyzer/version'
2
+ require 'log-analyzer/routes'
3
+ require 'log-analyzer/analyze'
4
+ require 'log-analyzer/log'
5
+ require 'log-analyzer/router'
6
+ require 'log-analyzer/pattern'
7
+ require 'log-analyzer/endpoint'
8
+ require 'log-analyzer/rails/railtie.rb' if defined?(Rails)
9
+
10
+ module Log
11
+ module Analyzer
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'log-analyzer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "log-analyzer"
8
+ spec.version = Log::Analyzer::VERSION
9
+ spec.authors = ["shiro16"]
10
+ spec.email = ["nyanyanyawan24@gmail.com"]
11
+
12
+ spec.summary = %q{Analyze the performance of each endpoint from the routing file}
13
+ spec.description = %q{Analyze the performance of each endpoint from the routing file}
14
+ spec.homepage = "https://github.com/shiro16/log-analyzer"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "actionpack", "~> 4.2.4"
23
+ spec.add_dependency "command_line_reporter"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "rails"
30
+ spec.add_development_dependency "sqlite3"
31
+ end
metadata ADDED
@@ -0,0 +1,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: log-analyzer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - shiro16
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: command_line_reporter
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Analyze the performance of each endpoint from the routing file
126
+ email:
127
+ - nyanyanyawan24@gmail.com
128
+ executables:
129
+ - log-analyzer
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - exe/log-analyzer
144
+ - lib/log-analyzer.rb
145
+ - lib/log-analyzer/analyze.rb
146
+ - lib/log-analyzer/cli.rb
147
+ - lib/log-analyzer/command/log-analyzer.rb
148
+ - lib/log-analyzer/endpoint.rb
149
+ - lib/log-analyzer/log.rb
150
+ - lib/log-analyzer/pattern.rb
151
+ - lib/log-analyzer/rails/railtie.rb
152
+ - lib/log-analyzer/rails/tasks.rake
153
+ - lib/log-analyzer/router.rb
154
+ - lib/log-analyzer/routes.rb
155
+ - lib/log-analyzer/version.rb
156
+ - log-analyzer.gemspec
157
+ homepage: https://github.com/shiro16/log-analyzer
158
+ licenses:
159
+ - MIT
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubyforge_project:
177
+ rubygems_version: 2.4.5
178
+ signing_key:
179
+ specification_version: 4
180
+ summary: Analyze the performance of each endpoint from the routing file
181
+ test_files: []