log_analyzer 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +11 -1
- data/bin/log_analyzer +15 -5
- data/lib/log_analyzer/analyzer.rb +37 -47
- data/lib/log_analyzer/configuration.rb +0 -1
- data/lib/log_analyzer/path_shortener.rb +34 -0
- data/lib/log_analyzer/utils.rb +48 -0
- data/lib/log_analyzer/version.rb +1 -1
- data/lib/log_analyzer.rb +7 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae28afd0313658e95cf16c50b1f9dc3a5ef5fe31
|
4
|
+
data.tar.gz: 72e12660b3efc9ed590784336db4cf71ebbeebd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ff772b58e8164658bdc961b5eece7fa8abd5db65206182b2cef96f1eb97763046e0484cfda2e71eeb5ff0cc2dbd181d1de674f03a159e625db8bca9b07cc795
|
7
|
+
data.tar.gz: c908a2abc1ca1084ba8a15ad570c7bd348768976ecc5b0983f31aa04a3aab8ac44993814a1f9f8d0e64f7ca14d06e9a53040ea337f7d5a6693b0b657e646376c
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -48,6 +48,9 @@ Samples:
|
|
48
48
|
* `log_analyzer -file log/production.log -sort count -filter view`
|
49
49
|
* `log_analyzer -file log/production.log -sort count -filter partial`
|
50
50
|
* `log_analyzer -file log/production.log -sort time -filter P`
|
51
|
+
* `log_analyzer log/production.log --short`
|
52
|
+
* `log_analyzer log/production.log -sp`
|
53
|
+
* `log_analyzer -file log/production.log --short`
|
51
54
|
* `log_analyzer --help`
|
52
55
|
|
53
56
|
**Based on results you can get an idea what to optimize. For example optimizing most often rendering view could give huge benefit. Now with this tool you can find out what are the numbers.**
|
@@ -62,7 +65,14 @@ To install this gem on your local machine, run `bundle exec rake install`. To re
|
|
62
65
|
|
63
66
|
## Contributing
|
64
67
|
|
65
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/igorkasyanchuk/log_analyzer. 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.
|
69
|
+
|
70
|
+
## Contributors
|
71
|
+
|
72
|
+
Big thank you to all contributors:
|
73
|
+
|
74
|
+
* @ck3g
|
75
|
+
* @ritaritual
|
66
76
|
|
67
77
|
## TODO
|
68
78
|
|
data/bin/log_analyzer
CHANGED
@@ -23,17 +23,27 @@ parser = OptionParser.new do |opts|
|
|
23
23
|
options[:filter] = v
|
24
24
|
end
|
25
25
|
|
26
|
+
opts.on("-sp", "--short", "Shrink long paths") do |v|
|
27
|
+
options[:short] = v
|
28
|
+
end
|
29
|
+
|
26
30
|
opts.on('-h', '--help', 'Displays Help') do
|
27
31
|
puts opts
|
28
32
|
exit
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
36
|
+
# hack, because "log_analyzer production.log --short" not works but "-sp" works
|
37
|
+
if i = ARGV.index("--short")
|
38
|
+
ARGV[i] = "-sp"
|
39
|
+
end
|
40
|
+
|
32
41
|
parser.parse!
|
33
42
|
|
34
|
-
options[:file]
|
35
|
-
options[:sort]
|
36
|
-
options[:filter]
|
43
|
+
options[:file] = ARGV[0] if ARGV.size == 1 # user first argument as filename
|
44
|
+
options[:sort] ||= :time # default order
|
45
|
+
options[:filter] ||= :all # default order
|
46
|
+
options[:short] ||= false
|
37
47
|
|
38
48
|
LogAnalyzer::Configuration.configure do |config|
|
39
49
|
config.filter = options[:filter]
|
@@ -45,7 +55,7 @@ if options[:file] && options[:sort]
|
|
45
55
|
analyzer = LogAnalyzer.analyze(filename: options[:file])
|
46
56
|
analyzer.run
|
47
57
|
analyzer.order(by: options[:sort])
|
48
|
-
analyzer.visualize
|
58
|
+
analyzer.visualize(short: options[:short])
|
49
59
|
else
|
50
60
|
parser.parse! %w[--help]
|
51
|
-
end
|
61
|
+
end
|
@@ -2,12 +2,9 @@ module LogAnalyzer
|
|
2
2
|
|
3
3
|
class Analyzer
|
4
4
|
DEFAULT_TABLE_WIDTH = 120 # width
|
5
|
-
|
6
|
-
|
7
|
-
INFO_DEFAULT = 100 # ms
|
5
|
+
CONTENT_LENGTH = (0..DEFAULT_TABLE_WIDTH - 20).freeze
|
6
|
+
ROWS_FOR_FOOTER = 10
|
8
7
|
HEADER = ['Type', 'View', 'Count', 'AVG (ms)', 'Max', 'Min'].freeze
|
9
|
-
PARTIAL_LABEL = " P ".on_green.black.freeze
|
10
|
-
VIEW_LABEL = " V ".on_yellow.black.freeze
|
11
8
|
MATCHER = /Rendered (.*\/.*) \((.*)ms\)/.freeze
|
12
9
|
|
13
10
|
attr_reader :filename
|
@@ -23,13 +20,13 @@ module LogAnalyzer
|
|
23
20
|
if line =~ MATCHER
|
24
21
|
if $1 && $2
|
25
22
|
view = $1
|
26
|
-
@stats[view] ||= Stat.new(type: find_type(view))
|
23
|
+
@stats[view] ||= Stat.new(type: Utils.find_type(view))
|
27
24
|
@stats[view].push($2)
|
28
25
|
end
|
29
26
|
end
|
30
27
|
end
|
31
28
|
rescue Errno::ENOENT
|
32
|
-
puts "File <#{filename}> not found or
|
29
|
+
puts "File <#{filename}> is not found or inaccessible.".red
|
33
30
|
exit
|
34
31
|
end
|
35
32
|
|
@@ -44,51 +41,44 @@ module LogAnalyzer
|
|
44
41
|
end
|
45
42
|
end
|
46
43
|
|
47
|
-
def visualize(limit: 100)
|
48
|
-
|
49
|
-
|
50
|
-
table = Terminal::Table.new \
|
51
|
-
headings: HEADER,
|
52
|
-
width: DEFAULT_TABLE_WIDTH do |t|
|
53
|
-
stats.each do |path, stat|
|
54
|
-
next unless filters.include?(stat.type)
|
55
|
-
t.add_row [
|
56
|
-
type_label(stat.type),
|
57
|
-
path[length],
|
58
|
-
stat.count,
|
59
|
-
avg_label(stat.avg),
|
60
|
-
stat.max,
|
61
|
-
stat.min,
|
62
|
-
]
|
63
|
-
end
|
64
|
-
t.add_separator
|
65
|
-
t.add_row(HEADER)
|
66
|
-
end
|
67
|
-
puts table
|
68
|
-
end
|
44
|
+
def visualize(limit: 100, short: false)
|
45
|
+
table = new_table
|
46
|
+
rows_count = 0
|
69
47
|
|
70
|
-
|
48
|
+
# preparing report
|
49
|
+
stats.each do |path, stat|
|
50
|
+
next unless LogAnalyzer.configuration.filters.include?(stat.type)
|
71
51
|
|
72
|
-
|
73
|
-
view.split('/'.freeze).last[0] == '_'.freeze ? 'P'.freeze : 'V'.freeze
|
74
|
-
end
|
52
|
+
table.add_row format_row(path, stat, short)
|
75
53
|
|
76
|
-
|
77
|
-
type == LogAnalyzer::Configuration::PARTIALS ? PARTIAL_LABEL : VIEW_LABEL
|
54
|
+
rows_count += 1
|
78
55
|
end
|
79
56
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
elsif avg > WARNING_DEFAULT
|
85
|
-
str.red
|
86
|
-
elsif avg > INFO_DEFAULT
|
87
|
-
str.yellow
|
88
|
-
else
|
89
|
-
str.green
|
90
|
-
end
|
57
|
+
# adding a footer
|
58
|
+
if rows_count > ROWS_FOR_FOOTER
|
59
|
+
table.add_separator
|
60
|
+
table.add_row(HEADER)
|
91
61
|
end
|
92
62
|
|
63
|
+
puts(table)
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def format_row(path, stat, short)
|
69
|
+
[
|
70
|
+
Utils.type_label(stat.type),
|
71
|
+
Utils.path_to_display(path, short: short, length: CONTENT_LENGTH),
|
72
|
+
stat.count,
|
73
|
+
Utils.avg_label(stat.avg),
|
74
|
+
stat.max,
|
75
|
+
stat.min,
|
76
|
+
]
|
77
|
+
end
|
78
|
+
|
79
|
+
def new_table
|
80
|
+
Terminal::Table.new(headings: HEADER, width: DEFAULT_TABLE_WIDTH)
|
81
|
+
end
|
82
|
+
|
93
83
|
end
|
94
|
-
end
|
84
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module LogAnalyzer
|
2
|
+
class PathShortener
|
3
|
+
CHARS_TO_IGNORE = %w(.)
|
4
|
+
MAX_LENGTH = 80
|
5
|
+
|
6
|
+
def initialize(path, max: MAX_LENGTH)
|
7
|
+
@path = path
|
8
|
+
@max = max
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.shrink(path, max: MAX_LENGTH)
|
12
|
+
new(path, max: max).shrink
|
13
|
+
end
|
14
|
+
|
15
|
+
def shrink
|
16
|
+
return path if path.length < max
|
17
|
+
|
18
|
+
File.join(
|
19
|
+
File.dirname(path).split(File::SEPARATOR).map { |dir| short_name(dir) },
|
20
|
+
File.basename(path)
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
attr_reader :path, :max
|
27
|
+
|
28
|
+
def short_name(name)
|
29
|
+
first_char = name[0] || "" # avoid nils in directory names
|
30
|
+
|
31
|
+
CHARS_TO_IGNORE.include?(first_char) ? name[0..1] : first_char
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module LogAnalyzer
|
2
|
+
class Utils
|
3
|
+
DANGER_DEFAULT = 800 # ms
|
4
|
+
WARNING_DEFAULT = 400 # ms
|
5
|
+
INFO_DEFAULT = 100 # ms
|
6
|
+
DEFAULT_PATH_WIDTH = 60
|
7
|
+
PARTIAL_LABEL = " P ".on_green.black.freeze
|
8
|
+
VIEW_LABEL = " V ".on_yellow.black.freeze
|
9
|
+
|
10
|
+
def Utils.find_type(view)
|
11
|
+
if view.split('/'.freeze).last[0] == '_'.freeze
|
12
|
+
LogAnalyzer::Configuration::PARTIALS
|
13
|
+
else
|
14
|
+
LogAnalyzer::Configuration::VIEWS
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def Utils.type_label(type)
|
19
|
+
if type == LogAnalyzer::Configuration::PARTIALS
|
20
|
+
PARTIAL_LABEL
|
21
|
+
else
|
22
|
+
VIEW_LABEL
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def Utils.avg_label(avg)
|
27
|
+
str = avg.to_s
|
28
|
+
if avg > DANGER_DEFAULT
|
29
|
+
str.white.on_red
|
30
|
+
elsif avg > WARNING_DEFAULT
|
31
|
+
str.red
|
32
|
+
elsif avg > INFO_DEFAULT
|
33
|
+
str.yellow
|
34
|
+
else
|
35
|
+
str.green
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def Utils.path_to_display(path, short: false, length: DEFAULT_PATH_WIDTH)
|
40
|
+
if short
|
41
|
+
PathShortener.shrink(path, max: length.last)
|
42
|
+
else
|
43
|
+
path[length]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/lib/log_analyzer/version.rb
CHANGED
data/lib/log_analyzer.rb
CHANGED
@@ -2,11 +2,17 @@ require "terminal-table"
|
|
2
2
|
require "colorize"
|
3
3
|
require "log_analyzer/version"
|
4
4
|
require "log_analyzer/configuration"
|
5
|
+
require "log_analyzer/utils"
|
5
6
|
require "log_analyzer/stat"
|
6
7
|
require "log_analyzer/analyzer"
|
8
|
+
require "log_analyzer/path_shortener"
|
7
9
|
|
8
10
|
module LogAnalyzer
|
9
|
-
def
|
11
|
+
def LogAnalyzer.analyze(filename:)
|
10
12
|
LogAnalyzer::Analyzer.new(filename: filename)
|
11
13
|
end
|
14
|
+
|
15
|
+
def LogAnalyzer.configuration
|
16
|
+
LogAnalyzer::Configuration.configuration
|
17
|
+
end
|
12
18
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: log_analyzer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Igor Kasyanchuk
|
@@ -139,7 +139,9 @@ files:
|
|
139
139
|
- lib/log_analyzer.rb
|
140
140
|
- lib/log_analyzer/analyzer.rb
|
141
141
|
- lib/log_analyzer/configuration.rb
|
142
|
+
- lib/log_analyzer/path_shortener.rb
|
142
143
|
- lib/log_analyzer/stat.rb
|
144
|
+
- lib/log_analyzer/utils.rb
|
143
145
|
- lib/log_analyzer/version.rb
|
144
146
|
- log_analyzer.gemspec
|
145
147
|
homepage: https://github.com/igorkasyanchuk
|