spring_onion 0.1.1 → 0.2.1

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
  SHA256:
3
- metadata.gz: 9029f4da9a3fa2875d738d9bd505fef3784ac40987453f30974eb3ce1d226bf8
4
- data.tar.gz: 53eaebd2f28fa3257a9477e2fc498f08be2e944375a9316a1d746c58f6b37f4e
3
+ metadata.gz: f81f2b07f2a586b8e02444106908d6d8eb8631d772db5bff80dd0fa6ee72b550
4
+ data.tar.gz: 0e23da7945c119c5c5dcf90f2ead4985647536d0c7f3cb091b47e8f2c94ae168
5
5
  SHA512:
6
- metadata.gz: 8013cf7f3086f2f0ee8abfb6261327f98f0983adc3adb1dd2945b5cc6039e68e979181f77ed95c4d87825cd32ab54cb6baf497e47c1c7424211cc27628d93d4f
7
- data.tar.gz: 57cf5c24c5bf52b543c64a281b2474612ba0861f85180748d92ee26fcf420675477004d8dd73a1f5e59fa8d1297e0092746767292073a03bbd888a916a153c1a
6
+ metadata.gz: 115c03712806b8b97d9ff1e65f512db06e8ebc9f4e555b743d97c57b9159c58dc4f43410b7fb73db178a584425e22b403f6005ed3be6cb9a01c57b3295c686e3
7
+ data.tar.gz: 4aa1951677c97b2fa2b45e2eeb3d49b51543e1632537c316a9d73d775b73b95472ced9074e06adc32d4c523bef5f069b9c4826c1f4bdf0a1e638e61d458dc2e1
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spring_onion (0.1.1)
4
+ spring_onion (0.2.1)
5
5
  activerecord
6
+ coderay
6
7
  mysql2
7
8
 
8
9
  GEM
@@ -24,6 +25,7 @@ GEM
24
25
  rake
25
26
  thor (>= 0.14.0)
26
27
  ast (2.4.1)
28
+ coderay (1.1.3)
27
29
  concurrent-ruby (1.1.6)
28
30
  diff-lcs (1.4.4)
29
31
  i18n (1.8.3)
data/README.md CHANGED
@@ -39,8 +39,8 @@ SpringOnion.source_filter_re = //
39
39
 
40
40
  class Employee < ActiveRecord::Base; end
41
41
 
42
- Employee.all.to_a.count
43
- #=> SpringOnion INFO 2020-07-18 01:53:27 +0900 {"sql":"SELECT `employees`.* FROM `employees`","explain":[{"line":1,"select_type":"SIMPLE","table":"employees","partitions":null,"type":"ALL","possible_keys":null,"key":null,"key_len":null,"ref":null,"rows":298936,"filtered":100.0,"Extra":null}],"violations":{"line 1":["slow_type"]},"backtrace":["/foo/bar/zoo/baz.rb:18:in `\u003ctop (required)\u003e'"]}
42
+ p Employee.all.to_a.count
43
+ #=> SpringOnion INFO 2020-07-18 01:53:27 +0900 {"sql":"SELECT `employees`.* FROM `employees`","explain":[{"line":1,"select_type":"SIMPLE","table":"employees","partitions":null,"type":"ALL","possible_keys":null,"key":null,"key_len":null,"ref":null,"rows":298936,"filtered":100.0,"Extra":null}],"warnings":{"line 1":["slow_type"]},"backtrace":["/foo/bar/zoo/baz.rb:18:in `\u003ctop (required)\u003e'"]}
44
44
  #=> 300024
45
45
  ```
46
46
 
@@ -65,7 +65,7 @@ Employee.all.to_a.count
65
65
  "Extra": null
66
66
  }
67
67
  ],
68
- "violations": {
68
+ "warnings": {
69
69
  "line 1": [
70
70
  "slow_type"
71
71
  ]
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'logger'
4
+ require 'coderay'
4
5
  require 'active_support'
5
6
 
6
7
  require 'spring_onion/config'
@@ -17,7 +17,7 @@ module SpringOnion
17
17
  'Using temporary'
18
18
  )
19
19
 
20
- @violations = {
20
+ @warnings = {
21
21
  slow_select_type: ->(exp) { SLOW_SELECT_TYPE_RE =~ exp['select_type'] },
22
22
  slow_type: ->(exp) { SLOW_TYPE_RE =~ exp['type'] },
23
23
  slow_possible_keys: ->(exp) { SLOW_POSSIBLE_KEYS_RE =~ exp['possible_keys'] },
@@ -42,22 +42,21 @@ module SpringOnion
42
42
  @ignore_sql_filter_re !~ sql && @sql_filter_re =~ sql
43
43
  end
44
44
 
45
- @source_filter_re = Regexp.union(
46
- [%r{/app/}].tap do |ary|
47
- re = ENV['SPRING_ONION_SOURCE_FILTER_RE']
48
- ary << Regexp.new(re) if re
49
- end
50
- )
45
+ @source_filter_re = ENV['SPRING_ONION_SOURCE_FILTER_RE'].yield_self do |re|
46
+ re ? Regexp.new(re) : %r{/app/}
47
+ end
51
48
 
52
49
  @ignore_source_filter_re = Regexp.union(
53
- [RbConfig::TOPDIR, *Gem.path].tap do |ary|
50
+ [RbConfig::TOPDIR, *Gem.path, '/.rbenv/versions/'].tap do |ary|
54
51
  re = ENV['SPRING_ONION_IGNORE_SOURCE_FILTER_RE']
55
52
  ary << Regexp.new(re) if re
56
53
  end
57
54
  )
58
55
 
59
56
  @source_filter = lambda do |backtrace_lines|
60
- backtrace_lines.grep_v(@ignore_source_filter_re).grep(@source_filter_re)
57
+ backtrace_lines = backtrace_lines.grep_v(@ignore_source_filter_re)
58
+ idx = backtrace_lines.index { |l| @source_filter_re =~ l }
59
+ idx ? backtrace_lines.slice(idx..-1) : []
61
60
  end
62
61
 
63
62
  @logger = Logger.new($stdout).tap do |logger|
@@ -67,14 +66,18 @@ module SpringOnion
67
66
  end
68
67
 
69
68
  @trace_len = 3
69
+ @json_pretty = (/\A(1|true)\z/i =~ ENV['SPRING_ONION_JSON_PRETTY'])
70
+ @color = /\A(1|true)\z/i =~ ENV.fetch('SPRING_ONION_COLOR', $stdout.tty?.to_s)
70
71
 
71
72
  class << self
72
73
  attr_accessor :enabled,
73
74
  :connection,
74
- :violations,
75
+ :warnings,
75
76
  :sql_filter_re, :ignore_sql_filter_re, :sql_filter,
76
77
  :source_filter_re, :ignore_source_filter_re, :source_filter,
77
78
  :logger,
78
- :trace_len
79
+ :trace_len,
80
+ :json_pretty,
81
+ :color
79
82
  end
80
83
  end
@@ -30,25 +30,34 @@ module SpringOnion
30
30
  end
31
31
 
32
32
  def _validate(exp, sql, trace)
33
- violations = SpringOnion.violations
34
- violation_names_by_line = {}
33
+ warnings = SpringOnion.warnings
34
+ warning_names_by_line = {}
35
35
 
36
36
  exp.each_with_index do |row, i|
37
- violation_names = violations.select do |_name, validator|
37
+ warning_names = warnings.select do |_name, validator|
38
38
  validator.call(row)
39
39
  end.keys
40
40
 
41
- violation_names_by_line["line #{i + 1}"] = violation_names unless violation_names.empty?
41
+ warning_names_by_line["line #{i + 1}"] = warning_names unless warning_names.empty?
42
42
  end
43
43
 
44
- return if violation_names_by_line.empty?
44
+ return if warning_names_by_line.empty?
45
45
 
46
- SpringOnion.logger.info({
46
+ h = {
47
47
  sql: sql,
48
48
  explain: exp.each_with_index.map { |r, i| { line: i + 1 }.merge(r) },
49
- violations: violation_names_by_line,
49
+ warnings: warning_names_by_line,
50
50
  backtrace: trace.slice(0, SpringOnion.trace_len),
51
- }.to_json)
51
+ }
52
+
53
+ line = if SpringOnion.json_pretty
54
+ JSON.pretty_generate(h)
55
+ else
56
+ JSON.dump(h)
57
+ end
58
+
59
+ line = CodeRay.scan(line, :json).terminal if SpringOnion.color
60
+ SpringOnion.logger.info(line)
52
61
  end
53
62
  end
54
63
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SpringOnion
4
- VERSION = '0.1.1'
4
+ VERSION = '0.2.1'
5
5
  end
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = 'Log MySQL queries with EXPLAIN that may be slow.'
13
13
  spec.homepage = 'https://github.com/winebarrel/spring_onion'
14
14
  spec.license = 'MIT'
15
- spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
16
16
 
17
17
  # Specify which files should be added to the gem when it is released.
18
18
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.require_paths = ['lib']
25
25
 
26
26
  spec.add_dependency 'activerecord'
27
+ spec.add_dependency 'coderay'
27
28
  spec.add_dependency 'mysql2'
28
29
  spec.add_development_dependency 'appraisal'
29
30
  spec.add_development_dependency 'bundler'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spring_onion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - winebarrel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-17 00:00:00.000000000 Z
11
+ date: 2020-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: coderay
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'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: mysql2
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -142,7 +156,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
156
  requirements:
143
157
  - - ">="
144
158
  - !ruby/object:Gem::Version
145
- version: 2.3.0
159
+ version: 2.5.0
146
160
  required_rubygems_version: !ruby/object:Gem::Requirement
147
161
  requirements:
148
162
  - - ">="